double-conversion-fast-dtoa.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
  33. #include "unicode/utypes.h"
  34. #if !UCONFIG_NO_FORMATTING
  35. #ifndef DOUBLE_CONVERSION_FAST_DTOA_H_
  36. #define DOUBLE_CONVERSION_FAST_DTOA_H_
  37. // ICU PATCH: Customize header file paths for ICU.
  38. #include "double-conversion-utils.h"
  39. // ICU PATCH: Wrap in ICU namespace
  40. U_NAMESPACE_BEGIN
  41. namespace double_conversion {
  42. enum FastDtoaMode {
  43. // Computes the shortest representation of the given input. The returned
  44. // result will be the most accurate number of this length. Longer
  45. // representations might be more accurate.
  46. FAST_DTOA_SHORTEST,
  47. // Same as FAST_DTOA_SHORTEST but for single-precision floats.
  48. FAST_DTOA_SHORTEST_SINGLE,
  49. // Computes a representation where the precision (number of digits) is
  50. // given as input. The precision is independent of the decimal point.
  51. FAST_DTOA_PRECISION
  52. };
  53. // FastDtoa will produce at most kFastDtoaMaximalLength digits. This does not
  54. // include the terminating '\0' character.
  55. static const int kFastDtoaMaximalLength = 17;
  56. // Same for single-precision numbers.
  57. static const int kFastDtoaMaximalSingleLength = 9;
  58. // Provides a decimal representation of v.
  59. // The result should be interpreted as buffer * 10^(point - length).
  60. //
  61. // Precondition:
  62. // * v must be a strictly positive finite double.
  63. //
  64. // Returns true if it succeeds, otherwise the result can not be trusted.
  65. // There will be *length digits inside the buffer followed by a null terminator.
  66. // If the function returns true and mode equals
  67. // - FAST_DTOA_SHORTEST, then
  68. // the parameter requested_digits is ignored.
  69. // The result satisfies
  70. // v == (double) (buffer * 10^(point - length)).
  71. // The digits in the buffer are the shortest representation possible. E.g.
  72. // if 0.099999999999 and 0.1 represent the same double then "1" is returned
  73. // with point = 0.
  74. // The last digit will be closest to the actual v. That is, even if several
  75. // digits might correctly yield 'v' when read again, the buffer will contain
  76. // the one closest to v.
  77. // - FAST_DTOA_PRECISION, then
  78. // the buffer contains requested_digits digits.
  79. // the difference v - (buffer * 10^(point-length)) is closest to zero for
  80. // all possible representations of requested_digits digits.
  81. // If there are two values that are equally close, then FastDtoa returns
  82. // false.
  83. // For both modes the buffer must be large enough to hold the result.
  84. bool FastDtoa(double d,
  85. FastDtoaMode mode,
  86. int requested_digits,
  87. Vector<char> buffer,
  88. int* length,
  89. int* decimal_point);
  90. } // namespace double_conversion
  91. // ICU PATCH: Close ICU namespace
  92. U_NAMESPACE_END
  93. #endif // DOUBLE_CONVERSION_FAST_DTOA_H_
  94. #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING