blob: 9ff5d1056aa2164171eac806e774629f55f7d697 [file] [log] [blame]
Russ Cox079c00a2008-11-17 12:34:03 -08001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Binary to decimal floating point conversion.
6// Algorithm:
7// 1) store mantissa in multiprecision decimal
8// 2) shift decimal by exponent
9// 3) read digits out & format
10
11package strconv
12
Russ Cox3b864e42009-08-12 13:18:37 -070013import "math"
Russ Cox079c00a2008-11-17 12:34:03 -080014
15// TODO: move elsewhere?
Russ Cox8a7cbad2009-01-15 17:22:17 -080016type floatInfo struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080017 mantbits uint
18 expbits uint
19 bias int
Russ Cox079c00a2008-11-17 12:34:03 -080020}
Russ Cox094f1d52009-10-08 15:14:54 -070021
22var float32info = floatInfo{23, 8, -127}
23var float64info = floatInfo{52, 11, -1023}
Russ Cox079c00a2008-11-17 12:34:03 -080024
Russ Coxefbeaed2011-12-05 15:48:21 -050025// FormatFloat converts the floating-point number f to a string,
26// according to the format fmt and precision prec. It rounds the
27// result assuming that the original was obtained from a floating-point
28// value of bitSize bits (32 for float32, 64 for float64).
Russ Cox5bf0fbe2009-03-05 15:29:04 -080029//
30// The format fmt is one of
31// 'b' (-ddddp±ddd, a binary exponent),
32// 'e' (-d.dddde±dd, a decimal exponent),
Russ Cox71793d42011-01-04 13:13:12 -050033// 'E' (-d.ddddE±dd, a decimal exponent),
34// 'f' (-ddd.dddd, no exponent),
35// 'g' ('e' for large exponents, 'f' otherwise), or
36// 'G' ('E' for large exponents, 'f' otherwise).
Russ Cox5bf0fbe2009-03-05 15:29:04 -080037//
38// The precision prec controls the number of digits
Russ Cox71793d42011-01-04 13:13:12 -050039// (excluding the exponent) printed by the 'e', 'E', 'f', 'g', and 'G' formats.
40// For 'e', 'E', and 'f' it is the number of digits after the decimal point.
41// For 'g' and 'G' it is the total number of digits.
Russ Cox5bf0fbe2009-03-05 15:29:04 -080042// The special precision -1 uses the smallest number of digits
Ian Lance Taylor36397812011-12-19 20:57:32 -080043// necessary such that ParseFloat will return f exactly.
Robert Griesemer127b5a62011-12-07 10:30:27 -080044func FormatFloat(f float64, fmt byte, prec, bitSize int) string {
Robert Griesemer2e3bd892011-12-07 14:45:45 -080045 return string(genericFtoa(make([]byte, 0, max(prec+4, 24)), f, fmt, prec, bitSize))
Russ Cox079c00a2008-11-17 12:34:03 -080046}
47
Russ Coxefbeaed2011-12-05 15:48:21 -050048// AppendFloat appends the string form of the floating-point number f,
49// as generated by FormatFloat, to dst and returns the extended buffer.
Martin Möhrmannd5b5d672015-02-13 15:59:54 +010050func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte {
Robert Griesemer127b5a62011-12-07 10:30:27 -080051 return genericFtoa(dst, f, fmt, prec, bitSize)
Russ Cox2d3e47c2010-06-18 22:43:37 -070052}
53
Robert Griesemer127b5a62011-12-07 10:30:27 -080054func genericFtoa(dst []byte, val float64, fmt byte, prec, bitSize int) []byte {
55 var bits uint64
56 var flt *floatInfo
57 switch bitSize {
58 case 32:
59 bits = uint64(math.Float32bits(float32(val)))
60 flt = &float32info
61 case 64:
62 bits = math.Float64bits(val)
63 flt = &float64info
64 default:
65 panic("strconv: illegal AppendFloat/FormatFloat bitSize")
66 }
67
Rob Pikea67292f2011-02-11 16:06:04 -080068 neg := bits>>(flt.expbits+flt.mantbits) != 0
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080069 exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1)
70 mant := bits & (uint64(1)<<flt.mantbits - 1)
Russ Cox079c00a2008-11-17 12:34:03 -080071
72 switch exp {
Robert Griesemer3bb00322009-11-09 21:23:52 -080073 case 1<<flt.expbits - 1:
Russ Cox079c00a2008-11-17 12:34:03 -080074 // Inf, NaN
Robert Griesemer127b5a62011-12-07 10:30:27 -080075 var s string
76 switch {
77 case mant != 0:
78 s = "NaN"
79 case neg:
80 s = "-Inf"
81 default:
82 s = "+Inf"
Russ Cox079c00a2008-11-17 12:34:03 -080083 }
Robert Griesemer127b5a62011-12-07 10:30:27 -080084 return append(dst, s...)
Russ Cox079c00a2008-11-17 12:34:03 -080085
86 case 0:
87 // denormalized
Robert Griesemer40621d52009-11-09 12:07:39 -080088 exp++
Russ Cox079c00a2008-11-17 12:34:03 -080089
90 default:
91 // add implicit top bit
Robert Griesemer40621d52009-11-09 12:07:39 -080092 mant |= uint64(1) << flt.mantbits
Russ Cox079c00a2008-11-17 12:34:03 -080093 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080094 exp += flt.bias
Russ Cox079c00a2008-11-17 12:34:03 -080095
96 // Pick off easy binary format.
97 if fmt == 'b' {
Robert Griesemer127b5a62011-12-07 10:30:27 -080098 return fmtB(dst, neg, mant, exp, flt)
Russ Cox079c00a2008-11-17 12:34:03 -080099 }
100
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200101 if !optimize {
102 return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
103 }
Rémy Oudompheng0575cd92012-01-13 23:24:33 +0100104
Rémy Oudomphengff034822012-08-05 20:30:13 +0200105 var digs decimalSlice
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200106 ok := false
107 // Negative precision means "only as much as needed to be exact."
108 shortest := prec < 0
Rémy Oudompheng0575cd92012-01-13 23:24:33 +0100109 if shortest {
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200110 // Try Grisu3 algorithm.
111 f := new(extFloat)
112 lower, upper := f.AssignComputeBounds(mant, exp, neg, flt)
113 var buf [32]byte
114 digs.d = buf[:]
115 ok = f.ShortestDecimal(&digs, &lower, &upper)
Rémy Oudompheng0575cd92012-01-13 23:24:33 +0100116 if !ok {
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200117 return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
Rémy Oudompheng0575cd92012-01-13 23:24:33 +0100118 }
119 // Precision for shortest representation mode.
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200120 switch fmt {
121 case 'e', 'E':
Robert Griesemer81a3f292015-02-04 15:07:04 -0800122 prec = max(digs.nd-1, 0)
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200123 case 'f':
124 prec = max(digs.nd-digs.dp, 0)
125 case 'g', 'G':
126 prec = digs.nd
127 }
128 } else if fmt != 'f' {
129 // Fixed number of digits.
130 digits := prec
131 switch fmt {
132 case 'e', 'E':
133 digits++
134 case 'g', 'G':
135 if prec == 0 {
136 prec = 1
Rémy Oudompheng0575cd92012-01-13 23:24:33 +0100137 }
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200138 digits = prec
139 }
140 if digits <= 15 {
141 // try fast algorithm when the number of digits is reasonable.
142 var buf [24]byte
143 digs.d = buf[:]
144 f := extFloat{mant, exp - int(flt.mantbits), neg}
145 ok = f.FixedDecimal(&digs, digits)
146 }
147 }
148 if !ok {
149 return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
150 }
151 return formatDigits(dst, shortest, neg, digs, prec, fmt)
152}
153
154// bigFtoa uses multiprecision computations to format a float.
155func bigFtoa(dst []byte, prec int, fmt byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
156 d := new(decimal)
157 d.Assign(mant)
158 d.Shift(exp - int(flt.mantbits))
159 var digs decimalSlice
160 shortest := prec < 0
161 if shortest {
162 roundShortest(d, mant, exp, flt)
163 digs = decimalSlice{d: d.d[:], nd: d.nd, dp: d.dp}
164 // Precision for shortest representation mode.
165 switch fmt {
166 case 'e', 'E':
167 prec = digs.nd - 1
168 case 'f':
169 prec = max(digs.nd-digs.dp, 0)
170 case 'g', 'G':
171 prec = digs.nd
Russ Cox079c00a2008-11-17 12:34:03 -0800172 }
173 } else {
Rémy Oudompheng0575cd92012-01-13 23:24:33 +0100174 // Round appropriately.
Russ Cox079c00a2008-11-17 12:34:03 -0800175 switch fmt {
Russ Coxa843b452009-08-31 16:38:30 -0700176 case 'e', 'E':
Robert Griesemer3bb00322009-11-09 21:23:52 -0800177 d.Round(prec + 1)
Russ Cox079c00a2008-11-17 12:34:03 -0800178 case 'f':
Robert Griesemer40621d52009-11-09 12:07:39 -0800179 d.Round(d.dp + prec)
Russ Coxa843b452009-08-31 16:38:30 -0700180 case 'g', 'G':
Russ Cox079c00a2008-11-17 12:34:03 -0800181 if prec == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800182 prec = 1
Russ Cox079c00a2008-11-17 12:34:03 -0800183 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800184 d.Round(prec)
Russ Cox079c00a2008-11-17 12:34:03 -0800185 }
Rémy Oudomphengff034822012-08-05 20:30:13 +0200186 digs = decimalSlice{d: d.d[:], nd: d.nd, dp: d.dp}
Russ Cox079c00a2008-11-17 12:34:03 -0800187 }
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200188 return formatDigits(dst, shortest, neg, digs, prec, fmt)
189}
Russ Cox079c00a2008-11-17 12:34:03 -0800190
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200191func formatDigits(dst []byte, shortest bool, neg bool, digs decimalSlice, prec int, fmt byte) []byte {
Russ Cox079c00a2008-11-17 12:34:03 -0800192 switch fmt {
Russ Coxa843b452009-08-31 16:38:30 -0700193 case 'e', 'E':
Rémy Oudomphengff034822012-08-05 20:30:13 +0200194 return fmtE(dst, neg, digs, prec, fmt)
Russ Cox079c00a2008-11-17 12:34:03 -0800195 case 'f':
Rémy Oudomphengff034822012-08-05 20:30:13 +0200196 return fmtF(dst, neg, digs, prec)
Russ Coxa843b452009-08-31 16:38:30 -0700197 case 'g', 'G':
Rob Pike21f8ae82010-06-29 16:39:17 -0700198 // trailing fractional zeros in 'e' form will be trimmed.
199 eprec := prec
Rémy Oudomphengff034822012-08-05 20:30:13 +0200200 if eprec > digs.nd && digs.nd >= digs.dp {
201 eprec = digs.nd
Russ Cox079c00a2008-11-17 12:34:03 -0800202 }
203 // %e is used if the exponent from the conversion
204 // is less than -4 or greater than or equal to the precision.
Russ Cox0e198da2008-11-23 17:27:44 -0800205 // if precision was the shortest possible, use precision 6 for this decision.
Russ Cox0e198da2008-11-23 17:27:44 -0800206 if shortest {
Robert Griesemer40621d52009-11-09 12:07:39 -0800207 eprec = 6
Russ Cox0e198da2008-11-23 17:27:44 -0800208 }
Rémy Oudomphengff034822012-08-05 20:30:13 +0200209 exp := digs.dp - 1
Russ Cox0e198da2008-11-23 17:27:44 -0800210 if exp < -4 || exp >= eprec {
Rémy Oudomphengff034822012-08-05 20:30:13 +0200211 if prec > digs.nd {
212 prec = digs.nd
Rob Pike21f8ae82010-06-29 16:39:17 -0700213 }
Rémy Oudomphengff034822012-08-05 20:30:13 +0200214 return fmtE(dst, neg, digs, prec-1, fmt+'e'-'g')
Russ Cox079c00a2008-11-17 12:34:03 -0800215 }
Rémy Oudomphengff034822012-08-05 20:30:13 +0200216 if prec > digs.dp {
217 prec = digs.nd
Rob Pike21f8ae82010-06-29 16:39:17 -0700218 }
Rémy Oudomphengff034822012-08-05 20:30:13 +0200219 return fmtF(dst, neg, digs, max(prec-digs.dp, 0))
Russ Cox079c00a2008-11-17 12:34:03 -0800220 }
221
Robert Griesemer127b5a62011-12-07 10:30:27 -0800222 // unknown format
223 return append(dst, '%', fmt)
Russ Cox079c00a2008-11-17 12:34:03 -0800224}
225
Robert Griesemeracd82d52015-05-27 12:08:38 -0700226// roundShortest rounds d (= mant * 2^exp) to the shortest number of digits
227// that will let the original floating point value be precisely reconstructed.
Russ Cox8a7cbad2009-01-15 17:22:17 -0800228func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) {
Russ Coxcf9b7f72008-11-19 12:50:34 -0800229 // If mantissa is zero, the number is zero; stop now.
230 if mant == 0 {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800231 d.nd = 0
232 return
Russ Coxcf9b7f72008-11-19 12:50:34 -0800233 }
234
Russ Cox079c00a2008-11-17 12:34:03 -0800235 // Compute upper and lower such that any decimal number
236 // between upper and lower (possibly inclusive)
237 // will round to the original floating point number.
238
Rémy Oudompheng37cd1652012-01-12 11:34:06 -0800239 // We may see at once that the number is already shortest.
240 //
241 // Suppose d is not denormal, so that 2^exp <= d < 10^dp.
242 // The closest shorter number is at least 10^(dp-nd) away.
243 // The lower/upper bounds computed below are at distance
244 // at most 2^(exp-mantbits).
245 //
246 // So the number is already shortest if 10^(dp-nd) > 2^(exp-mantbits),
247 // or equivalently log2(10)*(dp-nd) > exp-mantbits.
248 // It is true if 332/100*(dp-nd) >= exp-mantbits (log2(10) > 3.32).
249 minexp := flt.bias + 1 // minimum possible exponent
250 if exp > minexp && 332*(d.dp-d.nd) >= 100*(exp-int(flt.mantbits)) {
251 // The number is already shortest.
252 return
253 }
254
Russ Cox079c00a2008-11-17 12:34:03 -0800255 // d = mant << (exp - mantbits)
256 // Next highest floating point number is mant+1 << exp-mantbits.
Shenghou Mad1ef9b52012-12-19 03:04:09 +0800257 // Our upper bound is halfway between, mant*2+1 << exp-mantbits-1.
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500258 upper := new(decimal)
259 upper.Assign(mant*2 + 1)
Russ Coxcb51fdc2011-08-25 17:54:14 -0400260 upper.Shift(exp - int(flt.mantbits) - 1)
Russ Cox079c00a2008-11-17 12:34:03 -0800261
262 // d = mant << (exp - mantbits)
263 // Next lowest floating point number is mant-1 << exp-mantbits,
264 // unless mant-1 drops the significant bit and exp is not the minimum exp,
265 // in which case the next lowest is mant*2-1 << exp-mantbits-1.
266 // Either way, call it mantlo << explo-mantbits.
Shenghou Mad1ef9b52012-12-19 03:04:09 +0800267 // Our lower bound is halfway between, mantlo*2+1 << explo-mantbits-1.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800268 var mantlo uint64
269 var explo int
Robert Griesemer3bb00322009-11-09 21:23:52 -0800270 if mant > 1<<flt.mantbits || exp == minexp {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800271 mantlo = mant - 1
272 explo = exp
Russ Cox079c00a2008-11-17 12:34:03 -0800273 } else {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800274 mantlo = mant*2 - 1
275 explo = exp - 1
Russ Cox079c00a2008-11-17 12:34:03 -0800276 }
Russ Cox0ed5e6a2011-11-15 12:17:25 -0500277 lower := new(decimal)
278 lower.Assign(mantlo*2 + 1)
Russ Coxcb51fdc2011-08-25 17:54:14 -0400279 lower.Shift(explo - int(flt.mantbits) - 1)
Russ Cox079c00a2008-11-17 12:34:03 -0800280
281 // The upper and lower bounds are possible outputs only if
282 // the original mantissa is even, so that IEEE round-to-even
283 // would round to the original mantissa and not the neighbors.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800284 inclusive := mant%2 == 0
Russ Cox079c00a2008-11-17 12:34:03 -0800285
286 // Now we can figure out the minimum number of digits required.
287 // Walk along until d has distinguished itself from upper and lower.
288 for i := 0; i < d.nd; i++ {
Robert Griesemer0befa472015-09-23 09:52:21 -0700289 l := byte('0') // lower digit
Russ Cox079c00a2008-11-17 12:34:03 -0800290 if i < lower.nd {
Robert Griesemer40621d52009-11-09 12:07:39 -0800291 l = lower.d[i]
Russ Cox079c00a2008-11-17 12:34:03 -0800292 }
Robert Griesemer0befa472015-09-23 09:52:21 -0700293 m := d.d[i] // middle digit
294 u := byte('0') // upper digit
Russ Cox079c00a2008-11-17 12:34:03 -0800295 if i < upper.nd {
Robert Griesemer40621d52009-11-09 12:07:39 -0800296 u = upper.d[i]
Russ Cox079c00a2008-11-17 12:34:03 -0800297 }
298
299 // Okay to round down (truncate) if lower has a different digit
Robert Griesemer0befa472015-09-23 09:52:21 -0700300 // or if lower is inclusive and is exactly the result of rounding
301 // down (i.e., and we have reached the final digit of lower).
302 okdown := l != m || inclusive && i+1 == lower.nd
Russ Cox079c00a2008-11-17 12:34:03 -0800303
Robert Griesemer0befa472015-09-23 09:52:21 -0700304 // Okay to round up if upper has a different digit and either upper
305 // is inclusive or upper is bigger than the result of rounding up.
Russ Cox6f77cd22012-01-12 11:32:28 -0800306 okup := m != u && (inclusive || m+1 < u || i+1 < upper.nd)
Russ Cox079c00a2008-11-17 12:34:03 -0800307
308 // If it's okay to do either, then round to the nearest one.
309 // If it's okay to do only one, do it.
310 switch {
311 case okdown && okup:
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800312 d.Round(i + 1)
313 return
Russ Cox079c00a2008-11-17 12:34:03 -0800314 case okdown:
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800315 d.RoundDown(i + 1)
316 return
Russ Cox079c00a2008-11-17 12:34:03 -0800317 case okup:
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800318 d.RoundUp(i + 1)
319 return
Russ Cox079c00a2008-11-17 12:34:03 -0800320 }
321 }
322}
323
Rémy Oudomphengff034822012-08-05 20:30:13 +0200324type decimalSlice struct {
325 d []byte
326 nd, dp int
327 neg bool
328}
329
Russ Cox079c00a2008-11-17 12:34:03 -0800330// %e: -d.ddddde±dd
Rémy Oudomphengff034822012-08-05 20:30:13 +0200331func fmtE(dst []byte, neg bool, d decimalSlice, prec int, fmt byte) []byte {
Russ Cox079c00a2008-11-17 12:34:03 -0800332 // sign
333 if neg {
Robert Griesemer127b5a62011-12-07 10:30:27 -0800334 dst = append(dst, '-')
Russ Cox079c00a2008-11-17 12:34:03 -0800335 }
336
337 // first digit
Robert Griesemer127b5a62011-12-07 10:30:27 -0800338 ch := byte('0')
339 if d.nd != 0 {
340 ch = d.d[0]
Russ Cox079c00a2008-11-17 12:34:03 -0800341 }
Robert Griesemer127b5a62011-12-07 10:30:27 -0800342 dst = append(dst, ch)
Russ Cox079c00a2008-11-17 12:34:03 -0800343
344 // .moredigits
345 if prec > 0 {
Robert Griesemer127b5a62011-12-07 10:30:27 -0800346 dst = append(dst, '.')
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200347 i := 1
Robert Griesemer81a3f292015-02-04 15:07:04 -0800348 m := min(d.nd, prec+1)
349 if i < m {
350 dst = append(dst, d.d[i:m]...)
351 i = m
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200352 }
Robert Griesemer81a3f292015-02-04 15:07:04 -0800353 for ; i <= prec; i++ {
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200354 dst = append(dst, '0')
Russ Cox079c00a2008-11-17 12:34:03 -0800355 }
356 }
357
358 // e±
Robert Griesemer127b5a62011-12-07 10:30:27 -0800359 dst = append(dst, fmt)
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800360 exp := d.dp - 1
361 if d.nd == 0 { // special case: 0 has exponent 0
Robert Griesemer40621d52009-11-09 12:07:39 -0800362 exp = 0
Russ Cox079c00a2008-11-17 12:34:03 -0800363 }
364 if exp < 0 {
Robert Griesemer127b5a62011-12-07 10:30:27 -0800365 ch = '-'
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800366 exp = -exp
Russ Cox079c00a2008-11-17 12:34:03 -0800367 } else {
Robert Griesemer127b5a62011-12-07 10:30:27 -0800368 ch = '+'
Russ Cox079c00a2008-11-17 12:34:03 -0800369 }
Robert Griesemer127b5a62011-12-07 10:30:27 -0800370 dst = append(dst, ch)
Russ Cox079c00a2008-11-17 12:34:03 -0800371
Robert Griesemer81a3f292015-02-04 15:07:04 -0800372 // dd or ddd
373 switch {
374 case exp < 10:
375 dst = append(dst, '0', byte(exp)+'0')
376 case exp < 100:
377 dst = append(dst, byte(exp/10)+'0', byte(exp%10)+'0')
378 default:
379 dst = append(dst, byte(exp/100)+'0', byte(exp/10)%10+'0', byte(exp%10)+'0')
Russ Cox079c00a2008-11-17 12:34:03 -0800380 }
Robert Griesemer127b5a62011-12-07 10:30:27 -0800381
Rémy Oudomphengc1c02792012-09-01 16:31:46 +0200382 return dst
Russ Cox079c00a2008-11-17 12:34:03 -0800383}
384
385// %f: -ddddddd.ddddd
Rémy Oudomphengff034822012-08-05 20:30:13 +0200386func fmtF(dst []byte, neg bool, d decimalSlice, prec int) []byte {
Russ Cox079c00a2008-11-17 12:34:03 -0800387 // sign
388 if neg {
Robert Griesemer127b5a62011-12-07 10:30:27 -0800389 dst = append(dst, '-')
Russ Cox079c00a2008-11-17 12:34:03 -0800390 }
391
392 // integer, padded with zeros as needed.
393 if d.dp > 0 {
Robert Griesemer81a3f292015-02-04 15:07:04 -0800394 m := min(d.nd, d.dp)
395 dst = append(dst, d.d[:m]...)
396 for ; m < d.dp; m++ {
Robert Griesemer127b5a62011-12-07 10:30:27 -0800397 dst = append(dst, '0')
Russ Cox079c00a2008-11-17 12:34:03 -0800398 }
399 } else {
Robert Griesemer127b5a62011-12-07 10:30:27 -0800400 dst = append(dst, '0')
Russ Cox079c00a2008-11-17 12:34:03 -0800401 }
402
403 // fraction
404 if prec > 0 {
Robert Griesemer127b5a62011-12-07 10:30:27 -0800405 dst = append(dst, '.')
Russ Cox079c00a2008-11-17 12:34:03 -0800406 for i := 0; i < prec; i++ {
Robert Griesemer127b5a62011-12-07 10:30:27 -0800407 ch := byte('0')
408 if j := d.dp + i; 0 <= j && j < d.nd {
409 ch = d.d[j]
Russ Cox079c00a2008-11-17 12:34:03 -0800410 }
Robert Griesemer127b5a62011-12-07 10:30:27 -0800411 dst = append(dst, ch)
Russ Cox079c00a2008-11-17 12:34:03 -0800412 }
413 }
414
Robert Griesemer127b5a62011-12-07 10:30:27 -0800415 return dst
Russ Cox079c00a2008-11-17 12:34:03 -0800416}
417
Martin Möhrmannd5b5d672015-02-13 15:59:54 +0100418// %b: -ddddddddp±ddd
Robert Griesemer127b5a62011-12-07 10:30:27 -0800419func fmtB(dst []byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
Martin Möhrmannd5b5d672015-02-13 15:59:54 +0100420 // sign
Russ Cox079c00a2008-11-17 12:34:03 -0800421 if neg {
Martin Möhrmannd5b5d672015-02-13 15:59:54 +0100422 dst = append(dst, '-')
Russ Cox079c00a2008-11-17 12:34:03 -0800423 }
Martin Möhrmannd5b5d672015-02-13 15:59:54 +0100424
425 // mantissa
426 dst, _ = formatBits(dst, mant, 10, false, true)
427
428 // p
429 dst = append(dst, 'p')
430
431 // ±exponent
432 exp -= int(flt.mantbits)
433 if exp >= 0 {
434 dst = append(dst, '+')
435 }
436 dst, _ = formatBits(dst, uint64(exp), 10, exp < 0, true)
437
438 return dst
Russ Cox079c00a2008-11-17 12:34:03 -0800439}
440
Robert Griesemer81a3f292015-02-04 15:07:04 -0800441func min(a, b int) int {
442 if a < b {
443 return a
444 }
445 return b
446}
447
Russ Cox8a7cbad2009-01-15 17:22:17 -0800448func max(a, b int) int {
Russ Cox079c00a2008-11-17 12:34:03 -0800449 if a > b {
Robert Griesemer40621d52009-11-09 12:07:39 -0800450 return a
Russ Cox079c00a2008-11-17 12:34:03 -0800451 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800452 return b
Russ Cox079c00a2008-11-17 12:34:03 -0800453}