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_BIGNUM_DTOA_H_ 38*0e209d39SAndroid Build Coastguard Worker #define DOUBLE_CONVERSION_BIGNUM_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 BignumDtoaMode { 50*0e209d39SAndroid Build Coastguard Worker // Return the shortest correct representation. 51*0e209d39SAndroid Build Coastguard Worker // For example the output of 0.299999999999999988897 is (the less accurate but 52*0e209d39SAndroid Build Coastguard Worker // correct) 0.3. 53*0e209d39SAndroid Build Coastguard Worker BIGNUM_DTOA_SHORTEST, 54*0e209d39SAndroid Build Coastguard Worker // Same as BIGNUM_DTOA_SHORTEST but for single-precision floats. 55*0e209d39SAndroid Build Coastguard Worker BIGNUM_DTOA_SHORTEST_SINGLE, 56*0e209d39SAndroid Build Coastguard Worker // Return a fixed number of digits after the decimal point. 57*0e209d39SAndroid Build Coastguard Worker // For instance fixed(0.1, 4) becomes 0.1000 58*0e209d39SAndroid Build Coastguard Worker // If the input number is big, the output will be big. 59*0e209d39SAndroid Build Coastguard Worker BIGNUM_DTOA_FIXED, 60*0e209d39SAndroid Build Coastguard Worker // Return a fixed number of digits, no matter what the exponent is. 61*0e209d39SAndroid Build Coastguard Worker BIGNUM_DTOA_PRECISION 62*0e209d39SAndroid Build Coastguard Worker }; 63*0e209d39SAndroid Build Coastguard Worker 64*0e209d39SAndroid Build Coastguard Worker // Converts the given double 'v' to ascii. 65*0e209d39SAndroid Build Coastguard Worker // The result should be interpreted as buffer * 10^(point-length). 66*0e209d39SAndroid Build Coastguard Worker // The buffer will be null-terminated. 67*0e209d39SAndroid Build Coastguard Worker // 68*0e209d39SAndroid Build Coastguard Worker // The input v must be > 0 and different from NaN, and Infinity. 69*0e209d39SAndroid Build Coastguard Worker // 70*0e209d39SAndroid Build Coastguard Worker // The output depends on the given mode: 71*0e209d39SAndroid Build Coastguard Worker // - SHORTEST: produce the least amount of digits for which the internal 72*0e209d39SAndroid Build Coastguard Worker // identity requirement is still satisfied. If the digits are printed 73*0e209d39SAndroid Build Coastguard Worker // (together with the correct exponent) then reading this number will give 74*0e209d39SAndroid Build Coastguard Worker // 'v' again. The buffer will choose the representation that is closest to 75*0e209d39SAndroid Build Coastguard Worker // 'v'. If there are two at the same distance, than the number is round up. 76*0e209d39SAndroid Build Coastguard Worker // In this mode the 'requested_digits' parameter is ignored. 77*0e209d39SAndroid Build Coastguard Worker // - FIXED: produces digits necessary to print a given number with 78*0e209d39SAndroid Build Coastguard Worker // 'requested_digits' digits after the decimal point. The produced digits 79*0e209d39SAndroid Build Coastguard Worker // might be too short in which case the caller has to fill the gaps with '0's. 80*0e209d39SAndroid Build Coastguard Worker // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. 81*0e209d39SAndroid Build Coastguard Worker // Halfway cases are rounded up. The call toFixed(0.15, 2) thus returns 82*0e209d39SAndroid Build Coastguard Worker // buffer="2", point=0. 83*0e209d39SAndroid Build Coastguard Worker // Note: the length of the returned buffer has no meaning wrt the significance 84*0e209d39SAndroid Build Coastguard Worker // of its digits. That is, just because it contains '0's does not mean that 85*0e209d39SAndroid Build Coastguard Worker // any other digit would not satisfy the internal identity requirement. 86*0e209d39SAndroid Build Coastguard Worker // - PRECISION: produces 'requested_digits' where the first digit is not '0'. 87*0e209d39SAndroid Build Coastguard Worker // Even though the length of produced digits usually equals 88*0e209d39SAndroid Build Coastguard Worker // 'requested_digits', the function is allowed to return fewer digits, in 89*0e209d39SAndroid Build Coastguard Worker // which case the caller has to fill the missing digits with '0's. 90*0e209d39SAndroid Build Coastguard Worker // Halfway cases are again rounded up. 91*0e209d39SAndroid Build Coastguard Worker // 'BignumDtoa' expects the given buffer to be big enough to hold all digits 92*0e209d39SAndroid Build Coastguard Worker // and a terminating null-character. 93*0e209d39SAndroid Build Coastguard Worker void BignumDtoa(double v, BignumDtoaMode mode, int requested_digits, 94*0e209d39SAndroid Build Coastguard Worker Vector<char> buffer, int* length, int* point); 95*0e209d39SAndroid Build Coastguard Worker 96*0e209d39SAndroid Build Coastguard Worker } // namespace double_conversion 97*0e209d39SAndroid Build Coastguard Worker 98*0e209d39SAndroid Build Coastguard Worker // ICU PATCH: Close ICU namespace 99*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 100*0e209d39SAndroid Build Coastguard Worker 101*0e209d39SAndroid Build Coastguard Worker #endif // DOUBLE_CONVERSION_BIGNUM_DTOA_H_ 102*0e209d39SAndroid Build Coastguard Worker #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING 103