Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: runtime/lib/double.cc

Issue 9113043: Implement Double.{toString, toStringAsExponential, toStringAsPrecision} (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments and updates to status files. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « compiler/lib/implementation/number.js ('k') | runtime/lib/double.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <math.h> 5 #include <math.h>
6 6
7 #include "vm/bootstrap_natives.h" 7 #include "vm/bootstrap_natives.h"
8 8
9 #include "vm/bigint_operations.h" 9 #include "vm/bigint_operations.h"
10 #include "vm/double_conversion.h" 10 #include "vm/double_conversion.h"
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 arguments->SetReturn( 182 arguments->SetReturn(
183 Bigint::Handle(BigintOperations::NewFromDouble(result))); 183 Bigint::Handle(BigintOperations::NewFromDouble(result)));
184 } 184 }
185 } 185 }
186 186
187 187
188 DEFINE_NATIVE_ENTRY(Double_toStringAsFixed, 2) { 188 DEFINE_NATIVE_ENTRY(Double_toStringAsFixed, 2) {
189 const Double& arg = Double::CheckedHandle(arguments->At(0)); 189 const Double& arg = Double::CheckedHandle(arguments->At(0));
190 GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1)); 190 GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1));
191 double d = arg.value(); 191 double d = arg.value();
192 int fraction_digits_value = fraction_digits.Value(); 192 RawString* result = NULL;
Ivan Posva 2012/02/02 23:21:36 You should not keep raw pointers lying around. Thi
floitsch 2012/02/05 15:28:21 Fixed. But out of curiosity: was the current code
Ivan Posva 2012/02/06 18:25:44 It was broken since: - Assigning NULL to a RawObje
193 String& result = String::Handle(); 193 intptr_t fraction_digits_value = fraction_digits.Value();
194 bool succeeded = DoubleToStringAsFixed(d, fraction_digits_value, result); 194 if (0 <= fraction_digits_value && fraction_digits_value <= 20) {
195 if (!succeeded) { 195 result = DoubleToStringAsFixed(d, static_cast<int>(fraction_digits_value));
196 }
197 if (result == NULL) {
196 GrowableArray<const Object*> args; 198 GrowableArray<const Object*> args;
197 args.Add(&String::ZoneHandle(String::New( 199 args.Add(&String::ZoneHandle(String::New(
198 "Illegal arguments to double.toStringAsFixed"))); 200 "Illegal arguments to double.toStringAsFixed")));
199 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); 201 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
200 } 202 }
201 arguments->SetReturn(result); 203 arguments->SetReturn(String::Handle(result));
202 } 204 }
203 205
204 206
205 DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 2) { 207 DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 2) {
206 UNIMPLEMENTED(); 208 const Double& arg = Double::CheckedHandle(arguments->At(0));
209 GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1));
210 double d = arg.value();
211 RawString* result = NULL;
Ivan Posva 2012/02/02 23:21:36 ditto
floitsch 2012/02/05 15:28:21 Done.
212 intptr_t fraction_digits_value = fraction_digits.Value();
213 if (-1 <= fraction_digits_value && fraction_digits_value <= 20) {
214 result =
215 DoubleToStringAsExponential(d, static_cast<int>(fraction_digits_value));
216 }
217 if (result == NULL) {
218 GrowableArray<const Object*> args;
219 args.Add(&String::ZoneHandle(String::New(
220 "Illegal arguments to double.toStringAsExponential")));
221 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
222 }
223 arguments->SetReturn(String::Handle(result));
207 } 224 }
208 225
209 226
210 DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 2) { 227 DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 2) {
211 UNIMPLEMENTED(); 228 const Double& arg = Double::CheckedHandle(arguments->At(0));
229 GET_NATIVE_ARGUMENT(Smi, precision, arguments->At(1));
230 double d = arg.value();
231 RawString* result = NULL;
Ivan Posva 2012/02/02 23:21:36 ditto
floitsch 2012/02/05 15:28:21 Done.
232 intptr_t precision_value = precision.Value();
233 if (1 <= precision_value && precision_value <= 21) {
234 result = DoubleToStringAsPrecision(d, static_cast<int>(precision_value));
235 }
236 if (result == NULL) {
237 GrowableArray<const Object*> args;
238 args.Add(&String::ZoneHandle(String::New(
239 "Illegal arguments to double.toStringAsPrecision")));
240 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
241 }
242 arguments->SetReturn(String::Handle(result));
212 } 243 }
213 244
214 245
215 DEFINE_NATIVE_ENTRY(Double_isInfinite, 1) { 246 DEFINE_NATIVE_ENTRY(Double_isInfinite, 1) {
216 const Double& arg = Double::CheckedHandle(arguments->At(0)); 247 const Double& arg = Double::CheckedHandle(arguments->At(0));
217 if (isinf(arg.value())) { 248 if (isinf(arg.value())) {
218 arguments->SetReturn(Bool::Handle(Bool::True())); 249 arguments->SetReturn(Bool::Handle(Bool::True()));
219 } else { 250 } else {
220 arguments->SetReturn(Bool::Handle(Bool::False())); 251 arguments->SetReturn(Bool::Handle(Bool::False()));
221 } 252 }
(...skipping 17 matching lines...) Expand all
239 arguments->SetReturn(Bool::Handle(Bool::True())); 270 arguments->SetReturn(Bool::Handle(Bool::True()));
240 } else { 271 } else {
241 arguments->SetReturn(Bool::Handle(Bool::False())); 272 arguments->SetReturn(Bool::Handle(Bool::False()));
242 } 273 }
243 } 274 }
244 275
245 // Add here only functions using/referring to old-style casts. 276 // Add here only functions using/referring to old-style casts.
246 277
247 } // namespace dart 278 } // namespace dart
248 279
OLDNEW
« no previous file with comments | « compiler/lib/implementation/number.js ('k') | runtime/lib/double.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698