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 library dart.math; | 5 library dart.math; |
6 | 6 |
7 part "random.dart"; | 7 part "random.dart"; |
8 | 8 |
9 /** | 9 /** |
10 * Base of the natural logarithms. | 10 * Base of the natural logarithms. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 /** | 51 /** |
52 * Returns the lesser of two numbers. | 52 * Returns the lesser of two numbers. |
53 * | 53 * |
54 * Returns NaN if either argument is NaN. | 54 * Returns NaN if either argument is NaN. |
55 * The lesser of [:-0.0:] and [:0.0:] is [:-0.0:]. | 55 * The lesser of [:-0.0:] and [:0.0:] is [:-0.0:]. |
56 * If the arguments are otherwise equal (including int and doubles with the | 56 * If the arguments are otherwise equal (including int and doubles with the |
57 * same mathematical value) then it is unspecified which of the two arguments | 57 * same mathematical value) then it is unspecified which of the two arguments |
58 * is returned. | 58 * is returned. |
59 */ | 59 */ |
60 num min(num a, num b) { | 60 num min(num a, num b) { |
61 if (a is num) { | 61 if (a > b) return b; |
62 // TODO(floitsch): merge this if into the previous one, once dart2js | 62 if (a < b) return a; |
63 // correctly propagates types for logical ands. | 63 if (b is double) { |
64 if (b is num) { | 64 // Special case for NaN and -0.0. If one argument is NaN return NaN. |
65 if (a > b) return b; | 65 // [min] must also distinguish between -0.0 and 0.0. |
66 if (a < b) return a; | 66 if (a is double) { |
67 if (b is double) { | 67 if (a == 0.0) { |
68 // Special case for NaN and -0.0. If one argument is NaN return NaN. | 68 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN. |
69 // [min] must also distinguish between -0.0 and 0.0. | 69 // The following returns -0.0 if either a or b is -0.0, and it |
70 if (a is double) { | 70 // returns NaN if b is NaN. |
71 if (a == 0.0) { | 71 return (a + b) * a * b; |
72 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN. | |
73 // The following returns -0.0 if either a or b is -0.0, and it | |
74 // returns NaN if b is NaN. | |
75 return (a + b) * a * b; | |
76 } | |
77 } | |
78 // Check for NaN and b == -0.0. | |
79 if (a == 0 && b.isNegative || b.isNaN) return b; | |
80 return a; | |
81 } | 72 } |
82 return a; | |
83 } | 73 } |
84 throw new ArgumentError(b); | 74 // Check for NaN and b == -0.0. |
| 75 if (a == 0 && b.isNegative || b.isNaN) return b; |
| 76 return a; |
85 } | 77 } |
86 throw new ArgumentError(a); | 78 return a; |
87 } | 79 } |
88 | 80 |
89 /** | 81 /** |
90 * Returns the larger of two numbers. | 82 * Returns the larger of two numbers. |
91 * | 83 * |
92 * Returns NaN if either argument is NaN. | 84 * Returns NaN if either argument is NaN. |
93 * The larger of [:-0.0:] and [:0.0:] is [:0.0:]. If the arguments are | 85 * The larger of [:-0.0:] and [:0.0:] is [:0.0:]. If the arguments are |
94 * otherwise equal (including int and doubles with the same mathematical value) | 86 * otherwise equal (including int and doubles with the same mathematical value) |
95 * then it is unspecified which of the two arguments is returned. | 87 * then it is unspecified which of the two arguments is returned. |
96 */ | 88 */ |
97 num max(num a, num b) { | 89 num max(num a, num b) { |
98 if (a is num) { | 90 if (a > b) return a; |
99 // TODO(floitsch): merge this if into the previous one, once dart2js | 91 if (a < b) return b; |
100 // correctly propagates types for logical ands. | 92 if (b is double) { |
101 if (b is num) { | 93 // Special case for NaN and -0.0. If one argument is NaN return NaN. |
102 if (a > b) return a; | 94 // [max] must also distinguish between -0.0 and 0.0. |
103 if (a < b) return b; | 95 if (a is double) { |
104 if (b is double) { | 96 if (a == 0.0) { |
105 // Special case for NaN and -0.0. If one argument is NaN return NaN. | 97 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN. |
106 // [max] must also distinguish between -0.0 and 0.0. | 98 // The following returns 0.0 if either a or b is 0.0, and it |
107 if (a is double) { | 99 // returns NaN if b is NaN. |
108 if (a == 0.0) { | 100 return a + b; |
109 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN. | |
110 // The following returns 0.0 if either a or b is 0.0, and it | |
111 // returns NaN if b is NaN. | |
112 return a + b; | |
113 } | |
114 } | |
115 // Check for NaN. | |
116 if (b.isNaN) return b; | |
117 return a; | |
118 } | 101 } |
119 // max(-0.0, 0) must return 0. | |
120 if (b == 0 && a.isNegative) return b; | |
121 return a; | |
122 } | 102 } |
123 throw new ArgumentError(b); | 103 // Check for NaN. |
| 104 if (b.isNaN) return b; |
| 105 return a; |
124 } | 106 } |
125 throw new ArgumentError(a); | 107 // max(-0.0, 0) must return 0. |
| 108 if (b == 0 && a.isNegative) return b; |
| 109 return a; |
126 } | 110 } |
127 | 111 |
128 /** | 112 /** |
129 * A variant of [atan]. | 113 * A variant of [atan]. |
130 * | 114 * |
131 * Converts both arguments to doubles. | 115 * Converts both arguments to doubles. |
132 * | 116 * |
133 * Returns the angle between the positive x-axis and the vector ([b],[a]). | 117 * Returns the angle between the positive x-axis and the vector ([b],[a]). |
134 * The result, in radians, is in the range -PI..PI. | 118 * The result, in radians, is in the range -PI..PI. |
135 * | 119 * |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 * Returns NaN if [x] is NaN. | 198 * Returns NaN if [x] is NaN. |
215 */ | 199 */ |
216 external double exp(num x); | 200 external double exp(num x); |
217 | 201 |
218 /** | 202 /** |
219 * Converts [x] to a double and returns the natural logarithm of the value. | 203 * Converts [x] to a double and returns the natural logarithm of the value. |
220 * Returns negative infinity if [x] is equal to zero. | 204 * Returns negative infinity if [x] is equal to zero. |
221 * Returns NaN if [x] is NaN or less than zero. | 205 * Returns NaN if [x] is NaN or less than zero. |
222 */ | 206 */ |
223 external double log(num x); | 207 external double log(num x); |
OLD | NEW |