Source file src/strconv/ftoa.go

     1  // 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  
    11  package strconv
    12  
    13  import "math"
    14  
    15  // TODO: move elsewhere?
    16  type floatInfo struct {
    17  	mantbits uint
    18  	expbits  uint
    19  	bias     int
    20  }
    21  
    22  var float32info = floatInfo{23, 8, -127}
    23  var float64info = floatInfo{52, 11, -1023}
    24  
    25  // 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).
    29  //
    30  // The format fmt is one of
    31  // 'b' (-ddddp±ddd, a binary exponent),
    32  // 'e' (-d.dddde±dd, a decimal exponent),
    33  // 'E' (-d.ddddE±dd, a decimal exponent),
    34  // 'f' (-ddd.dddd, no exponent),
    35  // 'g' ('e' for large exponents, 'f' otherwise),
    36  // 'G' ('E' for large exponents, 'f' otherwise),
    37  // 'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
    38  // 'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).
    39  //
    40  // The precision prec controls the number of digits (excluding the exponent)
    41  // printed by the 'e', 'E', 'f', 'g', 'G', 'x', and 'X' formats.
    42  // For 'e', 'E', 'f', 'x', and 'X', it is the number of digits after the decimal point.
    43  // For 'g' and 'G' it is the maximum number of significant digits (trailing
    44  // zeros are removed).
    45  // The special precision -1 uses the smallest number of digits
    46  // necessary such that ParseFloat will return f exactly.
    47  func FormatFloat(f float64, fmt byte, prec, bitSize int) string {
    48  	return string(genericFtoa(make([]byte, 0, max(prec+4, 24)), f, fmt, prec, bitSize))
    49  }
    50  
    51  // AppendFloat appends the string form of the floating-point number f,
    52  // as generated by FormatFloat, to dst and returns the extended buffer.
    53  func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte {
    54  	return genericFtoa(dst, f, fmt, prec, bitSize)
    55  }
    56  
    57  func genericFtoa(dst []byte, val float64, fmt byte, prec, bitSize int) []byte {
    58  	var bits uint64
    59  	var flt *floatInfo
    60  	switch bitSize {
    61  	case 32:
    62  		bits = uint64(math.Float32bits(float32(val)))
    63  		flt = &float32info
    64  	case 64:
    65  		bits = math.Float64bits(val)
    66  		flt = &float64info
    67  	default:
    68  		panic("strconv: illegal AppendFloat/FormatFloat bitSize")
    69  	}
    70  
    71  	neg := bits>>(flt.expbits+flt.mantbits) != 0
    72  	exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1)
    73  	mant := bits & (uint64(1)<<flt.mantbits - 1)
    74  
    75  	switch exp {
    76  	case 1<<flt.expbits - 1:
    77  		// Inf, NaN
    78  		var s string
    79  		switch {
    80  		case mant != 0:
    81  			s = "NaN"
    82  		case neg:
    83  			s = "-Inf"
    84  		default:
    85  			s = "+Inf"
    86  		}
    87  		return append(dst, s...)
    88  
    89  	case 0:
    90  		// denormalized
    91  		exp++
    92  
    93  	default:
    94  		// add implicit top bit
    95  		mant |= uint64(1) << flt.mantbits
    96  	}
    97  	exp += flt.bias
    98  
    99  	// Pick off easy binary, hex formats.
   100  	if fmt == 'b' {
   101  		return fmtB(dst, neg, mant, exp, flt)
   102  	}
   103  	if fmt == 'x' || fmt == 'X' {
   104  		return fmtX(dst, prec, fmt, neg, mant, exp, flt)
   105  	}
   106  
   107  	if !optimize {
   108  		return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
   109  	}
   110  
   111  	var digs decimalSlice
   112  	ok := false
   113  	// Negative precision means "only as much as needed to be exact."
   114  	shortest := prec < 0
   115  	if shortest {
   116  		// Use Ryu algorithm.
   117  		var buf [32]byte
   118  		digs.d = buf[:]
   119  		ryuFtoaShortest(&digs, mant, exp-int(flt.mantbits), flt)
   120  		ok = true
   121  		// Precision for shortest representation mode.
   122  		switch fmt {
   123  		case 'e', 'E':
   124  			prec = max(digs.nd-1, 0)
   125  		case 'f':
   126  			prec = max(digs.nd-digs.dp, 0)
   127  		case 'g', 'G':
   128  			prec = digs.nd
   129  		}
   130  	} else if fmt != 'f' {
   131  		// Fixed number of digits.
   132  		digits := prec
   133  		switch fmt {
   134  		case 'e', 'E':
   135  			digits++
   136  		case 'g', 'G':
   137  			if prec == 0 {
   138  				prec = 1
   139  			}
   140  			digits = prec
   141  		default:
   142  			// Invalid mode.
   143  			digits = 1
   144  		}
   145  		var buf [24]byte
   146  		if bitSize == 32 && digits <= 9 {
   147  			digs.d = buf[:]
   148  			ryuFtoaFixed32(&digs, uint32(mant), exp-int(flt.mantbits), digits)
   149  			ok = true
   150  		} else if digits <= 18 {
   151  			digs.d = buf[:]
   152  			ryuFtoaFixed64(&digs, mant, exp-int(flt.mantbits), digits)
   153  			ok = true
   154  		}
   155  	}
   156  	if !ok {
   157  		return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
   158  	}
   159  	return formatDigits(dst, shortest, neg, digs, prec, fmt)
   160  }
   161  
   162  // bigFtoa uses multiprecision computations to format a float.
   163  func bigFtoa(dst []byte, prec int, fmt byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
   164  	d := new(decimal)
   165  	d.Assign(mant)
   166  	d.Shift(exp - int(flt.mantbits))
   167  	var digs decimalSlice
   168  	shortest := prec < 0
   169  	if shortest {
   170  		roundShortest(d, mant, exp, flt)
   171  		digs = decimalSlice{d: d.d[:], nd: d.nd, dp: d.dp}
   172  		// Precision for shortest representation mode.
   173  		switch fmt {
   174  		case 'e', 'E':
   175  			prec = digs.nd - 1
   176  		case 'f':
   177  			prec = max(digs.nd-digs.dp, 0)
   178  		case 'g', 'G':
   179  			prec = digs.nd
   180  		}
   181  	} else {
   182  		// Round appropriately.
   183  		switch fmt {
   184  		case 'e', 'E':
   185  			d.Round(prec + 1)
   186  		case 'f':
   187  			d.Round(d.dp + prec)
   188  		case 'g', 'G':
   189  			if prec == 0 {
   190  				prec = 1
   191  			}
   192  			d.Round(prec)
   193  		}
   194  		digs = decimalSlice{d: d.d[:], nd: d.nd, dp: d.dp}
   195  	}
   196  	return formatDigits(dst, shortest, neg, digs, prec, fmt)
   197  }
   198  
   199  func formatDigits(dst []byte, shortest bool, neg bool, digs decimalSlice, prec int, fmt byte) []byte {
   200  	switch fmt {
   201  	case 'e', 'E':
   202  		return fmtE(dst, neg, digs, prec, fmt)
   203  	case 'f':
   204  		return fmtF(dst, neg, digs, prec)
   205  	case 'g', 'G':
   206  		// trailing fractional zeros in 'e' form will be trimmed.
   207  		eprec := prec
   208  		if eprec > digs.nd && digs.nd >= digs.dp {
   209  			eprec = digs.nd
   210  		}
   211  		// %e is used if the exponent from the conversion
   212  		// is less than -4 or greater than or equal to the precision.
   213  		// if precision was the shortest possible, use precision 6 for this decision.
   214  		if shortest {
   215  			eprec = 6
   216  		}
   217  		exp := digs.dp - 1
   218  		if exp < -4 || exp >= eprec {
   219  			if prec > digs.nd {
   220  				prec = digs.nd
   221  			}
   222  			return fmtE(dst, neg, digs, prec-1, fmt+'e'-'g')
   223  		}
   224  		if prec > digs.dp {
   225  			prec = digs.nd
   226  		}
   227  		return fmtF(dst, neg, digs, max(prec-digs.dp, 0))
   228  	}
   229  
   230  	// unknown format
   231  	return append(dst, '%', fmt)
   232  }
   233  
   234  // roundShortest rounds d (= mant * 2^exp) to the shortest number of digits
   235  // that will let the original floating point value be precisely reconstructed.
   236  func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) {
   237  	// If mantissa is zero, the number is zero; stop now.
   238  	if mant == 0 {
   239  		d.nd = 0
   240  		return
   241  	}
   242  
   243  	// Compute upper and lower such that any decimal number
   244  	// between upper and lower (possibly inclusive)
   245  	// will round to the original floating point number.
   246  
   247  	// We may see at once that the number is already shortest.
   248  	//
   249  	// Suppose d is not denormal, so that 2^exp <= d < 10^dp.
   250  	// The closest shorter number is at least 10^(dp-nd) away.
   251  	// The lower/upper bounds computed below are at distance
   252  	// at most 2^(exp-mantbits).
   253  	//
   254  	// So the number is already shortest if 10^(dp-nd) > 2^(exp-mantbits),
   255  	// or equivalently log2(10)*(dp-nd) > exp-mantbits.
   256  	// It is true if 332/100*(dp-nd) >= exp-mantbits (log2(10) > 3.32).
   257  	minexp := flt.bias + 1 // minimum possible exponent
   258  	if exp > minexp && 332*(d.dp-d.nd) >= 100*(exp-int(flt.mantbits)) {
   259  		// The number is already shortest.
   260  		return
   261  	}
   262  
   263  	// d = mant << (exp - mantbits)
   264  	// Next highest floating point number is mant+1 << exp-mantbits.
   265  	// Our upper bound is halfway between, mant*2+1 << exp-mantbits-1.
   266  	upper := new(decimal)
   267  	upper.Assign(mant*2 + 1)
   268  	upper.Shift(exp - int(flt.mantbits) - 1)
   269  
   270  	// d = mant << (exp - mantbits)
   271  	// Next lowest floating point number is mant-1 << exp-mantbits,
   272  	// unless mant-1 drops the significant bit and exp is not the minimum exp,
   273  	// in which case the next lowest is mant*2-1 << exp-mantbits-1.
   274  	// Either way, call it mantlo << explo-mantbits.
   275  	// Our lower bound is halfway between, mantlo*2+1 << explo-mantbits-1.
   276  	var mantlo uint64
   277  	var explo int
   278  	if mant > 1<<flt.mantbits || exp == minexp {
   279  		mantlo = mant - 1
   280  		explo = exp
   281  	} else {
   282  		mantlo = mant*2 - 1
   283  		explo = exp - 1
   284  	}
   285  	lower := new(decimal)
   286  	lower.Assign(mantlo*2 + 1)
   287  	lower.Shift(explo - int(flt.mantbits) - 1)
   288  
   289  	// The upper and lower bounds are possible outputs only if
   290  	// the original mantissa is even, so that IEEE round-to-even
   291  	// would round to the original mantissa and not the neighbors.
   292  	inclusive := mant%2 == 0
   293  
   294  	// As we walk the digits we want to know whether rounding up would fall
   295  	// within the upper bound. This is tracked by upperdelta:
   296  	//
   297  	// If upperdelta == 0, the digits of d and upper are the same so far.
   298  	//
   299  	// If upperdelta == 1, we saw a difference of 1 between d and upper on a
   300  	// previous digit and subsequently only 9s for d and 0s for upper.
   301  	// (Thus rounding up may fall outside the bound, if it is exclusive.)
   302  	//
   303  	// If upperdelta == 2, then the difference is greater than 1
   304  	// and we know that rounding up falls within the bound.
   305  	var upperdelta uint8
   306  
   307  	// Now we can figure out the minimum number of digits required.
   308  	// Walk along until d has distinguished itself from upper and lower.
   309  	for ui := 0; ; ui++ {
   310  		// lower, d, and upper may have the decimal points at different
   311  		// places. In this case upper is the longest, so we iterate from
   312  		// ui==0 and start li and mi at (possibly) -1.
   313  		mi := ui - upper.dp + d.dp
   314  		if mi >= d.nd {
   315  			break
   316  		}
   317  		li := ui - upper.dp + lower.dp
   318  		l := byte('0') // lower digit
   319  		if li >= 0 && li < lower.nd {
   320  			l = lower.d[li]
   321  		}
   322  		m := byte('0') // middle digit
   323  		if mi >= 0 {
   324  			m = d.d[mi]
   325  		}
   326  		u := byte('0') // upper digit
   327  		if ui < upper.nd {
   328  			u = upper.d[ui]
   329  		}
   330  
   331  		// Okay to round down (truncate) if lower has a different digit
   332  		// or if lower is inclusive and is exactly the result of rounding
   333  		// down (i.e., and we have reached the final digit of lower).
   334  		okdown := l != m || inclusive && li+1 == lower.nd
   335  
   336  		switch {
   337  		case upperdelta == 0 && m+1 < u:
   338  			// Example:
   339  			// m = 12345xxx
   340  			// u = 12347xxx
   341  			upperdelta = 2
   342  		case upperdelta == 0 && m != u:
   343  			// Example:
   344  			// m = 12345xxx
   345  			// u = 12346xxx
   346  			upperdelta = 1
   347  		case upperdelta == 1 && (m != '9' || u != '0'):
   348  			// Example:
   349  			// m = 1234598x
   350  			// u = 1234600x
   351  			upperdelta = 2
   352  		}
   353  		// Okay to round up if upper has a different digit and either upper
   354  		// is inclusive or upper is bigger than the result of rounding up.
   355  		okup := upperdelta > 0 && (inclusive || upperdelta > 1 || ui+1 < upper.nd)
   356  
   357  		// If it's okay to do either, then round to the nearest one.
   358  		// If it's okay to do only one, do it.
   359  		switch {
   360  		case okdown && okup:
   361  			d.Round(mi + 1)
   362  			return
   363  		case okdown:
   364  			d.RoundDown(mi + 1)
   365  			return
   366  		case okup:
   367  			d.RoundUp(mi + 1)
   368  			return
   369  		}
   370  	}
   371  }
   372  
   373  type decimalSlice struct {
   374  	d      []byte
   375  	nd, dp int
   376  	neg    bool
   377  }
   378  
   379  // %e: -d.ddddde±dd
   380  func fmtE(dst []byte, neg bool, d decimalSlice, prec int, fmt byte) []byte {
   381  	// sign
   382  	if neg {
   383  		dst = append(dst, '-')
   384  	}
   385  
   386  	// first digit
   387  	ch := byte('0')
   388  	if d.nd != 0 {
   389  		ch = d.d[0]
   390  	}
   391  	dst = append(dst, ch)
   392  
   393  	// .moredigits
   394  	if prec > 0 {
   395  		dst = append(dst, '.')
   396  		i := 1
   397  		m := min(d.nd, prec+1)
   398  		if i < m {
   399  			dst = append(dst, d.d[i:m]...)
   400  			i = m
   401  		}
   402  		for ; i <= prec; i++ {
   403  			dst = append(dst, '0')
   404  		}
   405  	}
   406  
   407  	// e±
   408  	dst = append(dst, fmt)
   409  	exp := d.dp - 1
   410  	if d.nd == 0 { // special case: 0 has exponent 0
   411  		exp = 0
   412  	}
   413  	if exp < 0 {
   414  		ch = '-'
   415  		exp = -exp
   416  	} else {
   417  		ch = '+'
   418  	}
   419  	dst = append(dst, ch)
   420  
   421  	// dd or ddd
   422  	switch {
   423  	case exp < 10:
   424  		dst = append(dst, '0', byte(exp)+'0')
   425  	case exp < 100:
   426  		dst = append(dst, byte(exp/10)+'0', byte(exp%10)+'0')
   427  	default:
   428  		dst = append(dst, byte(exp/100)+'0', byte(exp/10)%10+'0', byte(exp%10)+'0')
   429  	}
   430  
   431  	return dst
   432  }
   433  
   434  // %f: -ddddddd.ddddd
   435  func fmtF(dst []byte, neg bool, d decimalSlice, prec int) []byte {
   436  	// sign
   437  	if neg {
   438  		dst = append(dst, '-')
   439  	}
   440  
   441  	// integer, padded with zeros as needed.
   442  	if d.dp > 0 {
   443  		m := min(d.nd, d.dp)
   444  		dst = append(dst, d.d[:m]...)
   445  		for ; m < d.dp; m++ {
   446  			dst = append(dst, '0')
   447  		}
   448  	} else {
   449  		dst = append(dst, '0')
   450  	}
   451  
   452  	// fraction
   453  	if prec > 0 {
   454  		dst = append(dst, '.')
   455  		for i := 0; i < prec; i++ {
   456  			ch := byte('0')
   457  			if j := d.dp + i; 0 <= j && j < d.nd {
   458  				ch = d.d[j]
   459  			}
   460  			dst = append(dst, ch)
   461  		}
   462  	}
   463  
   464  	return dst
   465  }
   466  
   467  // %b: -ddddddddp±ddd
   468  func fmtB(dst []byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
   469  	// sign
   470  	if neg {
   471  		dst = append(dst, '-')
   472  	}
   473  
   474  	// mantissa
   475  	dst, _ = formatBits(dst, mant, 10, false, true)
   476  
   477  	// p
   478  	dst = append(dst, 'p')
   479  
   480  	// ±exponent
   481  	exp -= int(flt.mantbits)
   482  	if exp >= 0 {
   483  		dst = append(dst, '+')
   484  	}
   485  	dst, _ = formatBits(dst, uint64(exp), 10, exp < 0, true)
   486  
   487  	return dst
   488  }
   489  
   490  // %x: -0x1.yyyyyyyyp±ddd or -0x0p+0. (y is hex digit, d is decimal digit)
   491  func fmtX(dst []byte, prec int, fmt byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
   492  	if mant == 0 {
   493  		exp = 0
   494  	}
   495  
   496  	// Shift digits so leading 1 (if any) is at bit 1<<60.
   497  	mant <<= 60 - flt.mantbits
   498  	for mant != 0 && mant&(1<<60) == 0 {
   499  		mant <<= 1
   500  		exp--
   501  	}
   502  
   503  	// Round if requested.
   504  	if prec >= 0 && prec < 15 {
   505  		shift := uint(prec * 4)
   506  		extra := (mant << shift) & (1<<60 - 1)
   507  		mant >>= 60 - shift
   508  		if extra|(mant&1) > 1<<59 {
   509  			mant++
   510  		}
   511  		mant <<= 60 - shift
   512  		if mant&(1<<61) != 0 {
   513  			// Wrapped around.
   514  			mant >>= 1
   515  			exp++
   516  		}
   517  	}
   518  
   519  	hex := lowerhex
   520  	if fmt == 'X' {
   521  		hex = upperhex
   522  	}
   523  
   524  	// sign, 0x, leading digit
   525  	if neg {
   526  		dst = append(dst, '-')
   527  	}
   528  	dst = append(dst, '0', fmt, '0'+byte((mant>>60)&1))
   529  
   530  	// .fraction
   531  	mant <<= 4 // remove leading 0 or 1
   532  	if prec < 0 && mant != 0 {
   533  		dst = append(dst, '.')
   534  		for mant != 0 {
   535  			dst = append(dst, hex[(mant>>60)&15])
   536  			mant <<= 4
   537  		}
   538  	} else if prec > 0 {
   539  		dst = append(dst, '.')
   540  		for i := 0; i < prec; i++ {
   541  			dst = append(dst, hex[(mant>>60)&15])
   542  			mant <<= 4
   543  		}
   544  	}
   545  
   546  	// p±
   547  	ch := byte('P')
   548  	if fmt == lower(fmt) {
   549  		ch = 'p'
   550  	}
   551  	dst = append(dst, ch)
   552  	if exp < 0 {
   553  		ch = '-'
   554  		exp = -exp
   555  	} else {
   556  		ch = '+'
   557  	}
   558  	dst = append(dst, ch)
   559  
   560  	// dd or ddd or dddd
   561  	switch {
   562  	case exp < 100:
   563  		dst = append(dst, byte(exp/10)+'0', byte(exp%10)+'0')
   564  	case exp < 1000:
   565  		dst = append(dst, byte(exp/100)+'0', byte((exp/10)%10)+'0', byte(exp%10)+'0')
   566  	default:
   567  		dst = append(dst, byte(exp/1000)+'0', byte(exp/100)%10+'0', byte((exp/10)%10)+'0', byte(exp%10)+'0')
   568  	}
   569  
   570  	return dst
   571  }
   572  
   573  func min(a, b int) int {
   574  	if a < b {
   575  		return a
   576  	}
   577  	return b
   578  }
   579  
   580  func max(a, b int) int {
   581  	if a > b {
   582  		return a
   583  	}
   584  	return b
   585  }
   586  

View as plain text