OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class Double implements double { | 5 class Double implements double { |
6 factory Double.fromInteger(int value) | 6 factory Double.fromInteger(int value) |
7 native "Double_doubleFromInteger"; | 7 native "Double_doubleFromInteger"; |
8 int hashCode() { | 8 int hashCode() { |
9 try { | 9 try { |
10 return toInt(); | 10 return toInt(); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } | 101 } |
102 | 102 |
103 double round() native "Double_round"; | 103 double round() native "Double_round"; |
104 double floor() native "Double_floor"; | 104 double floor() native "Double_floor"; |
105 double ceil () native "Double_ceil"; | 105 double ceil () native "Double_ceil"; |
106 double truncate() native "Double_truncate"; | 106 double truncate() native "Double_truncate"; |
107 int toInt() native "Double_toInt"; | 107 int toInt() native "Double_toInt"; |
108 double toDouble() { return this; } | 108 double toDouble() { return this; } |
109 | 109 |
110 double pow(num exponent) { | 110 double pow(num exponent) { |
111 return pow_(exponent.toDouble()); | 111 return _pow(exponent.toDouble()); |
112 } | 112 } |
113 double pow_(double exponent) native "Double_pow"; | 113 double _pow(double exponent) native "Double_pow"; |
114 | 114 |
115 String toStringAsFixed(int fractionDigits) { | 115 String toStringAsFixed(int fractionDigits) { |
116 // See ECMAScript-262, 15.7.4.5 for details. | 116 // See ECMAScript-262, 15.7.4.5 for details. |
117 | 117 |
118 // Step 2. | 118 // Step 2. |
119 if (fractionDigits < 0 || fractionDigits > 20) { | 119 if (fractionDigits < 0 || fractionDigits > 20) { |
120 // TODO(antonm): should be proper RangeError or Dart counterpart. | 120 // TODO(antonm): should be proper RangeError or Dart counterpart. |
121 throw "Range error"; | 121 throw "Range error"; |
122 } | 122 } |
123 | 123 |
124 // Step 3. | 124 // Step 3. |
125 double x = this; | 125 double x = this; |
126 | 126 |
127 // Step 4. | 127 // Step 4. |
128 if (x.isNaN()) { | 128 if (isNaN()) return "NaN"; |
129 return "NaN"; | 129 |
| 130 // Step 5 and 6 skipped. Will be dealt with by native function. |
| 131 |
| 132 // Step 7. |
| 133 if (x >= 1e21 || x <= -1e21) { |
| 134 return x.toString(); |
130 } | 135 } |
131 | 136 |
132 // Step 5. | 137 return _toStringAsFixed(fractionDigits); |
133 String s = ""; | 138 } |
| 139 String _toStringAsFixed(int fractionDigits) native "Double_toStringAsFixed"; |
134 | 140 |
135 // Step 6. | 141 String toStringAsExponential(int fractionDigits) { |
136 if (x.isNegative() && x != 0.0) { | 142 // See ECMAScript-262, 15.7.4.6 for details. |
137 s = "-"; | 143 |
138 x = -x; | 144 // The EcmaScript specification checks for NaN and Infinity before looking |
| 145 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and |
| 146 // look at the fractionDigits first. |
| 147 |
| 148 // Step 7. |
| 149 if (fractionDigits < 0 || fractionDigits > 20) { |
| 150 // TODO(antonm): should be proper RangeError or Dart counterpart. |
| 151 throw "Range error"; |
139 } | 152 } |
140 | 153 |
141 // Step 7. | 154 return _toStringAsExponential(fractionDigits); |
142 String m; | 155 } |
143 if (x > 10e21) { | 156 String _toStringAsExponential(int fractionDigits) |
144 m = x.toString(); | 157 native "Double_toStringAsExponential"; |
145 } else { | |
146 // Step 8. | |
147 | 158 |
148 // Step 8.a. | 159 String toStringAsPrecision(int precision) { |
149 // This is an approximation for n from the standard. | 160 // See ECMAScript-262, 15.7.4.7 for details. |
150 int n = (x * (10.0).pow(fractionDigits)).round().toInt(); | |
151 | 161 |
152 // Step 8.b. | 162 // The EcmaScript specification checks for NaN and Infinity before looking |
153 m = n.toString(); | 163 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and |
| 164 // look at the fractionDigits first. |
154 | 165 |
155 // Step 8.c. | 166 // Step 8. |
156 if (fractionDigits != 0) { | 167 if (precision < 1 || precision > 21) { |
157 // Step 8.c.i. | 168 // TODO(antonm): should be proper RangeError or Dart counterpart. |
158 int k = m.length; | 169 throw "Range error"; |
159 // Step 8.c.ii. | |
160 if (k <= fractionDigits) { | |
161 StringBuffer sb = new StringBuffer(); | |
162 for (int i = 0; i < fractionDigits + 1 - k; i++) { | |
163 sb.add("0"); | |
164 } | |
165 String z = sb.toString(); | |
166 m = z + m; | |
167 k = fractionDigits + 1; | |
168 } | |
169 // Step 8.c.iii. | |
170 String a = m.substring(0, k - fractionDigits); | |
171 String b = m.substring(k - fractionDigits); | |
172 // Step 8.c.iv. | |
173 m = a + "." + b; | |
174 } | |
175 } | 170 } |
176 | 171 |
177 // Step 9. | 172 return _toStringAsPrecision(precision); |
178 return s + m; | |
179 } | 173 } |
180 String toStringAsExponential(int fractionDigits) { | 174 String _toStringAsPrecision(int fractionDigits) |
181 throw "Double.toStringAsExponential unimplemented."; | 175 native "Double_toStringAsPrecision"; |
182 } | 176 |
183 String toStringAsPrecision(int precision) { | |
184 throw "Double.toStringAsPrecision unimplemented."; | |
185 } | |
186 String toRadixString(int radix) { | 177 String toRadixString(int radix) { |
187 throw "Double.toRadixString unimplemented."; | 178 throw "Double.toRadixString unimplemented."; |
188 } | 179 } |
189 | 180 |
190 // Order is: NaN > Infinity > ... > 0.0 > -0.0 > ... > -Infinity. | 181 // Order is: NaN > Infinity > ... > 0.0 > -0.0 > ... > -Infinity. |
191 int compareTo(Comparable other) { | 182 int compareTo(Comparable other) { |
192 final int EQUAL = 0, LESS = -1, GREATER = 1; | 183 final int EQUAL = 0, LESS = -1, GREATER = 1; |
193 if (this < other) { | 184 if (this < other) { |
194 return LESS; | 185 return LESS; |
195 } else if (this > other) { | 186 } else if (this > other) { |
(...skipping 10 matching lines...) Expand all Loading... |
206 return EQUAL; | 197 return EQUAL; |
207 } | 198 } |
208 } else if (isNaN()) { | 199 } else if (isNaN()) { |
209 return other.isNaN() ? EQUAL : GREATER; | 200 return other.isNaN() ? EQUAL : GREATER; |
210 } else { | 201 } else { |
211 // Other is NaN. | 202 // Other is NaN. |
212 return LESS; | 203 return LESS; |
213 } | 204 } |
214 } | 205 } |
215 } | 206 } |
OLD | NEW |