// digits, big-endian representation // decimal point // number of digits used // negative flag // discarded nonzero digits beyond d[:nd] Assign v to a. Round a to nd digits (or fewer).
If nd is zero, it means we're rounding
just to the left of the digits, as in
0.09 -> 0.1. Round a down to nd digits (or fewer). Round a up to nd digits (or fewer). Extract integer part, rounded appropriately.
No guarantees about overflow. Binary shift left (k > 0) or right (k < 0).(*decimal) String() string(*decimal) floatBits(flt *floatInfo) (b uint64, overflow bool)(*decimal) set(s string) (ok bool)
*decimal : fmt.Stringer
*decimal : context.stringer
*decimal : runtime.stringer
func leftShift(a *decimal, k uint)
func rightShift(a *decimal, k uint)
func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo)
func shouldRoundUp(a *decimal, nd int) bool
func trim(a *decimal)
Package-Level Functions (total 89, in which 18 are exported)
AppendBool appends "true" or "false", according to the value of b,
to dst and returns the extended buffer.
AppendComplex appends the result of FormatComplex to dst.
It is here for the runtime.
There is no public strconv.AppendComplex.
AppendFloat appends the string form of the floating-point number f,
as generated by [FormatFloat], to dst and returns the extended buffer.
AppendInt appends the string form of the integer i,
as generated by [FormatInt], to dst and returns the extended buffer.
AppendUint appends the string form of the unsigned integer i,
as generated by [FormatUint], to dst and returns the extended buffer.
Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
FormatBool returns "true" or "false" according to the value of b.
FormatComplex converts the complex number c to a string of the
form (a+bi) where a and b are the real and imaginary parts,
formatted according to the format fmt and precision prec.
The format fmt and precision prec have the same meaning as in [FormatFloat].
It rounds the result assuming that the original was obtained from a complex
value of bitSize bits, which must be 64 for complex64 and 128 for complex128.
FormatFloat converts the floating-point number f to a string,
according to the format fmt and precision prec. It rounds the
result assuming that the original was obtained from a floating-point
value of bitSize bits (32 for float32, 64 for float64).
The format fmt is one of
- 'b' (-ddddp±ddd, a binary exponent),
- 'e' (-d.dddde±dd, a decimal exponent),
- 'E' (-d.ddddE±dd, a decimal exponent),
- 'f' (-ddd.dddd, no exponent),
- 'g' ('e' for large exponents, 'f' otherwise),
- 'G' ('E' for large exponents, 'f' otherwise),
- 'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
- 'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).
The precision prec controls the number of digits (excluding the exponent)
printed by the 'e', 'E', 'f', 'g', 'G', 'x', and 'X' formats.
For 'e', 'E', 'f', 'x', and 'X', it is the number of digits after the decimal point.
For 'g' and 'G' it is the maximum number of significant digits (trailing
zeros are removed).
The special precision -1 uses the smallest number of digits
necessary such that ParseFloat will return f exactly.
The exponent is written as a decimal integer;
for all formats other than 'b', it will be at least two digits.
FormatInt returns the string representation of i in the given base,
for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
for digit values >= 10.
FormatUint returns the string representation of i in the given base,
for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
for digit values >= 10.
Itoa is equivalent to [FormatInt](int64(i), 10).
ParseBool returns the boolean value represented by the string.
It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
Any other value returns an error.
ParseComplex converts the string s to a complex number
with the precision specified by bitSize: 64 for complex64, or 128 for complex128.
When bitSize=64, the result still has type complex128, but it will be
convertible to complex64 without changing its value.
The number represented by s must be of the form N, Ni, or N±Ni, where N stands
for a floating-point number as recognized by [ParseFloat], and i is the imaginary
component. If the second N is unsigned, a + sign is required between the two components
as indicated by the ±. If the second N is NaN, only a + sign is accepted.
The form may be parenthesized and cannot contain any spaces.
The resulting complex number consists of the two components converted by ParseFloat.
The errors that ParseComplex returns have concrete type [*NumError]
and include err.Num = s.
If s is not syntactically well-formed, ParseComplex returns err.Err = ErrSyntax.
If s is syntactically well-formed but either component is more than 1/2 ULP
away from the largest floating point number of the given component's size,
ParseComplex returns err.Err = ErrRange and c = ±Inf for the respective component.
ParseFloat converts the string s to a floating-point number
with the precision specified by bitSize: 32 for float32, or 64 for float64.
When bitSize=32, the result still has type float64, but it will be
convertible to float32 without changing its value.
ParseFloat accepts decimal and hexadecimal floating-point numbers
as defined by the Go syntax for [floating-point literals].
If s is well-formed and near a valid floating-point number,
ParseFloat returns the nearest floating-point number rounded
using IEEE754 unbiased rounding.
(Parsing a hexadecimal floating-point value only rounds when
there are more bits in the hexadecimal representation than
will fit in the mantissa.)
The errors that ParseFloat returns have concrete type *NumError
and include err.Num = s.
If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
If s is syntactically well-formed but is more than 1/2 ULP
away from the largest floating point number of the given size,
ParseFloat returns f = ±Inf, err.Err = ErrRange.
ParseFloat recognizes the string "NaN", and the (possibly signed) strings "Inf" and "Infinity"
as their respective special floating point values. It ignores case when matching.
[floating-point literals]: https://go.dev/ref/spec#Floating-point_literals
ParseInt interprets a string s in the given base (0, 2 to 36) and
bit size (0 to 64) and returns the corresponding value i.
The string may begin with a leading sign: "+" or "-".
If the base argument is 0, the true base is implied by the string's
prefix following the sign (if present): 2 for "0b", 8 for "0" or "0o",
16 for "0x", and 10 otherwise. Also, for argument base 0 only,
underscore characters are permitted as defined by the Go syntax for
[integer literals].
The bitSize argument specifies the integer type
that the result must fit into. Bit sizes 0, 8, 16, 32, and 64
correspond to int, int8, int16, int32, and int64.
If bitSize is below 0 or above 64, an error is returned.
The errors that ParseInt returns have concrete type [*NumError]
and include err.Num = s. If s is empty or contains invalid
digits, err.Err = [ErrSyntax] and the returned value is 0;
if the value corresponding to s cannot be represented by a
signed integer of the given size, err.Err = [ErrRange] and the
returned value is the maximum magnitude integer of the
appropriate bitSize and sign.
[integer literals]: https://go.dev/ref/spec#Integer_literals
ParseUint is like [ParseInt] but for unsigned numbers.
A sign prefix is not permitted.
RuntimeFormatBase10 formats u into the tail of a
and returns the offset to the first byte written to a.
It is only for use by package runtime.
Other packages should use AppendUint.
If possible to convert decimal representation to 64-bit float f exactly,
entirely in floating-point math, do so, avoiding the expense of decimalToFloatBits.
Three common cases:
value is exact integer
value is exact integer * exact power of ten
value is exact integer / exact power of ten
These all produce potentially inexact but correctly rounded answers.
atofHex converts the hex floating-point string s
to a rounded float32 or float64 value (depending on flt==&float32info or flt==&float64info)
and returns it as a float64.
The string s has already been parsed into a mantissa, exponent, and sign (neg==true for negative).
If trunc is true, trailing non-zero bits have been omitted from the mantissa.
bigFtoa uses multiprecision computations to format a float.
commonPrefixLenIgnoreCase returns the length of the common
prefix of s and prefix, with the character case of s ignored.
The prefix argument must be all lower-case.
dboxDelta32 returns δ^(i) from the precomputed value of φ̃k for float32.
dboxDelta64 returns δ^(i) from the precomputed value of φ̃k for float64.
dboxDigits emits decimal digits of mant in d for float64
and adjusts the decimal point based on exp.
dragonboxFtoa computes the decimal significand and exponent
from the binary significand and exponent using the Dragonbox algorithm
and formats the decimal floating point number in d.
Almost identical to dragonboxFtoa64.
This is kept as a separate copy to minimize runtime overhead.
dboxMulPow32 computes x^(i), y^(i), z^(i)
from the precomputed value of φ̃k for float32
and also checks if x^(f), y^(f), z^(f) == 0 (section 5.2.1).
dboxMulPow64 computes x^(i), y^(i), z^(i)
from the precomputed value of φ̃k for float64
and also checks if x^(f), y^(f), z^(f) == 0 (section 5.2.1).
dboxParity32 computes only the parity of x^(i), y^(i), z^(i)
from the precomputed value of φ̃k for float32
and also checks if x^(f), y^(f), z^(f) = 0 (section 5.2.1).
dboxParity64 computes only the parity of x^(i), y^(i), z^(i)
from the precomputed value of φ̃k for float64
and also checks if x^(f), y^(f), z^(f) = 0 (section 5.2.1).
dboxPow32 gets the precomputed value of φ̃̃k for float32.
dboxPow64 gets the precomputed value of φ̃̃k for float64.
dboxRange32 returns the left and right float32 endpoints.
dboxRange64 returns the left and right float64 endpoints.
dboxRoundUp32 computes the round up of y (i.e., y^(ru)).
dboxRoundUp64 computes the round up of y (i.e., y^(ru)).
divisiblePow5 reports whether x is divisible by 5^p.
It returns false for p not in [1, 22],
because we only care about float64 mantissas, and 5^23 > 2^53.
fixedFtoa formats a number of decimal digits of mant*(2^exp) into d,
where mant > 0 and 1 ≤ digits ≤ 18.
If fmt == 'f', digits is a conservative overestimate, and the final
number of digits is prec past the decimal point.
%x: -0x1.yyyyyyyyp±ddd or -0x0p+0. (y is hex digit, d is decimal digit)
formatBase10 formats the decimal representation of u into the tail of a
and returns the offset of the first byte written to a. That is, after
i := formatBase10(a, u)
the decimal representation is in a[i:].
formatBits computes the string representation of u in the given base.
If neg is set, u is treated as negative int64 value. If append_ is
set, the string is appended to dst and the resulting byte slice is
returned as the first result value; otherwise the string is returned
as the second result value.
The caller is expected to have handled base 10 separately for speed.
Binary shift left (* 2) by k bits. k <= maxShift to avoid overflow.
lower(c) is a lower-case letter if and only if
c is either that lower-case letter or the equivalent upper-case letter.
Instead of writing c == 'x' || c == 'X' one can write lower(c) == 'x'.
Note that lower of non-letters can produce other non-letters.
mulLog10_2 returns math.Floor(x * log(2)/log(10)) for an integer x in
the range -1600 <= x && x <= +1600.
The range restriction lets us work in faster integer arithmetic instead of
slower floating point arithmetic. Correctness is verified by unit tests.
mulLog2_10 returns math.Floor(x * log(10)/log(2)) for an integer x in
the range -500 <= x && x <= +500.
The range restriction lets us work in faster integer arithmetic instead of
slower floating point arithmetic. Correctness is verified by unit tests.
pow10 returns the 128-bit mantissa and binary exponent of 10**e.
That is, 10^e = mant/2^128 * 2**exp.
If e is out of range, pow10 returns ok=false.
Is the leading prefix of b lexicographically less than s?
readFloat reads a decimal or hexadecimal mantissa and exponent from a float
string representation in s; the number may be followed by other characters.
readFloat reports the number of bytes consumed (i), and whether the number
is valid (ok).
Binary shift right (/ 2) by k bits. k <= maxShift to avoid overflow.
roundShortest rounds d (= mant * 2^exp) to the shortest number of digits
that will let the original floating point value be precisely reconstructed.
If we chop a at nd digits, should we round up?
small returns the string for an i with 0 <= i < nSmalls.
special returns the floating-point value for the special,
possibly signed floating-point representations inf, infinity,
and NaN. The result is ok if a prefix of s contains one
of these representations and n is the length of that prefix.
The character case is ignored.
trim trailing zeros from number.
(They are meaningless; the decimal point is tracked
independent of the number of digits.)
trimZeros trims trailing zeros from x.
It finds the largest p such that x % 10^p == 0
and then returns x / 10^p, p.
This is here for reference and tested, because it is an optimization
used by other ftoa algorithms, but in our implementations it has
never been benchmarked to be faster than trimming zeros after
formatting into decimal bytes.
uadd128 returns the full 128 bits of u + n.
umul128 returns the 128-bit product x*y.
umul128Upper64 returns the upper 64 bits (out of 128 bits) of x * y.
umul192 returns the 192-bit product x*y in three uint64s.
umul192Lower128 returns the lower 128 bits (out of 192 bits) of x * y.
umul192Upper128 returns the upper 128 bits (out of 192 bits) of x * y.
umul64 returns the full 64 bits of x * y.
umul96Lower64 returns the lower 64 bits (out of 96 bits) of x * y.
umul96Upper64 returns the upper 64 bits (out of 96 bits) of x * y.
underscoreOK reports whether the underscores in s are allowed.
Checking them in this one function lets all the parsers skip over them simply.
Underscore must appear only between digits or between a base prefix and a digit.
Package-Level Variables (total 10, none are exported)
div5Tab[p-1] is the multiplicative inverse of 5^p and maxUint64/5^p.
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.