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 new UnsupportedError( | 9 throw new UnsupportedError( |
10 "_IntegerImplementation can only be allocated by the VM"); | 10 "_IntegerImplementation can only be allocated by the VM"); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 } else { | 124 } else { |
125 return EQUAL; | 125 return EQUAL; |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
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 num clamp(num lowerLimit, num upperLimit) { |
| 135 if (lowerLimit is! num) throw new ArgumentError(lowerLimit); |
| 136 if (upperLimit is! num) throw new ArgumentError(upperLimit); |
| 137 |
| 138 // Special case for integers. |
| 139 if (lowerLimit is int && upperLimit is int) { |
| 140 if (lowerLimit > upperLimit) { |
| 141 throw new ArgumentError(lowerLimit); |
| 142 } |
| 143 if (this < lowerLimit) return lowerLimit; |
| 144 if (this > upperLimit) return upperLimit; |
| 145 return this; |
| 146 } |
| 147 // Generic case involving doubles. |
| 148 if (lowerLimit.compareTo(upperLimit) > 0) { |
| 149 throw new ArgumentError(lowerLimit); |
| 150 } |
| 151 if (lowerLimit.isNaN) return lowerLimit; |
| 152 // Note that we don't need to care for -0.0 for the lower limit. |
| 153 if (this < lowerLimit) return lowerLimit; |
| 154 if (this.compareTo(upperLimit) > 0) return upperLimit; |
| 155 return this; |
| 156 } |
| 157 |
134 int toInt() { return this; } | 158 int toInt() { return this; } |
135 double toDouble() { return new _Double.fromInteger(this); } | 159 double toDouble() { return new _Double.fromInteger(this); } |
136 | 160 |
137 int pow(int exponent) { | 161 int pow(int exponent) { |
138 double res = this.toDouble().pow(exponent); | 162 double res = this.toDouble().pow(exponent); |
139 if (res.isInfinite) { | 163 if (res.isInfinite) { |
140 // Use Bigint instead. | 164 // Use Bigint instead. |
141 throw "_IntegerImplementation.pow not implemented for large integers."; | 165 throw "_IntegerImplementation.pow not implemented for large integers."; |
142 } | 166 } |
143 return res.toInt(); | 167 return res.toInt(); |
144 } | 168 } |
145 | 169 |
146 String toStringAsFixed(int fractionDigits) { | 170 String toStringAsFixed(int fractionDigits) { |
147 return this.toDouble().toStringAsFixed(fractionDigits); | 171 return this.toDouble().toStringAsFixed(fractionDigits); |
148 } | 172 } |
149 String toStringAsExponential(int fractionDigits) { | 173 String toStringAsExponential([int fractionDigits]) { |
150 return this.toDouble().toStringAsExponential(fractionDigits); | 174 return this.toDouble().toStringAsExponential(fractionDigits); |
151 } | 175 } |
152 String toStringAsPrecision(int precision) { | 176 String toStringAsPrecision(int precision) { |
153 return this.toDouble().toStringAsPrecision(precision); | 177 return this.toDouble().toStringAsPrecision(precision); |
154 } | 178 } |
| 179 |
155 String toRadixString(int radix) { | 180 String toRadixString(int radix) { |
156 final table = const ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", | 181 final table = const ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", |
157 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", | 182 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", |
158 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", | 183 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", |
159 "u", "v", "w", "x", "y", "z"]; | 184 "u", "v", "w", "x", "y", "z"]; |
160 if (radix < 2 || radix > 36) { | 185 if (radix is! int || radix < 2 || radix > 36) { |
161 throw new ArgumentError(radix); | 186 throw new ArgumentError(radix); |
162 } | 187 } |
163 final bool isNegative = this < 0; | 188 final bool isNegative = this < 0; |
164 var value = isNegative ? -this : this; | 189 int value = isNegative ? -this : this; |
165 List temp = new List(); | 190 List temp = new List(); |
166 while (value > 0) { | 191 while (value > 0) { |
167 var digit = value % radix; | 192 int digit = value % radix; |
168 value ~/= radix; | 193 value ~/= radix; |
169 temp.add(digit); | 194 temp.add(digit); |
170 } | 195 } |
171 if (temp.isEmpty) { | 196 if (temp.isEmpty) { |
172 return "0"; | 197 return "0"; |
173 } | 198 } |
174 StringBuffer buffer = new StringBuffer(); | 199 StringBuffer buffer = new StringBuffer(); |
175 if (isNegative) buffer.add("-"); | 200 if (isNegative) buffer.add("-"); |
176 for (int i = temp.length - 1; i >= 0; i--) { | 201 for (int i = temp.length - 1; i >= 0; i--) { |
177 buffer.add(table[temp[i]]); | 202 buffer.add(table[temp[i]]); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 } | 263 } |
239 } | 264 } |
240 int _shlFromInt(int other) { | 265 int _shlFromInt(int other) { |
241 throw const OutOfMemoryError(); | 266 throw const OutOfMemoryError(); |
242 } | 267 } |
243 | 268 |
244 int pow(int exponent) { | 269 int pow(int exponent) { |
245 throw "Bigint.pow not implemented"; | 270 throw "Bigint.pow not implemented"; |
246 } | 271 } |
247 } | 272 } |
OLD | NEW |