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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/corelib/compare_to2_test.dart » ('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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // TODO(srdjan): fix limitations. 5 // TODO(srdjan): fix limitations.
6 // - shift amount must be a Smi. 6 // - shift amount must be a Smi.
7 class IntegerImplementation { 7 class IntegerImplementation {
8 factory IntegerImplementation._uninstantiable() { 8 factory IntegerImplementation._uninstantiable() {
9 throw const UnsupportedOperationException( 9 throw const UnsupportedOperationException(
10 "IntegerImplementation can only be allocated by the VM"); 10 "IntegerImplementation can only be allocated by the VM");
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 bool equalToInteger(int other) native "Integer_equalToInteger"; 88 bool equalToInteger(int other) native "Integer_equalToInteger";
89 int abs() { 89 int abs() {
90 return this < 0 ? -this : this; 90 return this < 0 ? -this : this;
91 } 91 }
92 bool isEven() { return ((this & 1) === 0); } 92 bool isEven() { return ((this & 1) === 0); }
93 bool isOdd() { return !isEven(); } 93 bool isOdd() { return !isEven(); }
94 bool isNaN() { return false; } 94 bool isNaN() { return false; }
95 bool isNegative() { return this < 0; } 95 bool isNegative() { return this < 0; }
96 bool isInfinite() { return false; } 96 bool isInfinite() { return false; }
97 97
98 int compareTo(Comparable other) { 98 int compareTo(num other) {
99 final int EQUAL = 0, LESS = -1, GREATER = 1; 99 final int EQUAL = 0, LESS = -1, GREATER = 1;
100 if (other is double) {
101 // TODO(floitsch): the following locals should be 'const'.
102 int MAX_EXACT_INT_TO_DOUBLE = 9007199254740992; // 2^53.
103 int MIN_EXACT_INT_TO_DOUBLE = -MAX_EXACT_INT_TO_DOUBLE;
104 double d = other;
105 if (d.isInfinite()) {
106 return d == double.NEGATIVE_INFINITY ? GREATER : LESS;
107 }
108 if (d.isNaN()) {
109 return LESS;
110 }
111 if (MIN_EXACT_INT_TO_DOUBLE <= this && this <= MAX_EXACT_INT_TO_DOUBLE) {
112 // Let the double implementation deal with -0.0.
113 return -(d.compareTo(this.toDouble()));
114 } else {
115 // If abs(other) > MAX_EXACT_INT_TO_DOUBLE, then other has an integer
116 // value (no bits below the decimal point).
117 other = d.toInt();
118 }
119 }
100 if (this < other) { 120 if (this < other) {
101 return LESS; 121 return LESS;
102 } else if (this > other) { 122 } else if (this > other) {
103 return GREATER; 123 return GREATER;
104 } else if (this == other) { 124 } else {
105 return EQUAL; 125 return EQUAL;
106 } else {
107 // Other is NaN.
108 return LESS;
109 } 126 }
110 } 127 }
111 128
112 int round() { return this; } 129 int round() { return this; }
113 int floor() { return this; } 130 int floor() { return this; }
114 int ceil() { return this; } 131 int ceil() { return this; }
115 int truncate() { return this; } 132 int truncate() { return this; }
116 133
117 int toInt() { return this; } 134 int toInt() { return this; }
118 double toDouble() { return new Double.fromInteger(this); } 135 double toDouble() { return new Double.fromInteger(this); }
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 } 236 }
220 } 237 }
221 int shlFromInt(int other) { 238 int shlFromInt(int other) {
222 throw const OutOfMemoryException(); 239 throw const OutOfMemoryException();
223 } 240 }
224 241
225 int pow(int exponent) { 242 int pow(int exponent) {
226 throw "Bigint.pow not implemented"; 243 throw "Bigint.pow not implemented";
227 } 244 }
228 } 245 }
OLDNEW
« 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