OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class Double implements double { | 5 class Double implements double { |
6 factory Double.fromInteger(int value) | 6 factory Double.fromInteger(int value) |
7 native "Double_doubleFromInteger"; | 7 native "Double_doubleFromInteger"; |
8 int hashCode() { | 8 int hashCode() { |
9 try { | 9 try { |
10 return toInt(); | 10 return toInt(); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 } | 96 } |
97 bool isOdd() { | 97 bool isOdd() { |
98 // TODO(floitsch): find more efficient way to implement Double.isOdd. | 98 // TODO(floitsch): find more efficient way to implement Double.isOdd. |
99 return this % 2.0 == 1.0; | 99 return this % 2.0 == 1.0; |
100 } | 100 } |
101 bool isNegative() native "Double_isNegative"; | 101 bool isNegative() native "Double_isNegative"; |
102 bool isInfinite() native "Double_isInfinite"; | 102 bool isInfinite() native "Double_isInfinite"; |
103 bool isNaN() native "Double_isNaN"; | 103 bool isNaN() native "Double_isNaN"; |
104 | 104 |
105 double abs() { | 105 double abs() { |
| 106 // Handle negative 0.0. |
| 107 if (this == 0.0) return 0.0; |
106 return this < 0.0 ? -this : this; | 108 return this < 0.0 ? -this : this; |
107 } | 109 } |
108 | 110 |
109 double round() native "Double_round"; | 111 double round() native "Double_round"; |
110 double floor() native "Double_floor"; | 112 double floor() native "Double_floor"; |
111 double ceil () native "Double_ceil"; | 113 double ceil () native "Double_ceil"; |
112 double truncate() native "Double_truncate"; | 114 double truncate() native "Double_truncate"; |
113 int toInt() native "Double_toInt"; | 115 int toInt() native "Double_toInt"; |
114 double toDouble() { return this; } | 116 double toDouble() { return this; } |
115 | 117 |
(...skipping 16 matching lines...) Expand all Loading... |
132 | 134 |
133 // Step 4. | 135 // Step 4. |
134 if (x.isNaN()) { | 136 if (x.isNaN()) { |
135 return "NaN"; | 137 return "NaN"; |
136 } | 138 } |
137 | 139 |
138 // Step 5. | 140 // Step 5. |
139 String s = ""; | 141 String s = ""; |
140 | 142 |
141 // Step 6. | 143 // Step 6. |
142 if (x.isNegative()) { | 144 if (x.isNegative() && x != 0.0) { |
143 s = "-"; | 145 s = "-"; |
144 x = -x; | 146 x = -x; |
145 } | 147 } |
146 | 148 |
147 // Step 7. | 149 // Step 7. |
148 String m; | 150 String m; |
149 if (x > 10e21) { | 151 if (x > 10e21) { |
150 m = x.toString(); | 152 m = x.toString(); |
151 } else { | 153 } else { |
152 // Step 8. | 154 // Step 8. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 } | 187 } |
186 String toStringAsExponential(int fractionDigits) { | 188 String toStringAsExponential(int fractionDigits) { |
187 throw "Double.toStringAsExponential unimplemented."; | 189 throw "Double.toStringAsExponential unimplemented."; |
188 } | 190 } |
189 String toStringAsPrecision(int precision) { | 191 String toStringAsPrecision(int precision) { |
190 throw "Double.toStringAsPrecision unimplemented."; | 192 throw "Double.toStringAsPrecision unimplemented."; |
191 } | 193 } |
192 String toRadixString(int radix) { | 194 String toRadixString(int radix) { |
193 throw "Double.toRadixString unimplemented."; | 195 throw "Double.toRadixString unimplemented."; |
194 } | 196 } |
| 197 |
| 198 // Order is: NaN > Infinity > ... > 0.0 > -0.0 > ... > -Infinity. |
195 int compareTo(Comparable other) { | 199 int compareTo(Comparable other) { |
196 if (this == other) return 0; | 200 final int EQUAL = 0, LESS = -1, GREATER = 1; |
197 if (this < other) return -1; | 201 if (this < other) { |
198 return 1; | 202 return LESS; |
| 203 } else if (this > other) { |
| 204 return GREATER; |
| 205 } else if (this == other) { |
| 206 if (this == 0.0) { |
| 207 bool thisIsNegative = isNegative(); |
| 208 bool otherIsNegative = other.isNegative(); |
| 209 if (thisIsNegative == otherIsNegative) { |
| 210 return EQUAL; |
| 211 } |
| 212 return thisIsNegative ? LESS : GREATER; |
| 213 } else { |
| 214 return EQUAL; |
| 215 } |
| 216 } else if (isNaN()) { |
| 217 return other.isNaN() ? EQUAL : GREATER; |
| 218 } else { |
| 219 // Other is NaN. |
| 220 return LESS; |
| 221 } |
199 } | 222 } |
200 } | 223 } |
OLD | NEW |