mfbt/double-conversion/fast-dtoa.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/double-conversion/fast-dtoa.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +// Copyright 2010 the V8 project authors. All rights reserved.
     1.5 +// Redistribution and use in source and binary forms, with or without
     1.6 +// modification, are permitted provided that the following conditions are
     1.7 +// met:
     1.8 +//
     1.9 +//     * Redistributions of source code must retain the above copyright
    1.10 +//       notice, this list of conditions and the following disclaimer.
    1.11 +//     * Redistributions in binary form must reproduce the above
    1.12 +//       copyright notice, this list of conditions and the following
    1.13 +//       disclaimer in the documentation and/or other materials provided
    1.14 +//       with the distribution.
    1.15 +//     * Neither the name of Google Inc. nor the names of its
    1.16 +//       contributors may be used to endorse or promote products derived
    1.17 +//       from this software without specific prior written permission.
    1.18 +//
    1.19 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.20 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.21 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.22 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.23 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.24 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.25 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.26 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.27 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.28 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.29 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 +
    1.31 +#ifndef DOUBLE_CONVERSION_FAST_DTOA_H_
    1.32 +#define DOUBLE_CONVERSION_FAST_DTOA_H_
    1.33 +
    1.34 +#include "utils.h"
    1.35 +
    1.36 +namespace double_conversion {
    1.37 +
    1.38 +enum FastDtoaMode {
    1.39 +  // Computes the shortest representation of the given input. The returned
    1.40 +  // result will be the most accurate number of this length. Longer
    1.41 +  // representations might be more accurate.
    1.42 +  FAST_DTOA_SHORTEST,
    1.43 +  // Same as FAST_DTOA_SHORTEST but for single-precision floats.
    1.44 +  FAST_DTOA_SHORTEST_SINGLE,
    1.45 +  // Computes a representation where the precision (number of digits) is
    1.46 +  // given as input. The precision is independent of the decimal point.
    1.47 +  FAST_DTOA_PRECISION
    1.48 +};
    1.49 +
    1.50 +// FastDtoa will produce at most kFastDtoaMaximalLength digits. This does not
    1.51 +// include the terminating '\0' character.
    1.52 +static const int kFastDtoaMaximalLength = 17;
    1.53 +// Same for single-precision numbers.
    1.54 +static const int kFastDtoaMaximalSingleLength = 9;
    1.55 +
    1.56 +// Provides a decimal representation of v.
    1.57 +// The result should be interpreted as buffer * 10^(point - length).
    1.58 +//
    1.59 +// Precondition:
    1.60 +//   * v must be a strictly positive finite double.
    1.61 +//
    1.62 +// Returns true if it succeeds, otherwise the result can not be trusted.
    1.63 +// There will be *length digits inside the buffer followed by a null terminator.
    1.64 +// If the function returns true and mode equals
    1.65 +//   - FAST_DTOA_SHORTEST, then
    1.66 +//     the parameter requested_digits is ignored.
    1.67 +//     The result satisfies
    1.68 +//         v == (double) (buffer * 10^(point - length)).
    1.69 +//     The digits in the buffer are the shortest representation possible. E.g.
    1.70 +//     if 0.099999999999 and 0.1 represent the same double then "1" is returned
    1.71 +//     with point = 0.
    1.72 +//     The last digit will be closest to the actual v. That is, even if several
    1.73 +//     digits might correctly yield 'v' when read again, the buffer will contain
    1.74 +//     the one closest to v.
    1.75 +//   - FAST_DTOA_PRECISION, then
    1.76 +//     the buffer contains requested_digits digits.
    1.77 +//     the difference v - (buffer * 10^(point-length)) is closest to zero for
    1.78 +//     all possible representations of requested_digits digits.
    1.79 +//     If there are two values that are equally close, then FastDtoa returns
    1.80 +//     false.
    1.81 +// For both modes the buffer must be large enough to hold the result.
    1.82 +bool FastDtoa(double d,
    1.83 +              FastDtoaMode mode,
    1.84 +              int requested_digits,
    1.85 +              Vector<char> buffer,
    1.86 +              int* length,
    1.87 +              int* decimal_point);
    1.88 +
    1.89 +}  // namespace double_conversion
    1.90 +
    1.91 +#endif  // DOUBLE_CONVERSION_FAST_DTOA_H_

mercurial