OLD | NEW |
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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 bool operator ==(other) { | 82 bool operator ==(other) { |
83 if (other is num) { | 83 if (other is num) { |
84 return other._equalToInteger(this); | 84 return other._equalToInteger(this); |
85 } | 85 } |
86 return false; | 86 return false; |
87 } | 87 } |
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 get isEven => ((this & 1) == 0); |
93 bool isOdd() { return !isEven(); } | 93 bool get isOdd => !isEven; |
94 bool isNaN() { return false; } | 94 bool get isNaN => false; |
95 bool isNegative() { return this < 0; } | 95 bool get isNegative => this < 0; |
96 bool isInfinite() { return false; } | 96 bool get isInfinite => false; |
97 | 97 |
98 int compareTo(num 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) { | 100 if (other is double) { |
101 // TODO(floitsch): the following locals should be 'const'. | 101 // TODO(floitsch): the following locals should be 'const'. |
102 int MAX_EXACT_INT_TO_DOUBLE = 9007199254740992; // 2^53. | 102 int MAX_EXACT_INT_TO_DOUBLE = 9007199254740992; // 2^53. |
103 int MIN_EXACT_INT_TO_DOUBLE = -MAX_EXACT_INT_TO_DOUBLE; | 103 int MIN_EXACT_INT_TO_DOUBLE = -MAX_EXACT_INT_TO_DOUBLE; |
104 double d = other; | 104 double d = other; |
105 if (d.isInfinite()) { | 105 if (d.isInfinite) { |
106 return d == double.NEGATIVE_INFINITY ? GREATER : LESS; | 106 return d == double.NEGATIVE_INFINITY ? GREATER : LESS; |
107 } | 107 } |
108 if (d.isNaN()) { | 108 if (d.isNaN) { |
109 return LESS; | 109 return LESS; |
110 } | 110 } |
111 if (MIN_EXACT_INT_TO_DOUBLE <= this && this <= MAX_EXACT_INT_TO_DOUBLE) { | 111 if (MIN_EXACT_INT_TO_DOUBLE <= this && this <= MAX_EXACT_INT_TO_DOUBLE) { |
112 // Let the double implementation deal with -0.0. | 112 // Let the double implementation deal with -0.0. |
113 return -(d.compareTo(this.toDouble())); | 113 return -(d.compareTo(this.toDouble())); |
114 } else { | 114 } else { |
115 // If abs(other) > MAX_EXACT_INT_TO_DOUBLE, then other has an integer | 115 // If abs(other) > MAX_EXACT_INT_TO_DOUBLE, then other has an integer |
116 // value (no bits below the decimal point). | 116 // value (no bits below the decimal point). |
117 other = d.toInt(); | 117 other = d.toInt(); |
118 } | 118 } |
(...skipping 10 matching lines...) Expand all Loading... |
129 int round() { return this; } | 129 int round() { return this; } |
130 int floor() { return this; } | 130 int floor() { return this; } |
131 int ceil() { return this; } | 131 int ceil() { return this; } |
132 int truncate() { return this; } | 132 int truncate() { return this; } |
133 | 133 |
134 int toInt() { return this; } | 134 int toInt() { return this; } |
135 double toDouble() { return new _Double.fromInteger(this); } | 135 double toDouble() { return new _Double.fromInteger(this); } |
136 | 136 |
137 int pow(int exponent) { | 137 int pow(int exponent) { |
138 double res = this.toDouble().pow(exponent); | 138 double res = this.toDouble().pow(exponent); |
139 if (res.isInfinite()) { | 139 if (res.isInfinite) { |
140 // Use Bigint instead. | 140 // Use Bigint instead. |
141 throw "_IntegerImplementation.pow not implemented for large integers."; | 141 throw "_IntegerImplementation.pow not implemented for large integers."; |
142 } | 142 } |
143 return res.toInt(); | 143 return res.toInt(); |
144 } | 144 } |
145 | 145 |
146 String toStringAsFixed(int fractionDigits) { | 146 String toStringAsFixed(int fractionDigits) { |
147 return this.toDouble().toStringAsFixed(fractionDigits); | 147 return this.toDouble().toStringAsFixed(fractionDigits); |
148 } | 148 } |
149 String toStringAsExponential(int fractionDigits) { | 149 String toStringAsExponential(int fractionDigits) { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 } | 238 } |
239 } | 239 } |
240 int _shlFromInt(int other) { | 240 int _shlFromInt(int other) { |
241 throw const OutOfMemoryError(); | 241 throw const OutOfMemoryError(); |
242 } | 242 } |
243 | 243 |
244 int pow(int exponent) { | 244 int pow(int exponent) { |
245 throw "Bigint.pow not implemented"; | 245 throw "Bigint.pow not implemented"; |
246 } | 246 } |
247 } | 247 } |
OLD | NEW |