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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/corelib/corelib.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiler/lib/implementation/number.dart
diff --git a/compiler/lib/implementation/number.dart b/compiler/lib/implementation/number.dart
index 9dc210acee82c19581a598ac04d9e929f6064f70..e30e394634ce95d7c8034afce02917bc27e68624 100644
--- a/compiler/lib/implementation/number.dart
+++ b/compiler/lib/implementation/number.dart
@@ -32,11 +32,33 @@ class NumberImplementation implements int, double native "Number" {
// CompareTo has to give a complete order, including -0/+0, NaN and
// Infinities.
+ // Order is: -Inf < .. < -0.0 < 0.0 .. < +inf < NaN.
NumberImplementation compareTo(NumberImplementation other) {
- // TODO(5427706): NumberImplementation.compareTo is broken, since it
- // doesn't take NaNs and -0.0 into account.
- // And it doesn't return an int...
- return this - other;
+ // Don't use the 'this' object (which is a JS Number object), but get the
+ // primitive JS number by invoking toDouble().
+ num thisValue = toDouble();
+ // Remember that NaN return false for any comparison.
+ if (thisValue < other) {
+ return -1;
+ } else if (thisValue > other) {
+ return 1;
+ } else if (thisValue == other) {
+ if (thisValue == 0) {
+ bool thisIsNegative = isNegative();
+ bool otherIsNegative = other.isNegative();
+ if (thisIsNegative == otherIsNegative) return 0;
+ if (thisIsNegative) return -1;
+ return 1;
+ }
+ return 0;
+ } else if (isNaN()) {
+ if (other.isNaN()) {
+ return 0;
+ }
+ return 1;
+ } else {
+ return -1;
+ }
}
bool isNegative() native;
« 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