OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "dart:typed_data"; | 5 import "dart:typed_data"; |
6 | 6 |
7 // A VM patch of the dart:math library. | 7 // A VM patch of the dart:math library. |
8 | 8 |
| 9 @patch |
| 10 T min<T extends num>(T a, T b) { |
| 11 // These partially redundant type checks improve code quality for dart2js. |
| 12 // Most of the improvement is at call sites from the inferred non-null num |
| 13 // return type. |
| 14 if (a is! num) throw new ArgumentError(a); |
| 15 if (b is! num) throw new ArgumentError(b); |
| 16 |
| 17 if (a > b) return b; |
| 18 if (a < b) return a; |
| 19 if (b is double) { |
| 20 // Special case for NaN and -0.0. If one argument is NaN return NaN. |
| 21 // [min] must also distinguish between -0.0 and 0.0. |
| 22 if (a is double) { |
| 23 if (a == 0.0) { |
| 24 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN. |
| 25 // The following returns -0.0 if either a or b is -0.0, and it |
| 26 // returns NaN if b is NaN. |
| 27 return (a + b) * a * b; |
| 28 } |
| 29 } |
| 30 // Check for NaN and b == -0.0. |
| 31 if (a == 0 && b.isNegative || b.isNaN) return b; |
| 32 return a; |
| 33 } |
| 34 return a; |
| 35 } |
| 36 |
| 37 @patch |
| 38 T max<T extends num>(T a, T b) { |
| 39 // These partially redundant type checks improve code quality for dart2js. |
| 40 // Most of the improvement is at call sites from the inferred non-null num |
| 41 // return type. |
| 42 if (a is! num) throw new ArgumentError(a); |
| 43 if (b is! num) throw new ArgumentError(b); |
| 44 |
| 45 if (a > b) return a; |
| 46 if (a < b) return b; |
| 47 if (b is double) { |
| 48 // Special case for NaN and -0.0. If one argument is NaN return NaN. |
| 49 // [max] must also distinguish between -0.0 and 0.0. |
| 50 if (a is double) { |
| 51 if (a == 0.0) { |
| 52 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN. |
| 53 // The following returns 0.0 if either a or b is 0.0, and it |
| 54 // returns NaN if b is NaN. |
| 55 return a + b; |
| 56 } |
| 57 } |
| 58 // Check for NaN. |
| 59 if (b.isNaN) return b; |
| 60 return a; |
| 61 } |
| 62 // max(-0.0, 0) must return 0. |
| 63 if (b == 0 && a.isNegative) return b; |
| 64 return a; |
| 65 } |
| 66 |
9 // If [x] is an [int] and [exponent] is a non-negative [int], the result is | 67 // If [x] is an [int] and [exponent] is a non-negative [int], the result is |
10 // an [int], otherwise the result is a [double]. | 68 // an [int], otherwise the result is a [double]. |
11 @patch | 69 @patch |
12 num pow(num x, num exponent) { | 70 num pow(num x, num exponent) { |
13 if ((x is int) && (exponent is int) && (exponent >= 0)) { | 71 if ((x is int) && (exponent is int) && (exponent >= 0)) { |
14 return _intPow(x, exponent); | 72 return _intPow(x, exponent); |
15 } | 73 } |
16 return _doublePow(x.toDouble(), exponent.toDouble()); | 74 return _doublePow(x.toDouble(), exponent.toDouble()); |
17 } | 75 } |
18 | 76 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 } | 273 } |
216 | 274 |
217 bool nextBool() { | 275 bool nextBool() { |
218 return _getBytes(1).isEven; | 276 return _getBytes(1).isEven; |
219 } | 277 } |
220 | 278 |
221 // Constants used by the algorithm. | 279 // Constants used by the algorithm. |
222 static const _POW2_32 = 1 << 32; | 280 static const _POW2_32 = 1 << 32; |
223 static const _POW2_53_D = 1.0 * (1 << 53); | 281 static const _POW2_53_D = 1.0 * (1 << 53); |
224 } | 282 } |
OLD | NEW |