Example #1
0
// String returns a kind messy decimal string version of this.
// It is utterly precise, and will use as many decimal digits
// as are necessary to completely represent this number.
func (f Float) String() string {
	sign := "+"
	if !f.sign {
		sign = "-"
	}

	var whole *mathx.Int
	var fraction *mathx.Int

	if f.exp <= 0 {
		whole = f.mantissa.Rsh(uint(-f.exp))
		fraction = f.mantissa.Sub(whole.Lsh(uint(-f.exp)))
	} else {
		whole = mathx.NewInt(0)
		fraction = f.mantissa
	}

	whole = f.mantissa.Rsh(uint(-f.exp))
	fraction = f.mantissa.Sub(whole.Lsh(uint(-f.exp)))

	digits := ""
	for fraction.Sign() != 0 {
		fraction = fraction.Mul64(10)
		digit := fraction.Rsh(uint(-f.exp))
		fraction = fraction.Sub(digit.Lsh(uint(-f.exp)))
		digits += digit.String()
	}
	if digits == "" {
		digits = "0"
	}
	return fmt.Sprintf("%s%s.%s", sign, whole.String(), digits)
}