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