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.
Code
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
Big-O Complexity VisualizerVisualize and compare algorithm time complexities like O(1), O(log n), O(n), O(n log n), and O(n²). Plot growth curves to understand performance scaling at a glance.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.GCD & LCM CalculatorFind the greatest common divisor and least common multiple of two or more integers, computed exactly with BigInt.IEEE 754 ConverterConvert decimal floats to and from IEEE 754 single- and double-precision bit patterns in hex and binary.
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:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.