// 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 "strconv" // TODO: move elsewhere? package type FloatInfo struct { mantbits uint; expbits uint; bias int; } package var float32info = FloatInfo{ 23, 8, -127 } package var float64info = FloatInfo{ 52, 11, -1023 } func FmtB(neg bool, mant uint64, exp int, flt *FloatInfo) string func FmtE(neg bool, d *Decimal, prec int) string func FmtF(neg bool, d *Decimal, prec int) string func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string func Max(a, b int) int func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) func FloatSize() int { // Figure out whether float is float32 or float64. // 1e-35 is representable in both, but 1e-70 // is too small for a float32. var f float = 1e-35; if f*f == 0 { return 32; } return 64; } export var floatsize = FloatSize() export func ftoa32(f float32, fmt byte, prec int) string { return GenericFtoa(uint64(sys.float32bits(f)), fmt, prec, &float32info); } export func ftoa64(f float64, fmt byte, prec int) string { return GenericFtoa(sys.float64bits(f), fmt, prec, &float64info); } export func ftoa(f float, fmt byte, prec int) string { if floatsize == 32 { return ftoa32(float32(f), fmt, prec); } return ftoa64(float64(f), fmt, prec); } func GenericFtoa(bits uint64, fmt byte, prec int, flt *FloatInfo) string { neg := bits>>flt.expbits>>flt.mantbits != 0; exp := int(bits>>flt.mantbits) & (1< d.nd { prec = d.nd; } // %e is used if the exponent from the conversion // is less than -4 or greater than or equal to the precision. exp := d.dp - 1; if exp < -4 || exp >= prec { return FmtE(neg, d, prec - 1); } return FmtF(neg, d, Max(prec - d.dp, 0)); } return "%" + string(fmt); } // Round d (= mant * 2^exp) to the shortest number of digits // that will let the original floating point value be precisely // reconstructed. Size is original floating point size (64 or 32). func RoundShortest(d *Decimal, mant uint64, exp int, flt *FloatInfo) { // TODO: Unless exp == minexp, if the number of digits in d // is less than 17, it seems unlikely that it could not be // the shortest possible number already. So maybe we can // bail out without doing the extra multiprecision math here. // Compute upper and lower such that any decimal number // between upper and lower (possibly inclusive) // will round to the original floating point number. // d = mant << (exp - mantbits) // Next highest floating point number is mant+1 << exp-mantbits. // Our upper bound is halfway inbetween, mant*2+1 << exp-mantbits-1. upper := NewDecimal(mant*2+1).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 inbetween, mantlo*2+1 << explo-mantbits-1. minexp := flt.bias + 1; // minimum possible exponent var mantlo uint64; var explo int; if mant > 1< 0 { buf[w] = '.'; w++; for i := 0; i < prec; i++ { if 1+i < d.nd { buf[w] = d.d[1+i]; } else { buf[w] = '0'; } w++; } } // e± buf[w] = 'e'; w++; exp := d.dp - 1; if d.nd == 0 { // special case: 0 has exponent 0 exp = 0; } if exp < 0 { buf[w] = '-'; exp = -exp; } else { buf[w] = '+'; } w++; // dddd // count digits n := 0; for e := exp; e > 0; e /= 10 { n++; } // leading zeros for i := n; i < 2; i++ { buf[w] = '0'; w++; } // digits w += n; n = 0; for e := exp; e > 0; e /= 10 { n++; buf[w-n] = byte(e%10 + '0'); } return string(buf[0:w]); } // %f: -ddddddd.ddddd func FmtF(neg bool, d *Decimal, prec int) string { buf := new([]byte, 1+Max(d.dp, 1)+1+Max(prec, 0)); w := 0; // sign if neg { buf[w] = '-'; w++; } // integer, padded with zeros as needed. if d.dp > 0 { var i int; for i = 0; i < d.dp && i < d.nd; i++ { buf[w] = d.d[i]; w++; } for ; i < d.dp; i++ { buf[w] = '0'; w++; } } else { buf[w] = '0'; w++; } // fraction if prec > 0 { buf[w] = '.'; w++; for i := 0; i < prec; i++ { if d.dp+i < 0 || d.dp+i >= d.nd { buf[w] = '0'; } else { buf[w] = d.d[d.dp+i]; } w++; } } return string(buf[0:w]); } // %b: -ddddddddp+ddd func FmtB(neg bool, mant uint64, exp int, flt *FloatInfo) string { var buf [50]byte; w := len(buf); exp -= int(flt.mantbits); esign := byte('+'); if exp < 0 { esign = '-'; exp = -exp; } n := 0; for exp > 0 || n < 1 { n++; w--; buf[w] = byte(exp%10 + '0'); exp /= 10 } w--; buf[w] = esign; w--; buf[w] = 'p'; n = 0; for mant > 0 || n < 1 { n++; w--; buf[w] = byte(mant%10 + '0'); mant /= 10; } if neg { w--; buf[w] = '-'; } return string((&buf)[w:len(buf)]); } func Max(a, b int) int { if a > b { return a; } return b; }