| 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 // If [x] is an [int] and [exponent] is a non-negative [int], the result is | 9 // If [x] is an [int] and [exponent] is a non-negative [int], the result is |
| 10 // an [int], otherwise the result is a [double]. | 10 // an [int], otherwise the result is a [double]. |
| 11 patch num pow(num x, num exponent) { | 11 @patch num pow(num x, num exponent) { |
| 12 if ((x is int) && (exponent is int) && (exponent >= 0)) { | 12 if ((x is int) && (exponent is int) && (exponent >= 0)) { |
| 13 return _intPow(x, exponent); | 13 return _intPow(x, exponent); |
| 14 } | 14 } |
| 15 return _doublePow(x.toDouble(), exponent.toDouble()); | 15 return _doublePow(x.toDouble(), exponent.toDouble()); |
| 16 } | 16 } |
| 17 | 17 |
| 18 double _doublePow(double base, double exponent) { | 18 double _doublePow(double base, double exponent) { |
| 19 if (exponent == 0.0) { | 19 if (exponent == 0.0) { |
| 20 return 1.0; // ECMA-262 15.8.2.13 | 20 return 1.0; // ECMA-262 15.8.2.13 |
| 21 } | 21 } |
| 22 // Speed up simple cases. | 22 // Speed up simple cases. |
| 23 if (exponent == 1.0) return base; | 23 if (exponent == 1.0) return base; |
| 24 if (exponent == 2.0) return base * base; | 24 if (exponent == 2.0) return base * base; |
| 25 if (exponent == 3.0) return base * base * base; | 25 if (exponent == 3.0) return base * base * base; |
| 26 | 26 |
| 27 if (base == 1.0) return 1.0; | 27 if (base == 1.0) return 1.0; |
| 28 | 28 |
| 29 if (base.isNaN || exponent.isNaN) { | 29 if (base.isNaN || exponent.isNaN) { |
| 30 return double.NAN; | 30 return double.NAN; |
| 31 } | 31 } |
| 32 if ((base != -double.INFINITY) && (exponent == 0.5)) { | 32 if ((base != -double.INFINITY) && (exponent == 0.5)) { |
| 33 if (base == 0.0) { | 33 if (base == 0.0) { |
| 34 return 0.0; | 34 return 0.0; |
| 35 } | 35 } |
| 36 return sqrt(base); | 36 return sqrt(base); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 49 } | 49 } |
| 50 exponent >>= 1; | 50 exponent >>= 1; |
| 51 // Skip unnecessary operation (can overflow to Mint or Bigint). | 51 // Skip unnecessary operation (can overflow to Mint or Bigint). |
| 52 if (exponent != 0) { | 52 if (exponent != 0) { |
| 53 base *= base; | 53 base *= base; |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 return result; | 56 return result; |
| 57 } | 57 } |
| 58 | 58 |
| 59 patch double atan2(num a, num b) => _atan2(a.toDouble(), b.toDouble()); | 59 @patch double atan2(num a, num b) => _atan2(a.toDouble(), b.toDouble()); |
| 60 patch double sin(num value) => _sin(value.toDouble()); | 60 @patch double sin(num value) => _sin(value.toDouble()); |
| 61 patch double cos(num value) => _cos(value.toDouble()); | 61 @patch double cos(num value) => _cos(value.toDouble()); |
| 62 patch double tan(num value) => _tan(value.toDouble()); | 62 @patch double tan(num value) => _tan(value.toDouble()); |
| 63 patch double acos(num value) => _acos(value.toDouble()); | 63 @patch double acos(num value) => _acos(value.toDouble()); |
| 64 patch double asin(num value) => _asin(value.toDouble()); | 64 @patch double asin(num value) => _asin(value.toDouble()); |
| 65 patch double atan(num value) => _atan(value.toDouble()); | 65 @patch double atan(num value) => _atan(value.toDouble()); |
| 66 patch double sqrt(num value) => _sqrt(value.toDouble()); | 66 @patch double sqrt(num value) => _sqrt(value.toDouble()); |
| 67 patch double exp(num value) => _exp(value.toDouble()); | 67 @patch double exp(num value) => _exp(value.toDouble()); |
| 68 patch double log(num value) => _log(value.toDouble()); | 68 @patch double log(num value) => _log(value.toDouble()); |
| 69 | 69 |
| 70 double _atan2(double a, double b) native "Math_atan2"; | 70 double _atan2(double a, double b) native "Math_atan2"; |
| 71 double _sin(double x) native "Math_sin"; | 71 double _sin(double x) native "Math_sin"; |
| 72 double _cos(double x) native "Math_cos"; | 72 double _cos(double x) native "Math_cos"; |
| 73 double _tan(double x) native "Math_tan"; | 73 double _tan(double x) native "Math_tan"; |
| 74 double _acos(double x) native "Math_acos"; | 74 double _acos(double x) native "Math_acos"; |
| 75 double _asin(double x) native "Math_asin"; | 75 double _asin(double x) native "Math_asin"; |
| 76 double _atan(double x) native "Math_atan"; | 76 double _atan(double x) native "Math_atan"; |
| 77 double _sqrt(double x) native "Math_sqrt"; | 77 double _sqrt(double x) native "Math_sqrt"; |
| 78 double _exp(double x) native "Math_exp"; | 78 double _exp(double x) native "Math_exp"; |
| 79 double _log(double x) native "Math_log"; | 79 double _log(double x) native "Math_log"; |
| 80 | 80 |
| 81 | 81 |
| 82 // TODO(iposva): Handle patch methods within a patch class correctly. | 82 // TODO(iposva): Handle patch methods within a patch class correctly. |
| 83 patch class Random { | 83 @patch class Random { |
| 84 | 84 |
| 85 /*patch*/ factory Random([int seed]) { | 85 /*@patch*/ factory Random([int seed]) { |
| 86 var state = _Random._setupSeed((seed == null) ? _Random._nextSeed() : seed); | 86 var state = _Random._setupSeed((seed == null) ? _Random._nextSeed() : seed); |
| 87 // Crank a couple of times to distribute the seed bits a bit further. | 87 // Crank a couple of times to distribute the seed bits a bit further. |
| 88 return new _Random._withState(state).._nextState() | 88 return new _Random._withState(state).._nextState() |
| 89 .._nextState() | 89 .._nextState() |
| 90 .._nextState() | 90 .._nextState() |
| 91 .._nextState(); | 91 .._nextState(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 /*patch*/ factory Random.secure() { | 94 /*@patch*/ factory Random.secure() { |
| 95 return new _SecureRandom(); | 95 return new _SecureRandom(); |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 | 99 |
| 100 class _Random implements Random { | 100 class _Random implements Random { |
| 101 // Internal state of the random number generator. | 101 // Internal state of the random number generator. |
| 102 final _state; | 102 final _state; |
| 103 static const _kSTATE_LO = 0; | 103 static const _kSTATE_LO = 0; |
| 104 static const _kSTATE_HI = 1; // Unused in Dart code. | 104 static const _kSTATE_HI = 1; // Unused in Dart code. |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 | 205 |
| 206 bool nextBool() { | 206 bool nextBool() { |
| 207 return _getBytes(1).isEven; | 207 return _getBytes(1).isEven; |
| 208 } | 208 } |
| 209 | 209 |
| 210 // Constants used by the algorithm. | 210 // Constants used by the algorithm. |
| 211 static const _POW2_32 = 1 << 32; | 211 static const _POW2_32 = 1 << 32; |
| 212 static const _POW2_53_D = 1.0 * (1 << 53); | 212 static const _POW2_53_D = 1.0 * (1 << 53); |
| 213 } | 213 } |
| 214 | 214 |
| OLD | NEW |