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 // doublePow will call exponent.toDouble(). | 15 // doublePow will call exponent.toDouble(). |
16 return _doublePow(x.toDouble(), exponent); | 16 return _doublePow(x.toDouble(), exponent); |
17 } | 17 } |
18 | 18 |
19 double _doublePow(double base, num exponent) { | 19 double _doublePow(double base, num exponent) { |
20 if (exponent == 0) { | 20 if (exponent == 0) { |
21 return 1.0; // ECMA-262 15.8.2.13 | 21 return 1.0; // ECMA-262 15.8.2.13 |
22 } | 22 } |
23 if (exponent is! num) { | 23 if (base == 1.0) return 1.0; |
24 throw new ArgumentError(null); | |
25 } | |
26 double doubleExponent = exponent.toDouble(); | 24 double doubleExponent = exponent.toDouble(); |
27 if (base.isNaN || exponent.isNaN) { | 25 if (base.isNaN || exponent.isNaN) { |
28 return double.NAN; | 26 return double.NAN; |
29 } | 27 } |
30 return _pow(base, doubleExponent); | 28 return _pow(base, doubleExponent); |
31 } | 29 } |
32 | 30 |
33 double _pow(double base, double exponent) native "Math_doublePow"; | 31 double _pow(double base, double exponent) native "Math_doublePow"; |
34 | 32 |
35 int _intPow(int base, int exponent) { | 33 int _intPow(int base, int exponent) { |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 static int _nextSeed() { | 161 static int _nextSeed() { |
164 if (_prng == null) { | 162 if (_prng == null) { |
165 // TODO(iposva): Use system to get a random seed. | 163 // TODO(iposva): Use system to get a random seed. |
166 _prng = new Random(new DateTime.now().millisecondsSinceEpoch); | 164 _prng = new Random(new DateTime.now().millisecondsSinceEpoch); |
167 } | 165 } |
168 // Trigger the PRNG once to change the internal state. | 166 // Trigger the PRNG once to change the internal state. |
169 _prng._nextState(); | 167 _prng._nextState(); |
170 return _prng._state[kSTATE_LO]; | 168 return _prng._state[kSTATE_LO]; |
171 } | 169 } |
172 } | 170 } |
OLD | NEW |