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 * To use this library in your code: | 8 * To use this library in your code: |
| 9 * | 9 * |
| 10 * import 'dart:math'; | 10 * import 'dart:math'; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 | 68 |
| 69 /** | 69 /** |
| 70 * Returns the lesser of two numbers. | 70 * Returns the lesser of two numbers. |
| 71 * | 71 * |
| 72 * Returns NaN if either argument is NaN. | 72 * Returns NaN if either argument is NaN. |
| 73 * The lesser of `-0.0` and `0.0` is `-0.0`. | 73 * The lesser of `-0.0` and `0.0` is `-0.0`. |
| 74 * If the arguments are otherwise equal (including int and doubles with the | 74 * If the arguments are otherwise equal (including int and doubles with the |
| 75 * same mathematical value) then it is unspecified which of the two arguments | 75 * same mathematical value) then it is unspecified which of the two arguments |
| 76 * is returned. | 76 * is returned. |
| 77 */ | 77 */ |
| 78 T min<T extends num>(T a, T b) { | 78 external T min<T extends num>(T a, T b); |
|
Lasse Reichstein Nielsen
2017/05/05 06:23:25
I think this'll be the first patched generic metho
| |
| 79 // These partially redundant type checks improve code quality for dart2js. | |
| 80 // Most of the improvement is at call sites from the inferred non-null num | |
| 81 // return type. | |
| 82 if (a is! num) throw new ArgumentError(a); | |
| 83 if (b is! num) throw new ArgumentError(b); | |
| 84 | |
| 85 if (a > b) return b; | |
| 86 if (a < b) return a; | |
| 87 if (b is double) { | |
| 88 // Special case for NaN and -0.0. If one argument is NaN return NaN. | |
| 89 // [min] must also distinguish between -0.0 and 0.0. | |
| 90 if (a is double) { | |
| 91 if (a == 0.0) { | |
| 92 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN. | |
| 93 // The following returns -0.0 if either a or b is -0.0, and it | |
| 94 // returns NaN if b is NaN. | |
| 95 return (a + b) * a * b; | |
| 96 } | |
| 97 } | |
| 98 // Check for NaN and b == -0.0. | |
| 99 if (a == 0 && b.isNegative || b.isNaN) return b; | |
| 100 return a; | |
| 101 } | |
| 102 return a; | |
| 103 } | |
| 104 | 79 |
| 105 /** | 80 /** |
| 106 * Returns the larger of two numbers. | 81 * Returns the larger of two numbers. |
| 107 * | 82 * |
| 108 * Returns NaN if either argument is NaN. | 83 * Returns NaN if either argument is NaN. |
| 109 * The larger of `-0.0` and `0.0` is `0.0`. If the arguments are | 84 * The larger of `-0.0` and `0.0` is `0.0`. If the arguments are |
| 110 * otherwise equal (including int and doubles with the same mathematical value) | 85 * otherwise equal (including int and doubles with the same mathematical value) |
| 111 * then it is unspecified which of the two arguments is returned. | 86 * then it is unspecified which of the two arguments is returned. |
| 112 */ | 87 */ |
| 113 T max<T extends num>(T a, T b) { | 88 external T max<T extends num>(T a, T b); |
| 114 // These partially redundant type checks improve code quality for dart2js. | |
| 115 // Most of the improvement is at call sites from the inferred non-null num | |
| 116 // return type. | |
| 117 if (a is! num) throw new ArgumentError(a); | |
| 118 if (b is! num) throw new ArgumentError(b); | |
| 119 | |
| 120 if (a > b) return a; | |
| 121 if (a < b) return b; | |
| 122 if (b is double) { | |
| 123 // Special case for NaN and -0.0. If one argument is NaN return NaN. | |
| 124 // [max] must also distinguish between -0.0 and 0.0. | |
| 125 if (a is double) { | |
| 126 if (a == 0.0) { | |
| 127 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN. | |
| 128 // The following returns 0.0 if either a or b is 0.0, and it | |
| 129 // returns NaN if b is NaN. | |
| 130 return a + b; | |
| 131 } | |
| 132 } | |
| 133 // Check for NaN. | |
| 134 if (b.isNaN) return b; | |
| 135 return a; | |
| 136 } | |
| 137 // max(-0.0, 0) must return 0. | |
| 138 if (b == 0 && a.isNegative) return b; | |
| 139 return a; | |
| 140 } | |
| 141 | 89 |
| 142 /** | 90 /** |
| 143 * A variant of [atan]. | 91 * A variant of [atan]. |
| 144 * | 92 * |
| 145 * Converts both arguments to [double]s. | 93 * Converts both arguments to [double]s. |
| 146 * | 94 * |
| 147 * Returns the angle in radians between the positive x-axis | 95 * Returns the angle in radians between the positive x-axis |
| 148 * and the vector ([b],[a]). | 96 * and the vector ([b],[a]). |
| 149 * The result is in the range -PI..PI. | 97 * The result is in the range -PI..PI. |
| 150 * | 98 * |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 */ | 209 */ |
| 262 external double exp(num x); | 210 external double exp(num x); |
| 263 | 211 |
| 264 /** | 212 /** |
| 265 * Converts [x] to a [double] and returns the natural logarithm of the value. | 213 * Converts [x] to a [double] and returns the natural logarithm of the value. |
| 266 * | 214 * |
| 267 * Returns negative infinity if [x] is equal to zero. | 215 * Returns negative infinity if [x] is equal to zero. |
| 268 * Returns NaN if [x] is NaN or less than zero. | 216 * Returns NaN if [x] is NaN or less than zero. |
| 269 */ | 217 */ |
| 270 external double log(num x); | 218 external double log(num x); |
| OLD | NEW |