Chromium Code Reviews| 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 * Returns NaN if [x] is NaN. | 244 * Returns NaN if [x] is NaN. |
| 245 */ | 245 */ |
| 246 external double exp(num x); | 246 external double exp(num x); |
| 247 | 247 |
| 248 /** | 248 /** |
| 249 * Converts [x] to a double and returns the natural logarithm of the value. | 249 * Converts [x] to a double and returns the natural logarithm of the value. |
| 250 * Returns negative infinity if [x] is equal to zero. | 250 * Returns negative infinity if [x] is equal to zero. |
| 251 * Returns NaN if [x] is NaN or less than zero. | 251 * Returns NaN if [x] is NaN or less than zero. |
| 252 */ | 252 */ |
| 253 external double log(num x); | 253 external double log(num x); |
| 254 | |
| 255 /** | |
| 256 * Computes the greatest common divisor between [a] and [b]. | |
| 257 * | |
| 258 * [a] and [b] must be non-negative integers. | |
| 259 */ | |
| 260 int gcd(int a, int b) { | |
|
Lasse Reichstein Nielsen
2014/08/19 08:31:31
Adding an uncapitalized top-level name to a dart:
srawlins
2014/08/20 01:06:27
Done and done. Thanks for this recommendation. I'v
| |
| 261 if (a is! int) throw new ArgumentError(a); | |
| 262 if (b is! int) throw new ArgumentError(b); | |
| 263 if (a.isNegative || b.isNegative) { | |
| 264 throw new RangeError("a and b must not be negative; received $a and $b."); | |
| 265 } | |
| 266 | |
| 267 // Iterative Binary GCD algorithm. | |
| 268 if (a == 0) return b; | |
| 269 if (b == 0) return a; | |
| 270 int powersOfTwo = 0; | |
| 271 int temp; | |
| 272 while (((a | b) & 1) == 0) { | |
| 273 powersOfTwo++; | |
| 274 a >>= 1; | |
| 275 b >>= 1; | |
|
Lasse Reichstein Nielsen
2014/08/19 08:31:31
This may be surprising when it doesn't work for va
srawlins
2014/08/20 01:06:27
Done.
| |
| 276 } | |
| 277 | |
| 278 while ((a & 1) == 0) a >>= 1; | |
|
Lasse Reichstein Nielsen
2014/08/19 08:31:31
a.isEven, ditto b below.
srawlins
2014/08/20 01:06:27
Done.
| |
| 279 | |
| 280 do { | |
| 281 while ((b & 1) == 0) b >>= 1; | |
| 282 if (a > b) { | |
| 283 temp = b; | |
| 284 b = a; | |
| 285 a = temp; | |
| 286 } | |
| 287 b -= a; | |
| 288 } while (b != 0); | |
| 289 | |
| 290 return a << powersOfTwo; | |
|
Lasse Reichstein Nielsen
2014/08/19 08:31:31
Same problem. You could keep powersOf2 as a factor
srawlins
2014/08/20 01:06:26
Done.
| |
| 291 } | |
| OLD | NEW |