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'; |
11 */ | 11 */ |
12 library dart.math; | 12 library dart.math; |
13 | 13 |
14 part "jenkins_smi_hash.dart"; | 14 part "jenkins_smi_hash.dart"; |
15 part "point.dart"; | 15 part "point.dart"; |
16 part "random.dart"; | 16 part "random.dart"; |
17 part "rectangle.dart"; | 17 part "rectangle.dart"; |
18 | 18 |
19 /** | 19 /** |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 /** | 61 /** |
62 * Returns the lesser of two numbers. | 62 * Returns the lesser of two numbers. |
63 * | 63 * |
64 * Returns NaN if either argument is NaN. | 64 * Returns NaN if either argument is NaN. |
65 * The lesser of `-0.0` and `0.0` is `-0.0`. | 65 * The lesser of `-0.0` and `0.0` is `-0.0`. |
66 * If the arguments are otherwise equal (including int and doubles with the | 66 * If the arguments are otherwise equal (including int and doubles with the |
67 * same mathematical value) then it is unspecified which of the two arguments | 67 * same mathematical value) then it is unspecified which of the two arguments |
68 * is returned. | 68 * is returned. |
69 */ | 69 */ |
70 num/*=T*/ min/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) { | 70 external num/*=T*/ min/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b); |
71 // These partially redundant type checks improve code quality for dart2js. | |
72 // Most of the improvement is at call sites from the inferred non-null num | |
73 // return type. | |
74 if (a is! num) throw new ArgumentError(a); | |
75 if (b is! num) throw new ArgumentError(b); | |
76 | |
77 if (a > b) return b; | |
78 if (a < b) return a; | |
79 if (b is double) { | |
80 // Special case for NaN and -0.0. If one argument is NaN return NaN. | |
81 // [min] must also distinguish between -0.0 and 0.0. | |
82 if (a is double) { | |
83 if (a == 0.0) { | |
84 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN. | |
85 // The following returns -0.0 if either a or b is -0.0, and it | |
86 // returns NaN if b is NaN. | |
87 return (a + b) * a * b; | |
88 } | |
89 } | |
90 // Check for NaN and b == -0.0. | |
91 if (a == 0 && b.isNegative || b.isNaN) return b; | |
92 return a; | |
93 } | |
94 return a; | |
95 } | |
96 | 71 |
97 /** | 72 /** |
98 * Returns the larger of two numbers. | 73 * Returns the larger of two numbers. |
99 * | 74 * |
100 * Returns NaN if either argument is NaN. | 75 * Returns NaN if either argument is NaN. |
101 * The larger of `-0.0` and `0.0` is `0.0`. If the arguments are | 76 * The larger of `-0.0` and `0.0` is `0.0`. If the arguments are |
102 * otherwise equal (including int and doubles with the same mathematical value) | 77 * otherwise equal (including int and doubles with the same mathematical value) |
103 * then it is unspecified which of the two arguments is returned. | 78 * then it is unspecified which of the two arguments is returned. |
104 */ | 79 */ |
105 num/*=T*/ max/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b) { | 80 external num/*=T*/ max/*<T extends num>*/(num/*=T*/ a, num/*=T*/ b); |
106 // These partially redundant type checks improve code quality for dart2js. | |
107 // Most of the improvement is at call sites from the inferred non-null num | |
108 // return type. | |
109 if (a is! num) throw new ArgumentError(a); | |
110 if (b is! num) throw new ArgumentError(b); | |
111 | |
112 if (a > b) return a; | |
113 if (a < b) return b; | |
114 if (b is double) { | |
115 // Special case for NaN and -0.0. If one argument is NaN return NaN. | |
116 // [max] must also distinguish between -0.0 and 0.0. | |
117 if (a is double) { | |
118 if (a == 0.0) { | |
119 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN. | |
120 // The following returns 0.0 if either a or b is 0.0, and it | |
121 // returns NaN if b is NaN. | |
122 return a + b; | |
123 } | |
124 } | |
125 // Check for NaN. | |
126 if (b.isNaN) return b; | |
127 return a; | |
128 } | |
129 // max(-0.0, 0) must return 0. | |
130 if (b == 0 && a.isNegative) return b; | |
131 return a; | |
132 } | |
133 | 81 |
134 /** | 82 /** |
135 * A variant of [atan]. | 83 * A variant of [atan]. |
136 * | 84 * |
137 * Converts both arguments to doubles. | 85 * Converts both arguments to doubles. |
138 * | 86 * |
139 * Returns the angle between the positive x-axis and the vector ([b],[a]). | 87 * Returns the angle between the positive x-axis and the vector ([b],[a]). |
140 * The result, in radians, is in the range -PI..PI. | 88 * The result, in radians, is in the range -PI..PI. |
141 * | 89 * |
142 * If [b] is positive, this is the same as `atan(b/a)`. | 90 * If [b] is positive, this is the same as `atan(b/a)`. |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 */ | 200 */ |
253 external double exp(num x); | 201 external double exp(num x); |
254 | 202 |
255 /** | 203 /** |
256 * Converts [x] to a double and returns the natural logarithm of the value. | 204 * Converts [x] to a double and returns the natural logarithm of the value. |
257 * | 205 * |
258 * Returns negative infinity if [x] is equal to zero. | 206 * Returns negative infinity if [x] is equal to zero. |
259 * Returns NaN if [x] is NaN or less than zero. | 207 * Returns NaN if [x] is NaN or less than zero. |
260 */ | 208 */ |
261 external double log(num x); | 209 external double log(num x); |
OLD | NEW |