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

Unified Diff: runtime/lib/integers.dart

Issue 10692099: Fix int.compareTo. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years, 5 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/compare_to2_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/integers.dart
diff --git a/runtime/lib/integers.dart b/runtime/lib/integers.dart
index c1a79c8f843e7f9a4c9e596090f43dd603d19196..f9f2014628973005f203ff61cb26d8d8d0770403 100644
--- a/runtime/lib/integers.dart
+++ b/runtime/lib/integers.dart
@@ -95,17 +95,34 @@ class IntegerImplementation {
bool isNegative() { return this < 0; }
bool isInfinite() { return false; }
- int compareTo(Comparable other) {
+ int compareTo(num other) {
final int EQUAL = 0, LESS = -1, GREATER = 1;
+ if (other is double) {
+ // TODO(floitsch): the following locals should be 'const'.
+ int MAX_EXACT_INT_TO_DOUBLE = 9007199254740992; // 2^53.
+ int MIN_EXACT_INT_TO_DOUBLE = -MAX_EXACT_INT_TO_DOUBLE;
+ double d = other;
+ if (d.isInfinite()) {
+ return d == double.NEGATIVE_INFINITY ? GREATER : LESS;
+ }
+ if (d.isNaN()) {
+ return LESS;
+ }
+ if (MIN_EXACT_INT_TO_DOUBLE <= this && this <= MAX_EXACT_INT_TO_DOUBLE) {
+ // Let the double implementation deal with -0.0.
+ return -(d.compareTo(this.toDouble()));
+ } else {
+ // If abs(other) > MAX_EXACT_INT_TO_DOUBLE, then other has an integer
+ // value (no bits below the decimal point).
+ other = d.toInt();
+ }
+ }
if (this < other) {
return LESS;
} else if (this > other) {
return GREATER;
- } else if (this == other) {
- return EQUAL;
} else {
- // Other is NaN.
- return LESS;
+ return EQUAL;
}
}
« no previous file with comments | « no previous file | tests/corelib/compare_to2_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698