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

View as plain text