2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Binary to decimal floating point conversion.
|
|
|
|
// Algorithm:
|
|
|
|
// 1) store mantissa in multiprecision decimal
|
|
|
|
// 2) shift decimal by exponent
|
|
|
|
// 3) read digits out & format
|
|
|
|
|
|
|
|
package strconv
|
|
|
|
|
|
|
|
import "math"
|
|
|
|
|
|
|
|
// TODO: move elsewhere?
|
|
|
|
type floatInfo struct {
|
|
|
|
mantbits uint
|
|
|
|
expbits uint
|
|
|
|
bias int
|
|
|
|
}
|
|
|
|
|
|
|
|
var float32info = floatInfo{23, 8, -127}
|
|
|
|
var float64info = floatInfo{52, 11, -1023}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// FormatFloat converts the floating-point number f to a string,
|
2016-07-22 20:15:38 +02:00
|
|
|
// according to the format fmt and precision prec. It rounds the
|
2011-12-14 16:41:54 +01:00
|
|
|
// result assuming that the original was obtained from a floating-point
|
|
|
|
// value of bitSize bits (32 for float32, 64 for float64).
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// The format fmt is one of
|
|
|
|
// 'b' (-ddddp±ddd, a binary exponent),
|
|
|
|
// 'e' (-d.dddde±dd, a decimal exponent),
|
2011-01-21 19:19:03 +01:00
|
|
|
// 'E' (-d.ddddE±dd, a decimal exponent),
|
|
|
|
// 'f' (-ddd.dddd, no exponent),
|
|
|
|
// 'g' ('e' for large exponents, 'f' otherwise), or
|
|
|
|
// 'G' ('E' for large exponents, 'f' otherwise).
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
|
|
|
// The precision prec controls the number of digits
|
2011-01-21 19:19:03 +01:00
|
|
|
// (excluding the exponent) printed by the 'e', 'E', 'f', 'g', and 'G' formats.
|
|
|
|
// For 'e', 'E', and 'f' it is the number of digits after the decimal point.
|
|
|
|
// For 'g' and 'G' it is the total number of digits.
|
2010-12-03 05:34:57 +01:00
|
|
|
// The special precision -1 uses the smallest number of digits
|
2012-01-13 06:11:45 +01:00
|
|
|
// necessary such that ParseFloat will return f exactly.
|
2012-01-12 02:31:45 +01:00
|
|
|
func FormatFloat(f float64, fmt byte, prec, bitSize int) string {
|
|
|
|
return string(genericFtoa(make([]byte, 0, max(prec+4, 24)), f, fmt, prec, bitSize))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// AppendFloat appends the string form of the floating-point number f,
|
|
|
|
// as generated by FormatFloat, to dst and returns the extended buffer.
|
2015-10-31 01:59:47 +01:00
|
|
|
func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte {
|
2012-01-12 02:31:45 +01:00
|
|
|
return genericFtoa(dst, f, fmt, prec, bitSize)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
func genericFtoa(dst []byte, val float64, fmt byte, prec, bitSize int) []byte {
|
|
|
|
var bits uint64
|
|
|
|
var flt *floatInfo
|
|
|
|
switch bitSize {
|
|
|
|
case 32:
|
|
|
|
bits = uint64(math.Float32bits(float32(val)))
|
|
|
|
flt = &float32info
|
|
|
|
case 64:
|
|
|
|
bits = math.Float64bits(val)
|
|
|
|
flt = &float64info
|
|
|
|
default:
|
|
|
|
panic("strconv: illegal AppendFloat/FormatFloat bitSize")
|
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
neg := bits>>(flt.expbits+flt.mantbits) != 0
|
2010-12-03 05:34:57 +01:00
|
|
|
exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1)
|
|
|
|
mant := bits & (uint64(1)<<flt.mantbits - 1)
|
|
|
|
|
|
|
|
switch exp {
|
|
|
|
case 1<<flt.expbits - 1:
|
|
|
|
// Inf, NaN
|
2012-01-12 02:31:45 +01:00
|
|
|
var s string
|
|
|
|
switch {
|
|
|
|
case mant != 0:
|
|
|
|
s = "NaN"
|
|
|
|
case neg:
|
|
|
|
s = "-Inf"
|
|
|
|
default:
|
|
|
|
s = "+Inf"
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-01-12 02:31:45 +01:00
|
|
|
return append(dst, s...)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
case 0:
|
|
|
|
// denormalized
|
|
|
|
exp++
|
|
|
|
|
|
|
|
default:
|
|
|
|
// add implicit top bit
|
|
|
|
mant |= uint64(1) << flt.mantbits
|
|
|
|
}
|
|
|
|
exp += flt.bias
|
|
|
|
|
|
|
|
// Pick off easy binary format.
|
|
|
|
if fmt == 'b' {
|
2012-01-12 02:31:45 +01:00
|
|
|
return fmtB(dst, neg, mant, exp, flt)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
if !optimize {
|
|
|
|
return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
|
|
|
|
}
|
|
|
|
|
|
|
|
var digs decimalSlice
|
|
|
|
ok := false
|
2010-12-03 05:34:57 +01:00
|
|
|
// Negative precision means "only as much as needed to be exact."
|
2012-01-25 21:56:26 +01:00
|
|
|
shortest := prec < 0
|
|
|
|
if shortest {
|
2012-10-23 06:31:11 +02:00
|
|
|
// Try Grisu3 algorithm.
|
|
|
|
f := new(extFloat)
|
|
|
|
lower, upper := f.AssignComputeBounds(mant, exp, neg, flt)
|
|
|
|
var buf [32]byte
|
|
|
|
digs.d = buf[:]
|
|
|
|
ok = f.ShortestDecimal(&digs, &lower, &upper)
|
2012-01-25 21:56:26 +01:00
|
|
|
if !ok {
|
2012-10-23 06:31:11 +02:00
|
|
|
return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
|
2012-01-25 21:56:26 +01:00
|
|
|
}
|
|
|
|
// Precision for shortest representation mode.
|
2012-10-23 06:31:11 +02:00
|
|
|
switch fmt {
|
|
|
|
case 'e', 'E':
|
2015-10-31 01:59:47 +01:00
|
|
|
prec = max(digs.nd-1, 0)
|
2012-10-23 06:31:11 +02:00
|
|
|
case 'f':
|
|
|
|
prec = max(digs.nd-digs.dp, 0)
|
|
|
|
case 'g', 'G':
|
|
|
|
prec = digs.nd
|
|
|
|
}
|
|
|
|
} else if fmt != 'f' {
|
|
|
|
// Fixed number of digits.
|
|
|
|
digits := prec
|
|
|
|
switch fmt {
|
|
|
|
case 'e', 'E':
|
|
|
|
digits++
|
|
|
|
case 'g', 'G':
|
|
|
|
if prec == 0 {
|
|
|
|
prec = 1
|
2012-01-25 21:56:26 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
digits = prec
|
|
|
|
}
|
|
|
|
if digits <= 15 {
|
|
|
|
// try fast algorithm when the number of digits is reasonable.
|
|
|
|
var buf [24]byte
|
|
|
|
digs.d = buf[:]
|
|
|
|
f := extFloat{mant, exp - int(flt.mantbits), neg}
|
|
|
|
ok = f.FixedDecimal(&digs, digits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
|
|
|
|
}
|
|
|
|
return formatDigits(dst, shortest, neg, digs, prec, fmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bigFtoa uses multiprecision computations to format a float.
|
|
|
|
func bigFtoa(dst []byte, prec int, fmt byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
|
|
|
|
d := new(decimal)
|
|
|
|
d.Assign(mant)
|
|
|
|
d.Shift(exp - int(flt.mantbits))
|
|
|
|
var digs decimalSlice
|
|
|
|
shortest := prec < 0
|
|
|
|
if shortest {
|
|
|
|
roundShortest(d, mant, exp, flt)
|
|
|
|
digs = decimalSlice{d: d.d[:], nd: d.nd, dp: d.dp}
|
|
|
|
// Precision for shortest representation mode.
|
|
|
|
switch fmt {
|
|
|
|
case 'e', 'E':
|
|
|
|
prec = digs.nd - 1
|
|
|
|
case 'f':
|
|
|
|
prec = max(digs.nd-digs.dp, 0)
|
|
|
|
case 'g', 'G':
|
|
|
|
prec = digs.nd
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-25 21:56:26 +01:00
|
|
|
// Round appropriately.
|
2010-12-03 05:34:57 +01:00
|
|
|
switch fmt {
|
|
|
|
case 'e', 'E':
|
|
|
|
d.Round(prec + 1)
|
|
|
|
case 'f':
|
|
|
|
d.Round(d.dp + prec)
|
|
|
|
case 'g', 'G':
|
|
|
|
if prec == 0 {
|
|
|
|
prec = 1
|
|
|
|
}
|
|
|
|
d.Round(prec)
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
digs = decimalSlice{d: d.d[:], nd: d.nd, dp: d.dp}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
return formatDigits(dst, shortest, neg, digs, prec, fmt)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
func formatDigits(dst []byte, shortest bool, neg bool, digs decimalSlice, prec int, fmt byte) []byte {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch fmt {
|
|
|
|
case 'e', 'E':
|
2012-10-23 06:31:11 +02:00
|
|
|
return fmtE(dst, neg, digs, prec, fmt)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'f':
|
2012-10-23 06:31:11 +02:00
|
|
|
return fmtF(dst, neg, digs, prec)
|
2010-12-03 05:34:57 +01:00
|
|
|
case 'g', 'G':
|
|
|
|
// trailing fractional zeros in 'e' form will be trimmed.
|
|
|
|
eprec := prec
|
2012-10-23 06:31:11 +02:00
|
|
|
if eprec > digs.nd && digs.nd >= digs.dp {
|
|
|
|
eprec = digs.nd
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
// %e is used if the exponent from the conversion
|
|
|
|
// is less than -4 or greater than or equal to the precision.
|
|
|
|
// if precision was the shortest possible, use precision 6 for this decision.
|
|
|
|
if shortest {
|
|
|
|
eprec = 6
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
exp := digs.dp - 1
|
2010-12-03 05:34:57 +01:00
|
|
|
if exp < -4 || exp >= eprec {
|
2012-10-23 06:31:11 +02:00
|
|
|
if prec > digs.nd {
|
|
|
|
prec = digs.nd
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
return fmtE(dst, neg, digs, prec-1, fmt+'e'-'g')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
if prec > digs.dp {
|
|
|
|
prec = digs.nd
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
return fmtF(dst, neg, digs, max(prec-digs.dp, 0))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
// unknown format
|
|
|
|
return append(dst, '%', fmt)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// roundShortest rounds d (= mant * 2^exp) to the shortest number of digits
|
|
|
|
// that will let the original floating point value be precisely reconstructed.
|
2010-12-03 05:34:57 +01:00
|
|
|
func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) {
|
|
|
|
// If mantissa is zero, the number is zero; stop now.
|
|
|
|
if mant == 0 {
|
|
|
|
d.nd = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute upper and lower such that any decimal number
|
|
|
|
// between upper and lower (possibly inclusive)
|
|
|
|
// will round to the original floating point number.
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// We may see at once that the number is already shortest.
|
|
|
|
//
|
|
|
|
// Suppose d is not denormal, so that 2^exp <= d < 10^dp.
|
|
|
|
// The closest shorter number is at least 10^(dp-nd) away.
|
|
|
|
// The lower/upper bounds computed below are at distance
|
|
|
|
// at most 2^(exp-mantbits).
|
|
|
|
//
|
|
|
|
// So the number is already shortest if 10^(dp-nd) > 2^(exp-mantbits),
|
|
|
|
// or equivalently log2(10)*(dp-nd) > exp-mantbits.
|
|
|
|
// It is true if 332/100*(dp-nd) >= exp-mantbits (log2(10) > 3.32).
|
|
|
|
minexp := flt.bias + 1 // minimum possible exponent
|
|
|
|
if exp > minexp && 332*(d.dp-d.nd) >= 100*(exp-int(flt.mantbits)) {
|
|
|
|
// The number is already shortest.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// d = mant << (exp - mantbits)
|
|
|
|
// Next highest floating point number is mant+1 << exp-mantbits.
|
2012-12-22 02:15:33 +01:00
|
|
|
// Our upper bound is halfway between, mant*2+1 << exp-mantbits-1.
|
2011-12-13 00:40:51 +01:00
|
|
|
upper := new(decimal)
|
|
|
|
upper.Assign(mant*2 + 1)
|
2011-10-27 01:57:58 +02:00
|
|
|
upper.Shift(exp - int(flt.mantbits) - 1)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// d = mant << (exp - mantbits)
|
|
|
|
// Next lowest floating point number is mant-1 << exp-mantbits,
|
|
|
|
// unless mant-1 drops the significant bit and exp is not the minimum exp,
|
|
|
|
// in which case the next lowest is mant*2-1 << exp-mantbits-1.
|
|
|
|
// Either way, call it mantlo << explo-mantbits.
|
2012-12-22 02:15:33 +01:00
|
|
|
// Our lower bound is halfway between, mantlo*2+1 << explo-mantbits-1.
|
2010-12-03 05:34:57 +01:00
|
|
|
var mantlo uint64
|
|
|
|
var explo int
|
|
|
|
if mant > 1<<flt.mantbits || exp == minexp {
|
|
|
|
mantlo = mant - 1
|
|
|
|
explo = exp
|
|
|
|
} else {
|
|
|
|
mantlo = mant*2 - 1
|
|
|
|
explo = exp - 1
|
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
lower := new(decimal)
|
|
|
|
lower.Assign(mantlo*2 + 1)
|
2011-10-27 01:57:58 +02:00
|
|
|
lower.Shift(explo - int(flt.mantbits) - 1)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// The upper and lower bounds are possible outputs only if
|
|
|
|
// the original mantissa is even, so that IEEE round-to-even
|
|
|
|
// would round to the original mantissa and not the neighbors.
|
|
|
|
inclusive := mant%2 == 0
|
|
|
|
|
|
|
|
// Now we can figure out the minimum number of digits required.
|
|
|
|
// Walk along until d has distinguished itself from upper and lower.
|
|
|
|
for i := 0; i < d.nd; i++ {
|
2016-02-03 22:58:02 +01:00
|
|
|
l := byte('0') // lower digit
|
2010-12-03 05:34:57 +01:00
|
|
|
if i < lower.nd {
|
|
|
|
l = lower.d[i]
|
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
m := d.d[i] // middle digit
|
|
|
|
u := byte('0') // upper digit
|
2010-12-03 05:34:57 +01:00
|
|
|
if i < upper.nd {
|
|
|
|
u = upper.d[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay to round down (truncate) if lower has a different digit
|
2016-02-03 22:58:02 +01:00
|
|
|
// or if lower is inclusive and is exactly the result of rounding
|
|
|
|
// down (i.e., and we have reached the final digit of lower).
|
|
|
|
okdown := l != m || inclusive && i+1 == lower.nd
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
// Okay to round up if upper has a different digit and either upper
|
|
|
|
// is inclusive or upper is bigger than the result of rounding up.
|
2012-01-25 21:56:26 +01:00
|
|
|
okup := m != u && (inclusive || m+1 < u || i+1 < upper.nd)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// If it's okay to do either, then round to the nearest one.
|
|
|
|
// If it's okay to do only one, do it.
|
|
|
|
switch {
|
|
|
|
case okdown && okup:
|
|
|
|
d.Round(i + 1)
|
|
|
|
return
|
|
|
|
case okdown:
|
|
|
|
d.RoundDown(i + 1)
|
|
|
|
return
|
|
|
|
case okup:
|
|
|
|
d.RoundUp(i + 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
type decimalSlice struct {
|
|
|
|
d []byte
|
|
|
|
nd, dp int
|
|
|
|
neg bool
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// %e: -d.ddddde±dd
|
2012-10-23 06:31:11 +02:00
|
|
|
func fmtE(dst []byte, neg bool, d decimalSlice, prec int, fmt byte) []byte {
|
2010-12-03 05:34:57 +01:00
|
|
|
// sign
|
|
|
|
if neg {
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, '-')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// first digit
|
2012-01-12 02:31:45 +01:00
|
|
|
ch := byte('0')
|
|
|
|
if d.nd != 0 {
|
|
|
|
ch = d.d[0]
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, ch)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// .moredigits
|
|
|
|
if prec > 0 {
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, '.')
|
2012-10-23 06:31:11 +02:00
|
|
|
i := 1
|
2015-10-31 01:59:47 +01:00
|
|
|
m := min(d.nd, prec+1)
|
|
|
|
if i < m {
|
|
|
|
dst = append(dst, d.d[i:m]...)
|
|
|
|
i = m
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
for ; i <= prec; i++ {
|
2012-10-23 06:31:11 +02:00
|
|
|
dst = append(dst, '0')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// e±
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, fmt)
|
2010-12-03 05:34:57 +01:00
|
|
|
exp := d.dp - 1
|
|
|
|
if d.nd == 0 { // special case: 0 has exponent 0
|
|
|
|
exp = 0
|
|
|
|
}
|
|
|
|
if exp < 0 {
|
2012-01-12 02:31:45 +01:00
|
|
|
ch = '-'
|
2010-12-03 05:34:57 +01:00
|
|
|
exp = -exp
|
|
|
|
} else {
|
2012-01-12 02:31:45 +01:00
|
|
|
ch = '+'
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, ch)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// dd or ddd
|
|
|
|
switch {
|
|
|
|
case exp < 10:
|
|
|
|
dst = append(dst, '0', byte(exp)+'0')
|
|
|
|
case exp < 100:
|
|
|
|
dst = append(dst, byte(exp/10)+'0', byte(exp%10)+'0')
|
|
|
|
default:
|
|
|
|
dst = append(dst, byte(exp/100)+'0', byte(exp/10)%10+'0', byte(exp%10)+'0')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-01-12 02:31:45 +01:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
return dst
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// %f: -ddddddd.ddddd
|
2012-10-23 06:31:11 +02:00
|
|
|
func fmtF(dst []byte, neg bool, d decimalSlice, prec int) []byte {
|
2010-12-03 05:34:57 +01:00
|
|
|
// sign
|
|
|
|
if neg {
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, '-')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// integer, padded with zeros as needed.
|
|
|
|
if d.dp > 0 {
|
2015-10-31 01:59:47 +01:00
|
|
|
m := min(d.nd, d.dp)
|
|
|
|
dst = append(dst, d.d[:m]...)
|
|
|
|
for ; m < d.dp; m++ {
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, '0')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, '0')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// fraction
|
|
|
|
if prec > 0 {
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, '.')
|
2010-12-03 05:34:57 +01:00
|
|
|
for i := 0; i < prec; i++ {
|
2012-01-12 02:31:45 +01:00
|
|
|
ch := byte('0')
|
|
|
|
if j := d.dp + i; 0 <= j && j < d.nd {
|
|
|
|
ch = d.d[j]
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-01-12 02:31:45 +01:00
|
|
|
dst = append(dst, ch)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
return dst
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// %b: -ddddddddp±ddd
|
2012-01-12 02:31:45 +01:00
|
|
|
func fmtB(dst []byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
|
2015-10-31 01:59:47 +01:00
|
|
|
// sign
|
|
|
|
if neg {
|
|
|
|
dst = append(dst, '-')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
|
|
|
|
// mantissa
|
|
|
|
dst, _ = formatBits(dst, mant, 10, false, true)
|
|
|
|
|
|
|
|
// p
|
|
|
|
dst = append(dst, 'p')
|
|
|
|
|
|
|
|
// ±exponent
|
|
|
|
exp -= int(flt.mantbits)
|
|
|
|
if exp >= 0 {
|
|
|
|
dst = append(dst, '+')
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
dst, _ = formatBits(dst, uint64(exp), 10, exp < 0, true)
|
|
|
|
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
return b
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|