Chromium Code Reviews| 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 NumberImplementation implements int, double native "Number" { | 5 class NumberImplementation implements int, double native "Number" { |
| 6 NumberImplementation operator +(NumberImplementation other) native; | 6 NumberImplementation operator +(NumberImplementation other) native; |
| 7 NumberImplementation operator -(NumberImplementation other) native; | 7 NumberImplementation operator -(NumberImplementation other) native; |
| 8 NumberImplementation operator *(NumberImplementation other) native; | 8 NumberImplementation operator *(NumberImplementation other) native; |
| 9 NumberImplementation operator /(NumberImplementation other) native; | 9 NumberImplementation operator /(NumberImplementation other) native; |
| 10 NumberImplementation operator ~/(NumberImplementation other) native; | 10 NumberImplementation operator ~/(NumberImplementation other) native; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 | 25 |
| 26 NumberImplementation remainder(num other) native; | 26 NumberImplementation remainder(num other) native; |
| 27 NumberImplementation abs() native; | 27 NumberImplementation abs() native; |
| 28 NumberImplementation round() native; | 28 NumberImplementation round() native; |
| 29 NumberImplementation floor() native; | 29 NumberImplementation floor() native; |
| 30 NumberImplementation ceil() native; | 30 NumberImplementation ceil() native; |
| 31 NumberImplementation truncate() native; | 31 NumberImplementation truncate() native; |
| 32 | 32 |
| 33 // CompareTo has to give a complete order, including -0/+0, NaN and | 33 // CompareTo has to give a complete order, including -0/+0, NaN and |
| 34 // Infinities. | 34 // Infinities. |
| 35 // Order is: -Inf < .. < -0.0 < 0.0 .. < +inf < NaN. | |
| 35 NumberImplementation compareTo(NumberImplementation other) { | 36 NumberImplementation compareTo(NumberImplementation other) { |
| 36 // TODO(5427706): NumberImplementation.compareTo is broken, since it | 37 // Don't use the 'this'-object, but get the native JS number. |
|
ngeoffray
2011/10/17 15:59:07
That's the call to 'toDouble'? Could you be explic
floitsch
2011/10/17 16:06:40
Done.
| |
| 37 // doesn't take NaNs and -0.0 into account. | 38 num thisValue = toDouble(); |
| 38 // And it doesn't return an int... | 39 // Remember that NaN return false for any comparison. |
| 39 return this - other; | 40 if (thisValue < other) { |
| 41 return -1; | |
| 42 } else if (thisValue > other) { | |
| 43 return 1; | |
| 44 } else if (thisValue == other) { | |
| 45 if (thisValue == 0) { | |
| 46 bool thisIsNegative = isNegative(); | |
| 47 bool otherIsNegative = other.isNegative(); | |
| 48 if (thisIsNegative == otherIsNegative) return 0; | |
| 49 if (thisIsNegative) return -1; | |
| 50 return 1; | |
| 51 } | |
| 52 return 0; | |
| 53 } else if (isNaN()) { | |
| 54 if (other.isNaN()) { | |
| 55 return 0; | |
| 56 } | |
| 57 return 1; | |
| 58 } else { | |
| 59 return -1; | |
| 60 } | |
| 40 } | 61 } |
| 41 | 62 |
| 42 bool isNegative() native; | 63 bool isNegative() native; |
| 43 bool isEven() native; | 64 bool isEven() native; |
| 44 bool isOdd() native; | 65 bool isOdd() native; |
| 45 bool isNaN() native; | 66 bool isNaN() native; |
| 46 bool isInfinite() native; | 67 bool isInfinite() native; |
| 47 | 68 |
| 48 int toInt() { | 69 int toInt() { |
| 49 if (isNaN()) throw new BadNumberFormatException("NaN"); | 70 if (isNaN()) throw new BadNumberFormatException("NaN"); |
| 50 if (isInfinite()) throw new BadNumberFormatException("Infinity"); | 71 if (isInfinite()) throw new BadNumberFormatException("Infinity"); |
| 51 NumberImplementation truncated = truncate(); | 72 NumberImplementation truncated = truncate(); |
| 52 // If truncated is -0.0 return +0. The test will also trigger for positive | 73 // If truncated is -0.0 return +0. The test will also trigger for positive |
| 53 // 0s but that's not a problem. | 74 // 0s but that's not a problem. |
| 54 if (truncated == -0.0) return 0; | 75 if (truncated == -0.0) return 0; |
| 55 return truncated; | 76 return truncated; |
| 56 } | 77 } |
| 57 | 78 |
| 58 NumberImplementation toDouble() native; | 79 NumberImplementation toDouble() native; |
| 59 String toString() native; | 80 String toString() native; |
| 60 String toStringAsFixed(int fractionDigits) native; | 81 String toStringAsFixed(int fractionDigits) native; |
| 61 String toStringAsExponential(int fractionDigits) native; | 82 String toStringAsExponential(int fractionDigits) native; |
| 62 String toStringAsPrecision(int precision) native; | 83 String toStringAsPrecision(int precision) native; |
| 63 String toRadixString(int radix) native; | 84 String toRadixString(int radix) native; |
| 64 | 85 |
| 65 int hashCode() native; | 86 int hashCode() native; |
| 66 get dynamic() { return toDouble(); } | 87 get dynamic() { return toDouble(); } |
| 67 } | 88 } |
| OLD | NEW |