}
// TODO(rsc): Better truncation handling.
-func stringToDecimal(s string) (neg bool, d *decimal, trunc bool, ok bool) {
+func (b *decimal) set(s string) (ok bool) {
i := 0
+ b.neg = false
// optional sign
if i >= len(s) {
case s[i] == '+':
i++
case s[i] == '-':
- neg = true
+ b.neg = true
i++
}
// digits
- b := new(decimal)
sawdot := false
sawdigits := false
for ; i < len(s); i++ {
return
}
- d = b
ok = true
return
}
// decimal power of ten to binary power of two.
var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26}
-func decimalToFloatBits(neg bool, d *decimal, trunc bool, flt *floatInfo) (b uint64, overflow bool) {
+func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) {
var exp int
var mant uint64
}
// Extract 1+flt.mantbits bits.
- mant = d.Shift(int(1 + flt.mantbits)).RoundedInteger()
+ d.Shift(int(1 + flt.mantbits))
+ mant = d.RoundedInteger()
// Rounding might have added a bit; shift down.
if mant == 2<<flt.mantbits {
// Assemble bits.
bits := mant & (uint64(1)<<flt.mantbits - 1)
bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
- if neg {
+ if d.neg {
bits |= 1 << flt.mantbits << flt.expbits
}
return bits, overflow
// Compute exact floating-point integer from d's digits.
// Caller is responsible for avoiding overflow.
-func decimalAtof64Int(neg bool, d *decimal) float64 {
+func (d *decimal) atof64int() float64 {
f := 0.0
for i := 0; i < d.nd; i++ {
f = f*10 + float64(d.d[i]-'0')
}
- if neg {
- f *= -1 // BUG work around 6g f = -f.
+ if d.neg {
+ f = -f
}
return f
}
-func decimalAtof32Int(neg bool, d *decimal) float32 {
+func (d *decimal) atof32int() float32 {
f := float32(0)
for i := 0; i < d.nd; i++ {
f = f*10 + float32(d.d[i]-'0')
}
- if neg {
- f *= -1 // BUG work around 6g f = -f.
+ if d.neg {
+ f = -f
}
return f
}
// value is exact integer * exact power of ten
// value is exact integer / exact power of ten
// These all produce potentially inexact but correctly rounded answers.
-func decimalAtof64(neg bool, d *decimal, trunc bool) (f float64, ok bool) {
+func (d *decimal) atof64() (f float64, ok bool) {
// Exact integers are <= 10^15.
// Exact powers of ten are <= 10^22.
if d.nd > 15 {
}
switch {
case d.dp == d.nd: // int
- f := decimalAtof64Int(neg, d)
+ f := d.atof64int()
return f, true
case d.dp > d.nd && d.dp <= 15+22: // int * 10^k
- f := decimalAtof64Int(neg, d)
+ f := d.atof64int()
k := d.dp - d.nd
// If exponent is big but number of digits is not,
// can move a few zeros into the integer part.
return f * float64pow10[k], true
case d.dp < d.nd && d.nd-d.dp <= 22: // int / 10^k
- f := decimalAtof64Int(neg, d)
+ f := d.atof64int()
return f / float64pow10[d.nd-d.dp], true
}
return
// If possible to convert decimal d to 32-bit float f exactly,
// entirely in floating-point math, do so, avoiding the machinery above.
-func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) {
+func (d *decimal) atof32() (f float32, ok bool) {
// Exact integers are <= 10^7.
// Exact powers of ten are <= 10^10.
if d.nd > 7 {
}
switch {
case d.dp == d.nd: // int
- f := decimalAtof32Int(neg, d)
+ f := d.atof32int()
return f, true
case d.dp > d.nd && d.dp <= 7+10: // int * 10^k
- f := decimalAtof32Int(neg, d)
+ f := d.atof32int()
k := d.dp - d.nd
// If exponent is big but number of digits is not,
// can move a few zeros into the integer part.
return f * float32pow10[k], true
case d.dp < d.nd && d.nd-d.dp <= 10: // int / 10^k
- f := decimalAtof32Int(neg, d)
+ f := d.atof32int()
return f / float32pow10[d.nd-d.dp], true
}
return
return float32(val), nil
}
- neg, d, trunc, ok := stringToDecimal(s)
- if !ok {
+ var d decimal
+ if !d.set(s) {
return 0, &NumError{s, os.EINVAL}
}
if optimize {
- if f, ok := decimalAtof32(neg, d, trunc); ok {
+ if f, ok := d.atof32(); ok {
return f, nil
}
}
- b, ovf := decimalToFloatBits(neg, d, trunc, &float32info)
+ b, ovf := d.floatBits(&float32info)
f = math.Float32frombits(uint32(b))
if ovf {
err = &NumError{s, os.ERANGE}
return val, nil
}
- neg, d, trunc, ok := stringToDecimal(s)
- if !ok {
+ var d decimal
+ if !d.set(s) {
return 0, &NumError{s, os.EINVAL}
}
if optimize {
- if f, ok := decimalAtof64(neg, d, trunc); ok {
+ if f, ok := d.atof64(); ok {
return f, nil
}
}
- b, ovf := decimalToFloatBits(neg, d, trunc, &float64info)
+ b, ovf := d.floatBits(&float64info)
f = math.Float64frombits(b)
if ovf {
err = &NumError{s, os.ERANGE}
// The shift is exp - flt.mantbits because mant is a 1-bit integer
// followed by a flt.mantbits fraction, and we are treating it as
// a 1+flt.mantbits-bit integer.
- d := newDecimal(mant).Shift(exp - int(flt.mantbits))
+ d := newDecimal(mant)
+ d.Shift(exp - int(flt.mantbits))
// Round appropriately.
// Negative precision means "only as much as needed to be exact."
// 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)
+ upper := newDecimal(mant*2 + 1)
+ upper.Shift(exp - int(flt.mantbits) - 1)
// d = mant << (exp - mantbits)
// Next lowest floating point number is mant-1 << exp-mantbits,
mantlo = mant*2 - 1
explo = exp - 1
}
- lower := newDecimal(mantlo*2 + 1).Shift(explo - int(flt.mantbits) - 1)
+ lower := newDecimal(mantlo*2 + 1)
+ lower.Shift(explo - int(flt.mantbits) - 1)
// The upper and lower bounds are possible outputs only if
// the original mantissa is even, so that IEEE round-to-even