| 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 // Dart core library. | 4 // Dart core library. |
| 5 | 5 |
| 6 // VM implementation of double. | 6 // VM implementation of double. |
| 7 | 7 |
| 8 patch class double { | 8 patch class double { |
| 9 | 9 |
| 10 static double _nativeParse(String str, | 10 static double _nativeParse(String str, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 int expPart = int._tryParseSmi(str, i, end - 1); | 73 int expPart = int._tryParseSmi(str, i, end - 1); |
| 74 if (expPart == null) return null; | 74 if (expPart == null) return null; |
| 75 exponent += expPart; | 75 exponent += expPart; |
| 76 break; | 76 break; |
| 77 } else { | 77 } else { |
| 78 return null; | 78 return null; |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 if (!digitsSeen) return null; // No digits. | 81 if (!digitsSeen) return null; // No digits. |
| 82 if (exponent == 0) return sign * doubleValue; | 82 if (exponent == 0) return sign * doubleValue; |
| 83 // Powers of 10 up to 10^22 are representable as doubles. | 83 const P10 = POWERS_OF_TEN; // From shared library |
| 84 // Powers of 10 above that are only approximate due to lack of precission. | |
| 85 const P10 = const [ | |
| 86 1.0, /* 0 */ | |
| 87 10.0, | |
| 88 100.0, | |
| 89 1000.0, | |
| 90 10000.0, | |
| 91 100000.0, /* 5 */ | |
| 92 1000000.0, | |
| 93 10000000.0, | |
| 94 100000000.0, | |
| 95 1000000000.0, | |
| 96 10000000000.0, /* 10 */ | |
| 97 100000000000.0, | |
| 98 1000000000000.0, | |
| 99 10000000000000.0, | |
| 100 100000000000000.0, | |
| 101 1000000000000000.0, /* 15 */ | |
| 102 10000000000000000.0, | |
| 103 100000000000000000.0, | |
| 104 1000000000000000000.0, | |
| 105 10000000000000000000.0, | |
| 106 100000000000000000000.0, /* 20 */ | |
| 107 1000000000000000000000.0, | |
| 108 10000000000000000000000.0, | |
| 109 ]; | |
| 110 if (exponent < 0) { | 84 if (exponent < 0) { |
| 111 int negExponent = -exponent; | 85 int negExponent = -exponent; |
| 112 if (negExponent >= P10.length) return null; | 86 if (negExponent >= P10.length) return null; |
| 113 return sign * (doubleValue / P10[negExponent]); | 87 return sign * (doubleValue / P10[negExponent]); |
| 114 } | 88 } |
| 115 if (exponent >= P10.length) return null; | 89 if (exponent >= P10.length) return null; |
| 116 return sign * (doubleValue * P10[exponent]); | 90 return sign * (doubleValue * P10[exponent]); |
| 117 } | 91 } |
| 118 | 92 |
| 119 static double _parse(var str) { | 93 static double _parse(var str) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 130 /* patch */ static double parse(String str, | 104 /* patch */ static double parse(String str, |
| 131 [double onError(String str)]) { | 105 [double onError(String str)]) { |
| 132 var result = _parse(str); | 106 var result = _parse(str); |
| 133 if (result == null) { | 107 if (result == null) { |
| 134 if (onError == null) throw new FormatException("Invalid double", str); | 108 if (onError == null) throw new FormatException("Invalid double", str); |
| 135 return onError(str); | 109 return onError(str); |
| 136 } | 110 } |
| 137 return result; | 111 return result; |
| 138 } | 112 } |
| 139 } | 113 } |
| OLD | NEW |