简体   繁体   中英

Go - Divide big.Float

I'm dealing with numbers that require a big.Float type, and I need to divide them. I'm aware that big.Int has a .Div() function, but if I'm correct, that truncates the value and loses the precision I get from using big.Float .

Relevant code

func e(prec int64) (res *big.Float) {
    res = big.NewFloat(float64(1.0))
    base := big.NewInt(prec)

    for i := base; i.Cmp(big.NewInt(int64(0))) == 1; _ = i.Sub(i, big.NewInt(1)) {
        d := big.NewFloat(float64(1.0))
        _ = d.Div(fact(i)) // error here
        res.Add(d)
    }

    return
}

Use Float.Quo for big.Float division:

x, y := big.NewFloat(10), big.NewFloat(3)
z := new(big.Float).Quo(x, y)

http://play.golang.org/p/GRPAKQNkq0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM