Chromium Code Reviews| Index: runtime/lib/double.dart |
| diff --git a/runtime/lib/double.dart b/runtime/lib/double.dart |
| index 12ac0908d39b22198c2adbc89faf74683be6901c..72ddf2f826c3f36282ce50ae62fb6c3b5726005f 100644 |
| --- a/runtime/lib/double.dart |
| +++ b/runtime/lib/double.dart |
| @@ -108,9 +108,9 @@ class Double implements double { |
| double toDouble() { return this; } |
| double pow(num exponent) { |
| - return pow_(exponent.toDouble()); |
| + return _pow(exponent.toDouble()); |
| } |
| - double pow_(double exponent) native "Double_pow"; |
| + double _pow(double exponent) native "Double_pow"; |
|
Ivan Posva
2011/12/20 00:30:03
Thanks!
|
| String toStringAsFixed(int fractionDigits) { |
| // See ECMAScript-262, 15.7.4.5 for details. |
| @@ -124,65 +124,53 @@ class Double implements double { |
| // Step 3. |
| double x = this; |
| - // Step 4. |
| - if (x.isNaN()) { |
| - return "NaN"; |
| + // Step 4, 5 and 6 skipped. Will be dealt with by native function. |
| + |
| + // Step 7. |
| + if (x >= 1e21 || x <= -1e21) { |
| + return x.toString(); |
| } |
| - // Step 5. |
| - String s = ""; |
| + return _toStringAsFixed(fractionDigits); |
| + } |
| + String _toStringAsFixed(int fractionDigits) native "Double_toStringAsFixed"; |
| + |
| + String toStringAsExponential(int fractionDigits) { |
| + // See ECMAScript-262, 15.7.4.6 for details. |
| - // Step 6. |
| - if (x.isNegative() && x != 0.0) { |
| - s = "-"; |
| - x = -x; |
| - } |
| + // The EcmaScript specification checks for NaN and Infinity before looking |
| + // at the fractionDigits. In Dart we are consistent with toStringAsFixed and |
| + // look at the fractionDigits first. |
| // Step 7. |
| - String m; |
| - if (x > 10e21) { |
| - m = x.toString(); |
| - } else { |
| - // Step 8. |
| - |
| - // Step 8.a. |
| - // This is an approximation for n from the standard. |
| - int n = (x * (10.0).pow(fractionDigits)).round().toInt(); |
| - |
| - // Step 8.b. |
| - m = n.toString(); |
| - |
| - // Step 8.c. |
| - if (fractionDigits != 0) { |
| - // Step 8.c.i. |
| - int k = m.length; |
| - // Step 8.c.ii. |
| - if (k <= fractionDigits) { |
| - StringBuffer sb = new StringBuffer(); |
| - for (int i = 0; i < fractionDigits + 1 - k; i++) { |
| - sb.add("0"); |
| - } |
| - String z = sb.toString(); |
| - m = z + m; |
| - k = fractionDigits + 1; |
| - } |
| - // Step 8.c.iii. |
| - String a = m.substring(0, k - fractionDigits); |
| - String b = m.substring(k - fractionDigits); |
| - // Step 8.c.iv. |
| - m = a + "." + b; |
| - } |
| + if (fractionDigits < 0 || fractionDigits > 20) { |
| + // TODO(antonm): should be proper RangeError or Dart counterpart. |
| + throw "Range error"; |
| } |
| - // Step 9. |
| - return s + m; |
| - } |
| - String toStringAsExponential(int fractionDigits) { |
| - throw "Double.toStringAsExponential unimplemented."; |
| + return _toStringAsExponential(fractionDigits); |
| } |
| + String _toStringAsExponential(int fractionDigits) |
| + native "Double_toStringAsExponential"; |
| + |
| String toStringAsPrecision(int precision) { |
| - throw "Double.toStringAsPrecision unimplemented."; |
| + // See ECMAScript-262, 15.7.4.7 for details. |
| + |
| + // The EcmaScript specification checks for NaN and Infinity before looking |
| + // at the fractionDigits. In Dart we are consistent with toStringAsFixed and |
| + // look at the fractionDigits first. |
| + |
| + // Step 8. |
| + if (precision < 1 || precision > 21) { |
| + // TODO(antonm): should be proper RangeError or Dart counterpart. |
| + throw "Range error"; |
| + } |
| + |
| + return _toStringAsPrecision(precision); |
| } |
| + String _toStringAsPrecision(int fractionDigits) |
| + native "Double_toStringAsPrecision"; |
| + |
| String toRadixString(int radix) { |
| throw "Double.toRadixString unimplemented."; |
| } |