| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // A VM patch of the dart:math library. |
| 6 |
| 7 patch int parseInt(String str) => MathNatives.parseInt(str); |
| 8 |
| 9 patch double parseDouble(String str) => MathNatives.parseDouble(str); |
| 10 |
| 11 patch double atan2(num a, num b) => MathNatives.atan2(a, b); |
| 12 |
| 13 patch num pow(num x, num exponent) => MathNatives.pow(x, exponent); |
| 14 |
| 15 patch double sin(num x) => MathNatives.sin(x); |
| 16 patch double cos(num x) => MathNatives.cos(x); |
| 17 patch double tan(num x) => MathNatives.tan(x); |
| 18 patch double acos(num x) => MathNatives.acos(x); |
| 19 patch double asin(num x) => MathNatives.asin(x); |
| 20 patch double atan(num x) => MathNatives.atan(x); |
| 21 patch double sqrt(num x) => MathNatives.sqrt(x); |
| 22 patch double exp(num x) => MathNatives.exp(x); |
| 23 patch double log(num x) => MathNatives.log(x); |
| 24 |
| 25 |
| 26 // TODO(iposva): Handle patch methods within a patch class correctly. |
| 27 patch class Random { |
| 28 |
| 29 /*patch*/ factory Random([int seed]) { |
| 30 if (seed == null) { |
| 31 seed = _Random._nextSeed(); |
| 32 } |
| 33 do { |
| 34 seed = (seed + 0x5A17) & _Random._MASK_64; |
| 35 } while (seed == 0); |
| 36 return new _Random._internal(seed); |
| 37 } |
| 38 } |
| 39 |
| 40 |
| 41 class _Random implements Random { |
| 42 // Internal state of the random number generator. |
| 43 var _state; |
| 44 |
| 45 _Random._internal(this._state); |
| 46 |
| 47 // The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32. |
| 48 // http://en.wikipedia.org/wiki/Multiply-with-carry |
| 49 // The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1. |
| 50 int _nextInt32() { |
| 51 _state = ((_A * (_state & _MASK_32)) + (_state >> 32)) & _MASK_64; |
| 52 return _state & _MASK_32; |
| 53 } |
| 54 |
| 55 int nextInt(int max) { |
| 56 if (max <= 0 || max > _POW2_32) { |
| 57 throw new IllegalArgumentException("max must be positive and < 2^32:" |
| 58 " $max"); |
| 59 } |
| 60 if ((max & -max) == max) { |
| 61 // Fast case for powers of two. |
| 62 return _nextInt32() & (max - 1); |
| 63 } |
| 64 |
| 65 var rnd32; |
| 66 var result; |
| 67 do { |
| 68 rnd32 = _nextInt32(); |
| 69 result = rnd32 % max; |
| 70 } while (rnd32 - result + max >= _POW2_32); |
| 71 return result; |
| 72 } |
| 73 |
| 74 double nextDouble() { |
| 75 return ((nextInt(1 << (26)) << 27) + nextInt(1 << 27)) / _POW2_53_D; |
| 76 } |
| 77 |
| 78 bool nextBool() { |
| 79 return nextInt(1) == 0; |
| 80 } |
| 81 |
| 82 // Constants used by the algorithm or masking. |
| 83 static final _MASK_32 = (1 << 32) - 1; |
| 84 static final _MASK_64 = (1 << 64) - 1; |
| 85 static final _POW2_32 = 1 << 32; |
| 86 static final _POW2_53_D = 1.0 * (1 << 53); |
| 87 |
| 88 static final _A = 0xffffda61; |
| 89 |
| 90 // Use a singleton Random object to get a new seed if no seed was passed. |
| 91 static var _prng = null; |
| 92 |
| 93 static int _nextSeed() { |
| 94 if (_prng == null) { |
| 95 // TODO(iposva): Use system to get a random seed. |
| 96 _prng = new Random(new Date.now().millisecondsSinceEpoch); |
| 97 } |
| 98 // Trigger the PRNG once to change the internal state. |
| 99 _prng._nextInt32(); |
| 100 return _prng._state; |
| 101 } |
| 102 } |
| OLD | NEW |