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_BASE_NUMBERS_BIGNUM_DTOA_H_
6#define V8_BASE_NUMBERS_BIGNUM_DTOA_H_
7
8#include "src/base/vector.h"
9
10namespace v8 {
11namespace base {
12
13enum 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.
55V8_BASE_EXPORT void BignumDtoa(double v, BignumDtoaMode mode,
56                               int requested_digits, Vector<char> buffer,
57                               int* length, int* point);
58
59}  // namespace base
60}  // namespace v8
61
62#endif  // V8_BASE_NUMBERS_BIGNUM_DTOA_H_
63