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 x.pow(exponent); | 13 return _intPow(x, exponent); |
14 } | 14 } |
15 // Double.pow will call exponent.toDouble(). | 15 // doublePow will call exponent.toDouble(). |
16 return x.toDouble().pow(exponent); | 16 return _doublePow(x.toDouble(), exponent); |
| 17 } |
| 18 |
| 19 double _doublePow(double base, num exponent) { |
| 20 if (exponent == 0) { |
| 21 return 1.0; // ECMA-262 15.8.2.13 |
| 22 } |
| 23 if (exponent is! num) { |
| 24 throw new ArgumentError(null); |
| 25 } |
| 26 double doubleExponent = exponent.toDouble(); |
| 27 if (base.isNaN || exponent.isNaN) { |
| 28 return double.NAN; |
| 29 } |
| 30 return _pow(base, doubleExponent); |
| 31 } |
| 32 |
| 33 double _pow(double base, double exponent) native "Math_doublePow"; |
| 34 |
| 35 int _intPow(int base, int exponent) { |
| 36 // Exponentiation by squaring. |
| 37 int result = 1; |
| 38 while (exponent != 0) { |
| 39 if ((exponent & 1) == 1) { |
| 40 result *= base; |
| 41 } |
| 42 exponent >>= 1; |
| 43 // Skip unnecessary operation (can overflow to Mint or Bigint). |
| 44 if (exponent != 0) { |
| 45 base *= base; |
| 46 } |
| 47 } |
| 48 return result; |
17 } | 49 } |
18 | 50 |
19 patch double atan2(num a, num b) => _atan2(a.toDouble(), b.toDouble()); | 51 patch double atan2(num a, num b) => _atan2(a.toDouble(), b.toDouble()); |
20 patch double sin(num value) => _sin(value.toDouble()); | 52 patch double sin(num value) => _sin(value.toDouble()); |
21 patch double cos(num value) => _cos(value.toDouble()); | 53 patch double cos(num value) => _cos(value.toDouble()); |
22 patch double tan(num value) => _tan(value.toDouble()); | 54 patch double tan(num value) => _tan(value.toDouble()); |
23 patch double acos(num value) => _acos(value.toDouble()); | 55 patch double acos(num value) => _acos(value.toDouble()); |
24 patch double asin(num value) => _asin(value.toDouble()); | 56 patch double asin(num value) => _asin(value.toDouble()); |
25 patch double atan(num value) => _atan(value.toDouble()); | 57 patch double atan(num value) => _atan(value.toDouble()); |
26 patch double sqrt(num value) => _sqrt(value.toDouble()); | 58 patch double sqrt(num value) => _sqrt(value.toDouble()); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 static int _nextSeed() { | 163 static int _nextSeed() { |
132 if (_prng == null) { | 164 if (_prng == null) { |
133 // TODO(iposva): Use system to get a random seed. | 165 // TODO(iposva): Use system to get a random seed. |
134 _prng = new Random(new DateTime.now().millisecondsSinceEpoch); | 166 _prng = new Random(new DateTime.now().millisecondsSinceEpoch); |
135 } | 167 } |
136 // Trigger the PRNG once to change the internal state. | 168 // Trigger the PRNG once to change the internal state. |
137 _prng._nextState(); | 169 _prng._nextState(); |
138 return _prng._state[kSTATE_LO]; | 170 return _prng._state[kSTATE_LO]; |
139 } | 171 } |
140 } | 172 } |
OLD | NEW |