i'm experimenting with go with pi calculating formulas, and made this:
package main
import (
"fmt"
"math"
)
func bbpformula() {
var result float64
for k := 0.0; k <= 100; k++ {
result += (1 / math.Pow(16.0, k) * ((4 / (8*k + 1)) - (2 / (8*k + 4)) - (1 / (8*k + 5)) - (1 / (8*k + 6))))
}
fmt.Println(result)
}
func main() {
bbpformula()
}
and the output is:
3.141592653589793
However, i would like more digits, i tried using the math/big package to make the variables big.Float with simpler formulas, but the arithmetic operations were extremely confusing and slow.
So my idea is overwriting the result variable when its overloaded (maximum capacity), but i have no ideia how to do that!
Could someone please explain to me how that is done?
edit: tried to use the if result > math.MaxFloat64 but the output turns into nothing
import (
"fmt"
"math"
)
func bbpformula() {
var result float64
var resultb float64
for k := 0.0; k <= 100; k++ {
result += (1 / math.Pow(16.0, k) * ((4 / (8*k + 1)) - (2 / (8*k + 4)) - (1 / (8*k + 5)) - (1 / (8*k + 6))))
if result > math.MaxFloat64 {
fmt.Println(result)
resultb += (1 / math.Pow(16.0, k) * ((4 / (8*k + 1)) - (2 / (8*k + 4)) - (1 / (8*k + 5)) - (1 / (8*k + 6))))
if resultb == math.MaxFloat64 {
fmt.Println(resultb)
result = 0
}
}
}
}
func main() {
bbpformula()
}
output:
Try using MaxFloat32
or MaxFloat64
in the math
package. Like, check if result > math.MaxFloat64
?