| 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 "random.dart"; | 10 part "random.dart"; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 /** | 54 /** |
| 55 * Returns the lesser of two numbers. | 55 * Returns the lesser of two numbers. |
| 56 * | 56 * |
| 57 * Returns NaN if either argument is NaN. | 57 * Returns NaN if either argument is NaN. |
| 58 * The lesser of [:-0.0:] and [:0.0:] is [:-0.0:]. | 58 * The lesser of [:-0.0:] and [:0.0:] is [:-0.0:]. |
| 59 * If the arguments are otherwise equal (including int and doubles with the | 59 * If the arguments are otherwise equal (including int and doubles with the |
| 60 * same mathematical value) then it is unspecified which of the two arguments | 60 * same mathematical value) then it is unspecified which of the two arguments |
| 61 * is returned. | 61 * is returned. |
| 62 */ | 62 */ |
| 63 num min(num a, num b) { | 63 num min(num a, num b) { |
| 64 // These partially redundant type checks improve code quality for dart2js. | |
| 65 // Most of the improvement is at call sites from the inferred non-null num | |
| 66 // return type. | |
| 67 if (a is! num) throw new ArgumentError(a); | |
| 68 if (b is! num) throw new ArgumentError(b); | |
| 69 | |
| 70 if (a > b) return b; | 64 if (a > b) return b; |
| 71 if (a < b) return a; | 65 if (a < b) return a; |
| 72 if (b is double) { | 66 if (b is double) { |
| 73 // Special case for NaN and -0.0. If one argument is NaN return NaN. | 67 // Special case for NaN and -0.0. If one argument is NaN return NaN. |
| 74 // [min] must also distinguish between -0.0 and 0.0. | 68 // [min] must also distinguish between -0.0 and 0.0. |
| 75 if (a is double) { | 69 if (a is double) { |
| 76 if (a == 0.0) { | 70 if (a == 0.0) { |
| 77 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN. | 71 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN. |
| 78 // The following returns -0.0 if either a or b is -0.0, and it | 72 // The following returns -0.0 if either a or b is -0.0, and it |
| 79 // returns NaN if b is NaN. | 73 // returns NaN if b is NaN. |
| 80 return (a + b) * a * b; | 74 return (a + b) * a * b; |
| 81 } | 75 } |
| 82 } | 76 } |
| 83 // Check for NaN and b == -0.0. | 77 // Check for NaN and b == -0.0. |
| 84 if (a == 0 && b.isNegative || b.isNaN) return b; | 78 if (a == 0 && b.isNegative || b.isNaN) return b; |
| 85 return a; | 79 return a; |
| 86 } | 80 } |
| 87 return a; | 81 return a; |
| 88 } | 82 } |
| 89 | 83 |
| 90 /** | 84 /** |
| 91 * Returns the larger of two numbers. | 85 * Returns the larger of two numbers. |
| 92 * | 86 * |
| 93 * Returns NaN if either argument is NaN. | 87 * Returns NaN if either argument is NaN. |
| 94 * The larger of [:-0.0:] and [:0.0:] is [:0.0:]. If the arguments are | 88 * The larger of [:-0.0:] and [:0.0:] is [:0.0:]. If the arguments are |
| 95 * otherwise equal (including int and doubles with the same mathematical value) | 89 * otherwise equal (including int and doubles with the same mathematical value) |
| 96 * then it is unspecified which of the two arguments is returned. | 90 * then it is unspecified which of the two arguments is returned. |
| 97 */ | 91 */ |
| 98 num max(num a, num b) { | 92 num max(num a, num b) { |
| 99 // These partially redundant type checks improve code quality for dart2js. | |
| 100 // Most of the improvement is at call sites from the inferred non-null num | |
| 101 // return type. | |
| 102 if (a is! num) throw new ArgumentError(a); | |
| 103 if (b is! num) throw new ArgumentError(b); | |
| 104 | |
| 105 if (a > b) return a; | 93 if (a > b) return a; |
| 106 if (a < b) return b; | 94 if (a < b) return b; |
| 107 if (b is double) { | 95 if (b is double) { |
| 108 // Special case for NaN and -0.0. If one argument is NaN return NaN. | 96 // Special case for NaN and -0.0. If one argument is NaN return NaN. |
| 109 // [max] must also distinguish between -0.0 and 0.0. | 97 // [max] must also distinguish between -0.0 and 0.0. |
| 110 if (a is double) { | 98 if (a is double) { |
| 111 if (a == 0.0) { | 99 if (a == 0.0) { |
| 112 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN. | 100 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN. |
| 113 // The following returns 0.0 if either a or b is 0.0, and it | 101 // The following returns 0.0 if either a or b is 0.0, and it |
| 114 // returns NaN if b is NaN. | 102 // returns NaN if b is NaN. |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 * Returns NaN if [x] is NaN. | 229 * Returns NaN if [x] is NaN. |
| 242 */ | 230 */ |
| 243 external double exp(num x); | 231 external double exp(num x); |
| 244 | 232 |
| 245 /** | 233 /** |
| 246 * Converts [x] to a double and returns the natural logarithm of the value. | 234 * Converts [x] to a double and returns the natural logarithm of the value. |
| 247 * Returns negative infinity if [x] is equal to zero. | 235 * Returns negative infinity if [x] is equal to zero. |
| 248 * Returns NaN if [x] is NaN or less than zero. | 236 * Returns NaN if [x] is NaN or less than zero. |
| 249 */ | 237 */ |
| 250 external double log(num x); | 238 external double log(num x); |
| OLD | NEW |