// 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} // 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. func FormatFloat(f float64, fmt byte, prec, bitSize int) string { return string(genericFtoa(make([]byte, 0, max(prec+4, 24)), f, fmt, prec, bitSize)) } // AppendFloat appends the string form of the floating-point number f, // as generated by [FormatFloat], to dst and returns the extended buffer. func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte { return genericFtoa(dst, f, fmt, prec, bitSize) } 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") } neg := bits>>(flt.expbits+flt.mantbits) != 0 exp := int(bits>>flt.mantbits) & (1< digs.nd && digs.nd >= digs.dp { eprec = digs.nd } // %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 } exp := digs.dp - 1 if exp < -4 || exp >= eprec { if prec > digs.nd { prec = digs.nd } return fmtE(dst, neg, digs, prec-1, fmt+'e'-'g') } if prec > digs.dp { prec = digs.nd } return fmtF(dst, neg, digs, max(prec-digs.dp, 0)) } // unknown format return append(dst, '%', fmt) } // roundShortest rounds d (= mant * 2^exp) to the shortest number of digits // that will let the original floating point value be precisely reconstructed. 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. // 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 } // d = mant << (exp - mantbits) // Next highest floating point number is mant+1 << exp-mantbits. // Our upper bound is halfway between, mant*2+1 << exp-mantbits-1. upper := new(decimal) upper.Assign(mant*2 + 1) upper.Shift(exp - int(flt.mantbits) - 1) // 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. // Our lower bound is halfway between, mantlo*2+1 << explo-mantbits-1. var mantlo uint64 var explo int if mant > 1<= d.nd { break } li := ui - upper.dp + lower.dp l := byte('0') // lower digit if li >= 0 && li < lower.nd { l = lower.d[li] } m := byte('0') // middle digit if mi >= 0 { m = d.d[mi] } u := byte('0') // upper digit if ui < upper.nd { u = upper.d[ui] } // Okay to round down (truncate) if lower has a different digit // 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 && li+1 == lower.nd switch { case upperdelta == 0 && m+1 < u: // Example: // m = 12345xxx // u = 12347xxx upperdelta = 2 case upperdelta == 0 && m != u: // Example: // m = 12345xxx // u = 12346xxx upperdelta = 1 case upperdelta == 1 && (m != '9' || u != '0'): // Example: // m = 1234598x // u = 1234600x upperdelta = 2 } // 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. okup := upperdelta > 0 && (inclusive || upperdelta > 1 || ui+1 < upper.nd) // 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(mi + 1) return case okdown: d.RoundDown(mi + 1) return case okup: d.RoundUp(mi + 1) return } } } type decimalSlice struct { d []byte nd, dp int } // %e: -d.ddddde±dd func fmtE(dst []byte, neg bool, d decimalSlice, prec int, fmt byte) []byte { // sign if neg { dst = append(dst, '-') } // first digit ch := byte('0') if d.nd != 0 { ch = d.d[0] } dst = append(dst, ch) // .moredigits if prec > 0 { dst = append(dst, '.') i := 1 m := min(d.nd, prec+1) if i < m { dst = append(dst, d.d[i:m]...) i = m } for ; i <= prec; i++ { dst = append(dst, '0') } } // e± dst = append(dst, fmt) exp := d.dp - 1 if d.nd == 0 { // special case: 0 has exponent 0 exp = 0 } if exp < 0 { ch = '-' exp = -exp } else { ch = '+' } dst = append(dst, ch) // 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') } return dst } // %f: -ddddddd.ddddd func fmtF(dst []byte, neg bool, d decimalSlice, prec int) []byte { // sign if neg { dst = append(dst, '-') } // integer, padded with zeros as needed. if d.dp > 0 { m := min(d.nd, d.dp) dst = append(dst, d.d[:m]...) for ; m < d.dp; m++ { dst = append(dst, '0') } } else { dst = append(dst, '0') } // fraction if prec > 0 { dst = append(dst, '.') for i := 0; i < prec; i++ { ch := byte('0') if j := d.dp + i; 0 <= j && j < d.nd { ch = d.d[j] } dst = append(dst, ch) } } return dst } // %b: -ddddddddp±ddd func fmtB(dst []byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte { // sign if neg { dst = append(dst, '-') } // mantissa dst, _ = formatBits(dst, mant, 10, false, true) // p dst = append(dst, 'p') // ±exponent exp -= int(flt.mantbits) if exp >= 0 { dst = append(dst, '+') } dst, _ = formatBits(dst, uint64(exp), 10, exp < 0, true) return dst } // %x: -0x1.yyyyyyyyp±ddd or -0x0p+0. (y is hex digit, d is decimal digit) func fmtX(dst []byte, prec int, fmt byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte { if mant == 0 { exp = 0 } // Shift digits so leading 1 (if any) is at bit 1<<60. mant <<= 60 - flt.mantbits for mant != 0 && mant&(1<<60) == 0 { mant <<= 1 exp-- } // Round if requested. if prec >= 0 && prec < 15 { shift := uint(prec * 4) extra := (mant << shift) & (1<<60 - 1) mant >>= 60 - shift if extra|(mant&1) > 1<<59 { mant++ } mant <<= 60 - shift if mant&(1<<61) != 0 { // Wrapped around. mant >>= 1 exp++ } } hex := lowerhex if fmt == 'X' { hex = upperhex } // sign, 0x, leading digit if neg { dst = append(dst, '-') } dst = append(dst, '0', fmt, '0'+byte((mant>>60)&1)) // .fraction mant <<= 4 // remove leading 0 or 1 if prec < 0 && mant != 0 { dst = append(dst, '.') for mant != 0 { dst = append(dst, hex[(mant>>60)&15]) mant <<= 4 } } else if prec > 0 { dst = append(dst, '.') for i := 0; i < prec; i++ { dst = append(dst, hex[(mant>>60)&15]) mant <<= 4 } } // p± ch := byte('P') if fmt == lower(fmt) { ch = 'p' } dst = append(dst, ch) if exp < 0 { ch = '-' exp = -exp } else { ch = '+' } dst = append(dst, ch) // dd or ddd or dddd switch { case exp < 100: dst = append(dst, byte(exp/10)+'0', byte(exp%10)+'0') case exp < 1000: dst = append(dst, byte(exp/100)+'0', byte((exp/10)%10)+'0', byte(exp%10)+'0') default: dst = append(dst, byte(exp/1000)+'0', byte(exp/100)%10+'0', byte((exp/10)%10)+'0', byte(exp%10)+'0') } return dst }