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 // A part of the dart:math library. | 5 // A part of the dart:math library. |
6 | 6 |
7 /** | 7 /** |
8 * Base of the natural logarithms. | 8 * Base of the natural logarithms. |
9 */ | 9 */ |
10 final double E = 2.718281828459045; | 10 final double E = 2.718281828459045; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 */ | 51 */ |
52 external int parseInt(String str); | 52 external int parseInt(String str); |
53 | 53 |
54 /** | 54 /** |
55 * Parses a [String] representation of a [double], and returns | 55 * Parses a [String] representation of a [double], and returns |
56 * a [double]. Throws a [BadNumberFormatException] if [str] cannot | 56 * a [double]. Throws a [BadNumberFormatException] if [str] cannot |
57 * be parsed as a [double]. | 57 * be parsed as a [double]. |
58 */ | 58 */ |
59 external double parseDouble(String str); | 59 external double parseDouble(String str); |
60 | 60 |
| 61 /** |
| 62 * Returns the minimum of two numbers. If either argument is NaN returns NaN. |
| 63 * The minimum of [:-0.0:] and [:0.0:] is [:-0.0:]. If both arguments are |
| 64 * equal (int and doubles with the same mathematical value are equal) then |
| 65 * it is unspecified which of the two arguments is returned. |
| 66 */ |
61 num min(num a, num b) { | 67 num min(num a, num b) { |
62 int c = a.compareTo(b); | 68 if (a is num) { |
63 if (c == 0) return a; | 69 // TODO(floitsch): merge this if into the previous one, once dart2js |
64 if (c < 0) { | 70 // correctly propagates types for logical ands. |
65 if ((b is double) && b.isNaN()) return b; | 71 if (b is num) { |
66 return a; | 72 if (a > b) return b; |
| 73 if (a < b) return a; |
| 74 if (b is double) { |
| 75 // Special case for NaN and -0.0. If one argument is NaN return NaN. |
| 76 // [min] must also distinguish between -0.0 and 0.0. |
| 77 if (a is double) { |
| 78 if (a == 0.0) { |
| 79 // a is either 0.0 or -0.0. b is either 0.0, -0.0 or NaN. |
| 80 // The following returns -0.0 if either a or b is -0.0, and it |
| 81 // returns NaN if b is NaN. |
| 82 return (a + b) * a * b; |
| 83 } |
| 84 } |
| 85 // Check for NaN and b == -0.0. |
| 86 if (a == 0 && b.isNegative() || b.isNaN()) return b; |
| 87 return a; |
| 88 } |
| 89 return a; |
| 90 } |
| 91 throw new IllegalArgumentException(b); |
67 } | 92 } |
68 if ((a is double) && a.isNaN()) return a; | 93 throw new IllegalArgumentException(a); |
69 return b; | |
70 } | |
71 | |
72 num max(num a, num b) { | |
73 // NaNs are handled correctly since the compareTo function always considers | |
74 // them to be bigger than any other operand. | |
75 return (a.compareTo(b) < 0) ? b : a; | |
76 } | 94 } |
77 | 95 |
78 /** | 96 /** |
| 97 * Returns the maximum of two numbers. If either argument is NaN returns NaN. |
| 98 * The maximum of [:-0.0:] and [:0.0:] is [:0.0:]. If both arguments are |
| 99 * equal (int and doubles with the same mathematical value are equal) then |
| 100 * it is unspecified which of the two arguments is returned. |
| 101 */ |
| 102 num max(num a, num b) { |
| 103 if (a is num) { |
| 104 // TODO(floitsch): merge this if into the previous one, once dart2js |
| 105 // correctly propagates types for logical ands. |
| 106 if (b is num) { |
| 107 if (a > b) return a; |
| 108 if (a < b) return b; |
| 109 if (b is double) { |
| 110 // Special case for NaN and -0.0. If one argument is NaN return NaN. |
| 111 // [max] must also distinguish between -0.0 and 0.0. |
| 112 if (a is double) { |
| 113 if (a == 0.0) { |
| 114 // a is either 0.0 or -0.0. b is either 0.0, -0.0, or NaN. |
| 115 // The following returns 0.0 if either a or b is 0.0, and it |
| 116 // returns NaN if b is NaN. |
| 117 return a + b; |
| 118 } |
| 119 } |
| 120 // Check for NaN. |
| 121 if (b.isNaN()) return b; |
| 122 return a; |
| 123 } |
| 124 // max(-0.0, 0) must return 0. |
| 125 if (b == 0 && a.isNegative()) return b; |
| 126 return a; |
| 127 } |
| 128 throw new IllegalArgumentException(b); |
| 129 } |
| 130 throw new IllegalArgumentException(a); |
| 131 } |
| 132 |
| 133 /** |
79 * Returns the arc tangent of [a]/[b] with sign according to quadrant. | 134 * Returns the arc tangent of [a]/[b] with sign according to quadrant. |
80 */ | 135 */ |
81 external double atan2(num a, num b); | 136 external double atan2(num a, num b); |
82 | 137 |
83 /** | 138 /** |
84 * If the [exponent] is an integer the result is of the same type as [x]. | 139 * If the [exponent] is an integer the result is of the same type as [x]. |
85 * Otherwise it is a [double]. | 140 * Otherwise it is a [double]. |
86 */ | 141 */ |
87 external num pow(num x, num exponent); | 142 external num pow(num x, num exponent); |
88 | 143 |
89 external double sin(num x); | 144 external double sin(num x); |
90 external double cos(num x); | 145 external double cos(num x); |
91 external double tan(num x); | 146 external double tan(num x); |
92 external double acos(num x); | 147 external double acos(num x); |
93 external double asin(num x); | 148 external double asin(num x); |
94 external double atan(num x); | 149 external double atan(num x); |
95 external double sqrt(num x); | 150 external double sqrt(num x); |
96 external double exp(num x); | 151 external double exp(num x); |
97 external double log(num x); | 152 external double log(num x); |
OLD | NEW |