| 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 /** | 5 /** |
| 6 * Mathematical constants and functions, plus a random number generator. | 6 * Mathematical constants and functions, plus a random number generator. |
| 7 */ | 7 */ |
| 8 library dart.math; | 8 library dart.math; |
| 9 | 9 |
| 10 part "jenkins_smi_hash.dart"; | 10 part "jenkins_smi_hash.dart"; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 } | 120 } |
| 121 // Check for NaN. | 121 // Check for NaN. |
| 122 if (b.isNaN) return b; | 122 if (b.isNaN) return b; |
| 123 return a; | 123 return a; |
| 124 } | 124 } |
| 125 // max(-0.0, 0) must return 0. | 125 // max(-0.0, 0) must return 0. |
| 126 if (b == 0 && a.isNegative) return b; | 126 if (b == 0 && a.isNegative) return b; |
| 127 return a; | 127 return a; |
| 128 } | 128 } |
| 129 | 129 |
| 130 @patch | 130 /** |
| 131 * A variant of [atan]. |
| 132 * |
| 133 * Converts both arguments to doubles. |
| 134 * |
| 135 * Returns the angle between the positive x-axis and the vector ([b],[a]). |
| 136 * The result, in radians, is in the range -PI..PI. |
| 137 * |
| 138 * If [b] is positive, this is the same as [:atan(b/a):]. |
| 139 * |
| 140 * The result is negative when [a] is negative (including when [a] is the |
| 141 * double -0.0). |
| 142 * |
| 143 * If [a] is equal to zero, the vector ([b],[a]) is considered parallel to |
| 144 * the x-axis, even if [b] is also equal to zero. The sign of [b] determines |
| 145 * the direction of the vector along the x-axis. |
| 146 * |
| 147 * Returns NaN if either argument is NaN. |
| 148 */ |
| 131 double atan2(num a, num b) | 149 double atan2(num a, num b) |
| 132 => JS('double', r'Math.atan2(#, #)', checkNum(a), checkNum(b)); | 150 => JS('double', r'Math.atan2(#, #)', checkNum(a), checkNum(b)); |
| 133 | 151 |
| 134 @patch | 152 /** |
| 153 * Returns [x] to the power of [exponent]. |
| 154 * |
| 155 * If [x] is an [int] and [exponent] is a non-negative [int], the result is |
| 156 * an [int], otherwise both arguments are converted to doubles first, and the |
| 157 * result is a [double]. |
| 158 * |
| 159 * For integers, the power is always equal to the mathematical result of `x` to |
| 160 * the power `exponent`, only limited by the available memory. |
| 161 * |
| 162 * For doubles, `pow(x, y)` handles edge cases as follows: |
| 163 * |
| 164 * - if `y` is zero (0.0 or -0.0), the result is always 1.0. |
| 165 * - if `x` is 1.0, the result is always 1.0. |
| 166 * - otherwise, if either `x` or `y` is NaN then the result is NaN. |
| 167 * - if `x` is negative (but not -0.0) and `y` is a finite non-integer, the |
| 168 * result is NaN. |
| 169 * - if `x` is Infinity and `y` is negative, the result is 0.0. |
| 170 * - if `x` is Infinity and `y` is positive, the result is Infinity. |
| 171 * - if `x` is 0.0 and `y` is negative, the result is Infinity. |
| 172 * - if `x` is 0.0 and `y` is positive, the result is 0.0. |
| 173 * - if `x` is -Infinity or -0.0 and `y` is an odd integer, then the result is |
| 174 * `-pow(-x ,y)`. |
| 175 * - if `x` is -Infinity or -0.0 and `y` is not an odd integer, then the result |
| 176 * is the same as `pow(-x , y)`. |
| 177 * - if `y` is Infinity and the absolute value of `x` is less than 1, the |
| 178 * result is 0.0. |
| 179 * - if `y` is Infinity and `x` is -1, the result is 1.0. |
| 180 * - if `y` is Infinity and the absolute value of `x` is greater than 1, |
| 181 * the result is Infinity. |
| 182 * - if `y` is -Infinity, the result is `1/pow(x, Infinity)`. |
| 183 * |
| 184 * This corresponds to the `pow` function defined in the IEEE Standard 754-2008. |
| 185 * |
| 186 * Notice that an [int] result cannot overflow, but a [double] result might |
| 187 * be [double.INFINITY]. |
| 188 */ |
| 135 num pow(num x, num exponent) { | 189 num pow(num x, num exponent) { |
| 136 checkNum(x); | 190 checkNum(x); |
| 137 checkNum(exponent); | 191 checkNum(exponent); |
| 138 return JS('num', r'Math.pow(#, #)', x, exponent); | 192 return JS('num', r'Math.pow(#, #)', x, exponent); |
| 139 } | 193 } |
| 140 | 194 |
| 141 @patch | 195 /** |
| 196 * Converts [x] to a double and returns the sine of the value. |
| 197 * |
| 198 * If [x] is not a finite number, the result is NaN. |
| 199 */ |
| 142 double sin(num x) | 200 double sin(num x) |
| 143 => JS('double', r'Math.sin(#)', checkNum(x)); | 201 => JS('double', r'Math.sin(#)', checkNum(x)); |
| 144 | 202 |
| 145 @patch | 203 /** |
| 204 * Converts [x] to a double and returns the cosine of the value. |
| 205 * |
| 206 * If [x] is not a finite number, the result is NaN. |
| 207 */ |
| 146 double cos(num x) | 208 double cos(num x) |
| 147 => JS('double', r'Math.cos(#)', checkNum(x)); | 209 => JS('double', r'Math.cos(#)', checkNum(x)); |
| 148 | 210 |
| 149 @patch | 211 /** |
| 212 * Converts [x] to a double and returns the tangent of the value. |
| 213 * |
| 214 * The tangent function is equivalent to [:sin(x)/cos(x):] and may be |
| 215 * infinite (positive or negative) when [:cos(x):] is equal to zero. |
| 216 * If [x] is not a finite number, the result is NaN. |
| 217 */ |
| 150 double tan(num x) | 218 double tan(num x) |
| 151 => JS('double', r'Math.tan(#)', checkNum(x)); | 219 => JS('double', r'Math.tan(#)', checkNum(x)); |
| 152 | 220 |
| 153 @patch | 221 /** |
| 222 * Converts [x] to a double and returns the arc cosine of the value. |
| 223 * |
| 224 * Returns a value in the range -PI..PI, or NaN if [x] is outside |
| 225 * the range -1..1. |
| 226 */ |
| 154 double acos(num x) | 227 double acos(num x) |
| 155 => JS('double', r'Math.acos(#)', checkNum(x)); | 228 => JS('double', r'Math.acos(#)', checkNum(x)); |
| 156 | 229 |
| 157 @patch | 230 /** |
| 231 * Converts [x] to a double and returns the arc sine of the value. |
| 232 * Returns a value in the range -PI..PI, or NaN if [x] is outside |
| 233 * the range -1..1. |
| 234 */ |
| 158 double asin(num x) | 235 double asin(num x) |
| 159 => JS('double', r'Math.asin(#)', checkNum(x)); | 236 => JS('double', r'Math.asin(#)', checkNum(x)); |
| 160 | 237 |
| 161 @patch | 238 /** |
| 239 * Converts [x] to a dobule and returns the arc tangent of the vlaue. |
| 240 * Returns a value in the range -PI/2..PI/2, or NaN if [x] is NaN. |
| 241 */ |
| 162 double atan(num x) | 242 double atan(num x) |
| 163 => JS('double', r'Math.atan(#)', checkNum(x)); | 243 => JS('double', r'Math.atan(#)', checkNum(x)); |
| 164 | 244 |
| 165 @patch | 245 /** |
| 246 * Converts [x] to a double and returns the positive square root of the value. |
| 247 * |
| 248 * Returns -0.0 if [x] is -0.0, and NaN if [x] is otherwise negative or NaN. |
| 249 */ |
| 166 double sqrt(num x) | 250 double sqrt(num x) |
| 167 => JS('double', r'Math.sqrt(#)', checkNum(x)); | 251 => JS('double', r'Math.sqrt(#)', checkNum(x)); |
| 168 | 252 |
| 169 @patch | 253 /** |
| 254 * Converts [x] to a double and returns the natural exponent, [E], |
| 255 * to the power [x]. |
| 256 * Returns NaN if [x] is NaN. |
| 257 */ |
| 170 double exp(num x) | 258 double exp(num x) |
| 171 => JS('double', r'Math.exp(#)', checkNum(x)); | 259 => JS('double', r'Math.exp(#)', checkNum(x)); |
| 172 | 260 |
| 173 @patch | 261 /** |
| 262 * Converts [x] to a double and returns the natural logarithm of the value. |
| 263 * Returns negative infinity if [x] is equal to zero. |
| 264 * Returns NaN if [x] is NaN or less than zero. |
| 265 */ |
| 174 double log(num x) | 266 double log(num x) |
| 175 => JS('double', r'Math.log(#)', checkNum(x)); | 267 => JS('double', r'Math.log(#)', checkNum(x)); |
| 176 | 268 |
| 177 const int _POW2_32 = 0x100000000; | 269 const int _POW2_32 = 0x100000000; |
| 178 class _JSRandom implements Random { | 270 class _JSRandom implements Random { |
| 179 // The Dart2JS implementation of Random doesn't use a seed. | 271 // The Dart2JS implementation of Random doesn't use a seed. |
| 180 const _JSRandom(); | 272 const _JSRandom(); |
| 181 | 273 |
| 182 int nextInt(int max) { | 274 int nextInt(int max) { |
| 183 if (max <= 0 || max > _POW2_32) { | 275 if (max <= 0 || max > _POW2_32) { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 _nextState(); | 432 _nextState(); |
| 341 int bits27 = _lo & ((1 << 27) - 1); | 433 int bits27 = _lo & ((1 << 27) - 1); |
| 342 return (bits26 * _POW2_27_D + bits27) / _POW2_53_D; | 434 return (bits26 * _POW2_27_D + bits27) / _POW2_53_D; |
| 343 } | 435 } |
| 344 | 436 |
| 345 bool nextBool() { | 437 bool nextBool() { |
| 346 _nextState(); | 438 _nextState(); |
| 347 return (_lo & 1) == 0; | 439 return (_lo & 1) == 0; |
| 348 } | 440 } |
| 349 } | 441 } |
| OLD | NEW |