Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_FAST_DTOA_H_ |
| 6 | #define V8_FAST_DTOA_H_ |
| 7 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/vector.h" |
| 9 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 10 | namespace v8 { |
| 11 | namespace internal { |
| 12 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 13 | enum FastDtoaMode { |
| 14 | // Computes the shortest representation of the given input. The returned |
| 15 | // result will be the most accurate number of this length. Longer |
| 16 | // representations might be more accurate. |
| 17 | FAST_DTOA_SHORTEST, |
| 18 | // Computes a representation where the precision (number of digits) is |
| 19 | // given as input. The precision is independent of the decimal point. |
| 20 | FAST_DTOA_PRECISION |
| 21 | }; |
| 22 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 23 | // FastDtoa will produce at most kFastDtoaMaximalLength digits. This does not |
| 24 | // include the terminating '\0' character. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 25 | const int kFastDtoaMaximalLength = 17; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 26 | |
| 27 | // Provides a decimal representation of v. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 28 | // The result should be interpreted as buffer * 10^(point - length). |
| 29 | // |
| 30 | // Precondition: |
| 31 | // * v must be a strictly positive finite double. |
| 32 | // |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 33 | // Returns true if it succeeds, otherwise the result can not be trusted. |
| 34 | // There will be *length digits inside the buffer followed by a null terminator. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 35 | // If the function returns true and mode equals |
| 36 | // - FAST_DTOA_SHORTEST, then |
| 37 | // the parameter requested_digits is ignored. |
| 38 | // The result satisfies |
| 39 | // v == (double) (buffer * 10^(point - length)). |
| 40 | // The digits in the buffer are the shortest representation possible. E.g. |
| 41 | // if 0.099999999999 and 0.1 represent the same double then "1" is returned |
| 42 | // with point = 0. |
| 43 | // The last digit will be closest to the actual v. That is, even if several |
| 44 | // digits might correctly yield 'v' when read again, the buffer will contain |
| 45 | // the one closest to v. |
| 46 | // - FAST_DTOA_PRECISION, then |
| 47 | // the buffer contains requested_digits digits. |
| 48 | // the difference v - (buffer * 10^(point-length)) is closest to zero for |
| 49 | // all possible representations of requested_digits digits. |
| 50 | // If there are two values that are equally close, then FastDtoa returns |
| 51 | // false. |
| 52 | // For both modes the buffer must be large enough to hold the result. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 53 | bool FastDtoa(double d, |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 54 | FastDtoaMode mode, |
| 55 | int requested_digits, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 56 | Vector<char> buffer, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 57 | int* length, |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 58 | int* decimal_point); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 59 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 60 | } // namespace internal |
| 61 | } // namespace v8 |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 62 | |
| 63 | #endif // V8_FAST_DTOA_H_ |