• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_BIGNUM_DTOA_H_
6 #define V8_BIGNUM_DTOA_H_
7 
8 #include "src/vector.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 enum BignumDtoaMode {
14   // Return the shortest correct representation.
15   // For example the output of 0.299999999999999988897 is (the less accurate but
16   // correct) 0.3.
17   BIGNUM_DTOA_SHORTEST,
18   // Return a fixed number of digits after the decimal point.
19   // For instance fixed(0.1, 4) becomes 0.1000
20   // If the input number is big, the output will be big.
21   BIGNUM_DTOA_FIXED,
22   // Return a fixed number of digits, no matter what the exponent is.
23   BIGNUM_DTOA_PRECISION
24 };
25 
26 // Converts the given double 'v' to ASCII.
27 // The result should be interpreted as buffer * 10^(point-length).
28 // The buffer will be null-terminated.
29 //
30 // The input v must be > 0 and different from NaN, and Infinity.
31 //
32 // The output depends on the given mode:
33 //  - SHORTEST: produce the least amount of digits for which the internal
34 //   identity requirement is still satisfied. If the digits are printed
35 //   (together with the correct exponent) then reading this number will give
36 //   'v' again. The buffer will choose the representation that is closest to
37 //   'v'. If there are two at the same distance, than the number is round up.
38 //   In this mode the 'requested_digits' parameter is ignored.
39 //  - FIXED: produces digits necessary to print a given number with
40 //   'requested_digits' digits after the decimal point. The produced digits
41 //   might be too short in which case the caller has to fill the gaps with '0's.
42 //   Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
43 //   Halfway cases are rounded up. The call toFixed(0.15, 2) thus returns
44 //     buffer="2", point=0.
45 //   Note: the length of the returned buffer has no meaning wrt the significance
46 //   of its digits. That is, just because it contains '0's does not mean that
47 //   any other digit would not satisfy the internal identity requirement.
48 //  - PRECISION: produces 'requested_digits' where the first digit is not '0'.
49 //   Even though the length of produced digits usually equals
50 //   'requested_digits', the function is allowed to return fewer digits, in
51 //   which case the caller has to fill the missing digits with '0's.
52 //   Halfway cases are again rounded up.
53 // 'BignumDtoa' expects the given buffer to be big enough to hold all digits
54 // and a terminating null-character.
55 void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits,
56                 Vector<char> buffer, int* length, int* point);
57 
58 }  // namespace internal
59 }  // namespace v8
60 
61 #endif  // V8_BIGNUM_DTOA_H_
62