dplece1882
2019-05-14 15:03
浏览 1.6k
已采纳

Golang big.Float的精度问题

I've met some interesting problem of Golang big.Float calculation.

The Problem is

10001000100010001000100010001000100010001000100015.5533 / 1000000000000000000

= 10001000100010001000100010001000.1000100010001000155533

However, big.Float gave "10001000100010001000100010001000.10001000100010001555329999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997"

The code:

var prec uint = 1024 // 512
dec, _ := new(big.Float).SetPrec(prec).SetString("1000000000000000000")
f, _ := new(big.Float).SetPrec(prec).SetString("10001000100010001000100010001000100010001000100015.5533")
q := f.Quo(f, dec)

fmt.Printf("Percision: %d
", prec)
fmt.Printf("Quotient: %s
", q.Text('f', -1))

Result:

Percision: 1024
Quotient: 10001000100010001000100010001000.10001000100010001555329999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997

And the more confusing part is, if I set prec = 512, a smaller precision, it produced correct result instead.

Percision: 512
Quotient: 10001000100010001000100010001000.1000100010001000155533

Doses any one know what's wrong of my code or how to configure big.Float to get expected result?

Thanks to all!

图片转代码服务由CSDN问答提供 功能建议

我遇到了一些有趣的Golang big.Float计算问题。

问题是

10001000100010001000100010001000100010001000100015.5533 / 1000000000000000000

= 10001000100010001000100010001000.1000100010001000155533

然而,big.Float给“10001000100010001000100010001000.10001000100010001555329999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997 “

代码:

  var prec uint = 1024 // 512 
dec,  _:= new(big.Float).SetPrec(prec).SetString(“ 1000000000000000000”)
f,_:= new(big.Float).SetPrec(prec).SetString(“ 100010001000100010001000100010001000100010001000100015.5533”)
q:= f  .Quo(f,dec)
 
fmt.Printf(“ Percision:%d 
”,prec)
fmt.Pri  ntf(“商:%s 
”,q.Text('f',-1))
   
 
 

结果:

 精度:1024 
商数:10001000100010001000100010001000.1000100010001000155532999999999999999999999999999999999999999999999999999999999 <9999>>   
 
 
 精度:512 
商数:10001000100010001000100010001000.1000100010001000155533 
   
 
 

任何人都知道我的问题是什么 代码或如何配置big.Float以获得预期的结果?

感谢所有人!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dragon4587 2019-05-14 15:49
    最佳回答

    From go doc math/big.Float:

    A nonzero finite Float represents a multi-precision floating point number

    sign × mantissa × 2**exponent

    with 0.5 <= mantissa < 1.0, and MinExp <= exponent <= MaxExp.

    And SetPrec sets the bitwidth of the mantissa not some decimal precision.

    Like with float64s not every decimal number can be represented exact in a big.Float and your code shows this. The fact that you see what you expect to see with prec=512 is due to different rounding and printing.

    Rule of thumb: big.Floats behave like "normal" floats with all their shortcomings (here not every decimal fraction can be represented) but may show less rounding errors.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题