Big Float SetPrec Precision

Raise precision with big.Float SetPrec, print a long decimal expansion, and read Acc to learn whether a division was rounded.

GoData Integrity

Code

snippet
package main

import (
	"fmt"
	"math/big"
)

func main() {
	// binary floating point cannot represent 0.1 exactly
	fmt.Println("float64 sum:", 0.1+0.2)

	// big.Float keeps as many mantissa bits as SetPrec allows
	x, ok := new(big.Float).SetPrec(200).SetString("0.1")
	if !ok {
		fmt.Println("parse failed")
		return
	}
	y, _ := new(big.Float).SetPrec(200).SetString("0.2")
	sum := new(big.Float).SetPrec(200).Add(x, y)
	fmt.Println("big.Float sum:", sum.Text('f', 40))

	// division records whether the stored result was rounded
	third := new(big.Float).SetPrec(200).Quo(x, new(big.Float).SetInt64(3))
	fmt.Println("one third:", third.Text('f', 30))
	fmt.Println("accuracy:", third.Acc())

	// lowering the precision rounds the stored value
	low := new(big.Float).SetPrec(8).Set(x)
	fmt.Println("8-bit mantissa:", low.Text('g', 8))
	fmt.Println("precisions:", x.Prec(), low.Prec())
}

Walkthrough

Big Float SetPrec Precision comes down to the function main.

What to watch for

  • No package installation, no environment variables, no setup steps.

Using it

The run emits 7 labeled sections — float64 sum:, parse failed, big.Float sum:, one third:, accuracy:, 8-bit mantissa:, precisions: — one per step.

Key statements

third := new(big.Float).SetPrec(200).Quo(x, new(big.Float).SetInt64(3))

Interactive Tools for This Topic

FAQ

What does Big Float SetPrec Precision print when run?
Running the file directly is enough — expect labeled output lines such as `float64 sum:`, `parse failed`, `big.Float sum:`.