1// © 2018 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3//
4// From the double-conversion library. Original license:
5//
6// Copyright 2010 the V8 project authors. All rights reserved.
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following
15// disclaimer in the documentation and/or other materials provided
16// with the distribution.
17// * Neither the name of Google Inc. nor the names of its
18// contributors may be used to endorse or promote products derived
19// from this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33// ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
34#include "unicode/utypes.h"
35#if !UCONFIG_NO_FORMATTING
36
37#ifndef DOUBLE_CONVERSION_FAST_DTOA_H_
38#define DOUBLE_CONVERSION_FAST_DTOA_H_
39
40// ICU PATCH: Customize header file paths for ICU.
41
42#include "double-conversion-utils.h"
43
44// ICU PATCH: Wrap in ICU namespace
45U_NAMESPACE_BEGIN
46
47namespace double_conversion {
48
49enum FastDtoaMode {
50 // Computes the shortest representation of the given input. The returned
51 // result will be the most accurate number of this length. Longer
52 // representations might be more accurate.
53 FAST_DTOA_SHORTEST,
54 // Same as FAST_DTOA_SHORTEST but for single-precision floats.
55 FAST_DTOA_SHORTEST_SINGLE,
56 // Computes a representation where the precision (number of digits) is
57 // given as input. The precision is independent of the decimal point.
58 FAST_DTOA_PRECISION
59};
60
61// FastDtoa will produce at most kFastDtoaMaximalLength digits. This does not
62// include the terminating '\0' character.
63static const int kFastDtoaMaximalLength = 17;
64// Same for single-precision numbers.
65static const int kFastDtoaMaximalSingleLength = 9;
66
67// Provides a decimal representation of v.
68// The result should be interpreted as buffer * 10^(point - length).
69//
70// Precondition:
71// * v must be a strictly positive finite double.
72//
73// Returns true if it succeeds, otherwise the result can not be trusted.
74// There will be *length digits inside the buffer followed by a null terminator.
75// If the function returns true and mode equals
76// - FAST_DTOA_SHORTEST, then
77// the parameter requested_digits is ignored.
78// The result satisfies
79// v == (double) (buffer * 10^(point - length)).
80// The digits in the buffer are the shortest representation possible. E.g.
81// if 0.099999999999 and 0.1 represent the same double then "1" is returned
82// with point = 0.
83// The last digit will be closest to the actual v. That is, even if several
84// digits might correctly yield 'v' when read again, the buffer will contain
85// the one closest to v.
86// - FAST_DTOA_PRECISION, then
87// the buffer contains requested_digits digits.
88// the difference v - (buffer * 10^(point-length)) is closest to zero for
89// all possible representations of requested_digits digits.
90// If there are two values that are equally close, then FastDtoa returns
91// false.
92// For both modes the buffer must be large enough to hold the result.
93bool FastDtoa(double d,
94 FastDtoaMode mode,
95 int requested_digits,
96 Vector<char> buffer,
97 int* length,
98 int* decimal_point);
99
100} // namespace double_conversion
101
102// ICU PATCH: Close ICU namespace
103U_NAMESPACE_END
104
105#endif // DOUBLE_CONVERSION_FAST_DTOA_H_
106#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING
107