| 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 if (base == 1.0) return 1.0; | 22 if (base == 1.0) return 1.0; |
| 23 if (exponent == 1.0) return base; |
| 24 if (exponent == 2.0) return base * base; |
| 25 if (exponent == 3.0) return base * base * base; |
| 26 |
| 23 if (base.isNaN || exponent.isNaN) { | 27 if (base.isNaN || exponent.isNaN) { |
| 24 return double.NAN; | 28 return double.NAN; |
| 25 } | 29 } |
| 26 return _pow(base, exponent); | 30 return _pow(base, exponent); |
| 27 } | 31 } |
| 28 | 32 |
| 29 double _pow(double base, double exponent) native "Math_doublePow"; | 33 double _pow(double base, double exponent) native "Math_doublePow"; |
| 30 | 34 |
| 31 int _intPow(int base, int exponent) { | 35 int _intPow(int base, int exponent) { |
| 32 // Exponentiation by squaring. | 36 // Exponentiation by squaring. |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; | 154 static Uint32List _setupSeed(int seed) native "Random_setupSeed"; |
| 151 // Get a seed from the VM's random number provider. | 155 // Get a seed from the VM's random number provider. |
| 152 static Uint32List _initialSeed() native "Random_initialSeed"; | 156 static Uint32List _initialSeed() native "Random_initialSeed"; |
| 153 | 157 |
| 154 static int _nextSeed() { | 158 static int _nextSeed() { |
| 155 // Trigger the PRNG once to change the internal state. | 159 // Trigger the PRNG once to change the internal state. |
| 156 _prng._nextState(); | 160 _prng._nextState(); |
| 157 return _prng._state[kSTATE_LO]; | 161 return _prng._state[kSTATE_LO]; |
| 158 } | 162 } |
| 159 } | 163 } |
| OLD | NEW |