Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: compiler/lib/implementation/number.dart

Issue 8317008: Implement compareTo on DartC. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/corelib/corelib.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 (which is a JS Number object), but get the
37 // doesn't take NaNs and -0.0 into account. 38 // primitive JS number by invoking toDouble().
38 // And it doesn't return an int... 39 num thisValue = toDouble();
39 return this - other; 40 // Remember that NaN return false for any comparison.
41 if (thisValue < other) {
42 return -1;
43 } else if (thisValue > other) {
44 return 1;
45 } else if (thisValue == other) {
46 if (thisValue == 0) {
47 bool thisIsNegative = isNegative();
48 bool otherIsNegative = other.isNegative();
49 if (thisIsNegative == otherIsNegative) return 0;
50 if (thisIsNegative) return -1;
51 return 1;
52 }
53 return 0;
54 } else if (isNaN()) {
55 if (other.isNaN()) {
56 return 0;
57 }
58 return 1;
59 } else {
60 return -1;
61 }
40 } 62 }
41 63
42 bool isNegative() native; 64 bool isNegative() native;
43 bool isEven() native; 65 bool isEven() native;
44 bool isOdd() native; 66 bool isOdd() native;
45 bool isNaN() native; 67 bool isNaN() native;
46 bool isInfinite() native; 68 bool isInfinite() native;
47 69
48 int toInt() { 70 int toInt() {
49 if (isNaN()) throw new BadNumberFormatException("NaN"); 71 if (isNaN()) throw new BadNumberFormatException("NaN");
50 if (isInfinite()) throw new BadNumberFormatException("Infinity"); 72 if (isInfinite()) throw new BadNumberFormatException("Infinity");
51 NumberImplementation truncated = truncate(); 73 NumberImplementation truncated = truncate();
52 // If truncated is -0.0 return +0. The test will also trigger for positive 74 // If truncated is -0.0 return +0. The test will also trigger for positive
53 // 0s but that's not a problem. 75 // 0s but that's not a problem.
54 if (truncated == -0.0) return 0; 76 if (truncated == -0.0) return 0;
55 return truncated; 77 return truncated;
56 } 78 }
57 79
58 NumberImplementation toDouble() native; 80 NumberImplementation toDouble() native;
59 String toString() native; 81 String toString() native;
60 String toStringAsFixed(int fractionDigits) native; 82 String toStringAsFixed(int fractionDigits) native;
61 String toStringAsExponential(int fractionDigits) native; 83 String toStringAsExponential(int fractionDigits) native;
62 String toStringAsPrecision(int precision) native; 84 String toStringAsPrecision(int precision) native;
63 String toRadixString(int radix) native; 85 String toRadixString(int radix) native;
64 86
65 int hashCode() native; 87 int hashCode() native;
66 get dynamic() { return toDouble(); } 88 get dynamic() { return toDouble(); }
67 } 89 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/corelib.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698