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

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: Don't use NULL and rebase. Created 8 years, 10 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
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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 } else if ((Mint::kMinValue <= result) && (result <= Mint::kMaxValue)) { 179 } else if ((Mint::kMinValue <= result) && (result <= Mint::kMaxValue)) {
180 arguments->SetReturn(Mint::Handle(Mint::New(static_cast<int64_t>(result)))); 180 arguments->SetReturn(Mint::Handle(Mint::New(static_cast<int64_t>(result))));
181 } else { 181 } else {
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 // The boundaries are exclusive.
190 static const double kLowerBoundary = -1e21;
191 static const double kUpperBoundary = 1e21;
192
189 const Double& arg = Double::CheckedHandle(arguments->At(0)); 193 const Double& arg = Double::CheckedHandle(arguments->At(0));
190 GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1)); 194 GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1));
191 double d = arg.value(); 195 double d = arg.value();
192 int fraction_digits_value = fraction_digits.Value();
193 String& result = String::Handle(); 196 String& result = String::Handle();
194 bool succeeded = DoubleToStringAsFixed(d, fraction_digits_value, result); 197 intptr_t fraction_digits_value = fraction_digits.Value();
195 if (!succeeded) { 198 if (0 <= fraction_digits_value && fraction_digits_value <= 20
199 && kLowerBoundary < d && d < kUpperBoundary) {
200 result = DoubleToStringAsFixed(d, static_cast<int>(fraction_digits_value));
201 }
202 if (result.IsNull()) {
Ivan Posva 2012/02/27 14:47:14 I assume this test is here in case the DoubleToStr
floitsch 2012/02/27 19:55:40 Done.
196 GrowableArray<const Object*> args; 203 GrowableArray<const Object*> args;
197 args.Add(&String::ZoneHandle(String::New( 204 args.Add(&String::ZoneHandle(String::New(
198 "Illegal arguments to double.toStringAsFixed"))); 205 "Illegal arguments to double.toStringAsFixed")));
199 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args); 206 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
200 } 207 }
201 arguments->SetReturn(result); 208 arguments->SetReturn(result);
202 } 209 }
203 210
204 211
205 DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 2) { 212 DEFINE_NATIVE_ENTRY(Double_toStringAsExponential, 2) {
206 UNIMPLEMENTED(); 213 const Double& arg = Double::CheckedHandle(arguments->At(0));
214 GET_NATIVE_ARGUMENT(Smi, fraction_digits, arguments->At(1));
215 double d = arg.value();
216 String& result = String::Handle();
217 intptr_t fraction_digits_value = fraction_digits.Value();
218 if (-1 <= fraction_digits_value && fraction_digits_value <= 20) {
219 result =
220 DoubleToStringAsExponential(d, static_cast<int>(fraction_digits_value));
Ivan Posva 2012/02/27 14:47:14 Style nit: Generally we break the line after the c
floitsch 2012/02/27 19:55:40 Done.
221 }
222 if (result.IsNull()) {
223 GrowableArray<const Object*> args;
224 args.Add(&String::ZoneHandle(String::New(
225 "Illegal arguments to double.toStringAsExponential")));
226 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
227 }
228 arguments->SetReturn(result);
207 } 229 }
208 230
209 231
210 DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 2) { 232 DEFINE_NATIVE_ENTRY(Double_toStringAsPrecision, 2) {
211 UNIMPLEMENTED(); 233 const Double& arg = Double::CheckedHandle(arguments->At(0));
234 GET_NATIVE_ARGUMENT(Smi, precision, arguments->At(1));
235 double d = arg.value();
236 String& result = String::Handle();
237 intptr_t precision_value = precision.Value();
238 if (1 <= precision_value && precision_value <= 21) {
239 result = DoubleToStringAsPrecision(d, static_cast<int>(precision_value));
240 }
241 if (result.IsNull()) {
242 GrowableArray<const Object*> args;
243 args.Add(&String::ZoneHandle(String::New(
244 "Illegal arguments to double.toStringAsPrecision")));
245 Exceptions::ThrowByType(Exceptions::kIllegalArgument, args);
246 }
247 arguments->SetReturn(result);
212 } 248 }
213 249
214 250
215 DEFINE_NATIVE_ENTRY(Double_isInfinite, 1) { 251 DEFINE_NATIVE_ENTRY(Double_isInfinite, 1) {
216 const Double& arg = Double::CheckedHandle(arguments->At(0)); 252 const Double& arg = Double::CheckedHandle(arguments->At(0));
217 if (isinf(arg.value())) { 253 if (isinf(arg.value())) {
218 arguments->SetReturn(Bool::Handle(Bool::True())); 254 arguments->SetReturn(Bool::Handle(Bool::True()));
219 } else { 255 } else {
220 arguments->SetReturn(Bool::Handle(Bool::False())); 256 arguments->SetReturn(Bool::Handle(Bool::False()));
221 } 257 }
(...skipping 17 matching lines...) Expand all
239 arguments->SetReturn(Bool::Handle(Bool::True())); 275 arguments->SetReturn(Bool::Handle(Bool::True()));
240 } else { 276 } else {
241 arguments->SetReturn(Bool::Handle(Bool::False())); 277 arguments->SetReturn(Bool::Handle(Bool::False()));
242 } 278 }
243 } 279 }
244 280
245 // Add here only functions using/referring to old-style casts. 281 // Add here only functions using/referring to old-style casts.
246 282
247 } // namespace dart 283 } // namespace dart
248 284
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698