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