| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 // Buffer for the decimal part of the result. We only generate up | 387 // Buffer for the decimal part of the result. We only generate up |
| 388 // to kBufferSize - 1 chars for the decimal part. | 388 // to kBufferSize - 1 chars for the decimal part. |
| 389 char decimal_buffer[kBufferSize]; | 389 char decimal_buffer[kBufferSize]; |
| 390 decimal_buffer[kBufferSize - 1] = '\0'; | 390 decimal_buffer[kBufferSize - 1] = '\0'; |
| 391 | 391 |
| 392 // Make sure the value is positive. | 392 // Make sure the value is positive. |
| 393 bool is_negative = value < 0.0; | 393 bool is_negative = value < 0.0; |
| 394 if (is_negative) value = -value; | 394 if (is_negative) value = -value; |
| 395 | 395 |
| 396 // Get the integer part and the decimal part. | 396 // Get the integer part and the decimal part. |
| 397 double integer_part = floor(value); | 397 double integer_part = std::floor(value); |
| 398 double decimal_part = value - integer_part; | 398 double decimal_part = value - integer_part; |
| 399 | 399 |
| 400 // Convert the integer part starting from the back. Always generate | 400 // Convert the integer part starting from the back. Always generate |
| 401 // at least one digit. | 401 // at least one digit. |
| 402 int integer_pos = kBufferSize - 2; | 402 int integer_pos = kBufferSize - 2; |
| 403 do { | 403 do { |
| 404 double remainder = fmod(integer_part, radix); | 404 double remainder = std::fmod(integer_part, radix); |
| 405 integer_buffer[integer_pos--] = chars[static_cast<int>(remainder)]; | 405 integer_buffer[integer_pos--] = chars[static_cast<int>(remainder)]; |
| 406 integer_part -= remainder; | 406 integer_part -= remainder; |
| 407 integer_part /= radix; | 407 integer_part /= radix; |
| 408 } while (integer_part >= 1.0); | 408 } while (integer_part >= 1.0); |
| 409 // Sanity check. | 409 // Sanity check. |
| 410 ASSERT(integer_pos > 0); | 410 ASSERT(integer_pos > 0); |
| 411 // Add sign if needed. | 411 // Add sign if needed. |
| 412 if (is_negative) integer_buffer[integer_pos--] = '-'; | 412 if (is_negative) integer_buffer[integer_pos--] = '-'; |
| 413 | 413 |
| 414 // Convert the decimal part. Repeatedly multiply by the radix to | 414 // Convert the decimal part. Repeatedly multiply by the radix to |
| 415 // generate the next char. Never generate more than kBufferSize - 1 | 415 // generate the next char. Never generate more than kBufferSize - 1 |
| 416 // chars. | 416 // chars. |
| 417 // | 417 // |
| 418 // TODO(1093998): We will often generate a full decimal_buffer of | 418 // TODO(1093998): We will often generate a full decimal_buffer of |
| 419 // chars because hitting zero will often not happen. The right | 419 // chars because hitting zero will often not happen. The right |
| 420 // solution would be to continue until the string representation can | 420 // solution would be to continue until the string representation can |
| 421 // be read back and yield the original value. To implement this | 421 // be read back and yield the original value. To implement this |
| 422 // efficiently, we probably have to modify dtoa. | 422 // efficiently, we probably have to modify dtoa. |
| 423 int decimal_pos = 0; | 423 int decimal_pos = 0; |
| 424 while ((decimal_part > 0.0) && (decimal_pos < kBufferSize - 1)) { | 424 while ((decimal_part > 0.0) && (decimal_pos < kBufferSize - 1)) { |
| 425 decimal_part *= radix; | 425 decimal_part *= radix; |
| 426 decimal_buffer[decimal_pos++] = | 426 decimal_buffer[decimal_pos++] = |
| 427 chars[static_cast<int>(floor(decimal_part))]; | 427 chars[static_cast<int>(std::floor(decimal_part))]; |
| 428 decimal_part -= floor(decimal_part); | 428 decimal_part -= std::floor(decimal_part); |
| 429 } | 429 } |
| 430 decimal_buffer[decimal_pos] = '\0'; | 430 decimal_buffer[decimal_pos] = '\0'; |
| 431 | 431 |
| 432 // Compute the result size. | 432 // Compute the result size. |
| 433 int integer_part_size = kBufferSize - 2 - integer_pos; | 433 int integer_part_size = kBufferSize - 2 - integer_pos; |
| 434 // Make room for zero termination. | 434 // Make room for zero termination. |
| 435 unsigned result_size = integer_part_size + decimal_pos; | 435 unsigned result_size = integer_part_size + decimal_pos; |
| 436 // If the number has a decimal part, leave room for the period. | 436 // If the number has a decimal part, leave room for the period. |
| 437 if (decimal_pos > 0) result_size++; | 437 if (decimal_pos > 0) result_size++; |
| 438 // Allocate result and fill in the parts. | 438 // Allocate result and fill in the parts. |
| 439 SimpleStringBuilder builder(result_size + 1); | 439 SimpleStringBuilder builder(result_size + 1); |
| 440 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); | 440 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
| 441 if (decimal_pos > 0) builder.AddCharacter('.'); | 441 if (decimal_pos > 0) builder.AddCharacter('.'); |
| 442 builder.AddSubstring(decimal_buffer, decimal_pos); | 442 builder.AddSubstring(decimal_buffer, decimal_pos); |
| 443 return builder.Finalize(); | 443 return builder.Finalize(); |
| 444 } | 444 } |
| 445 | 445 |
| 446 } } // namespace v8::internal | 446 } } // namespace v8::internal |
| OLD | NEW |