decimal icon indicating copy to clipboard operation
decimal copied to clipboard

Wrong value in same numbers: Div + Mul

Open vsokolovsky555 opened this issue 4 years ago • 4 comments

	fee := decimal.NewFromFloat(0.0295)
	feeMin := decimal.NewFromFloat(4)
	feeAmt := decimal.NewFromFloat(2.95)

	fmt.Println((4 / 2.95) * 0.0295)
	fmt.Println(feeMin.Div(feeAmt).Mul(fee).String())
	fmt.Println(feeMin.Mul(fee.Div(feeAmt)).String())
0.04
0.03999999999999999975
0.04

This example: https://play.golang.org/p/qaAtlBHTJpY

vsokolovsky555 avatar Oct 29 '20 12:10 vsokolovsky555

Hi @vsokolovsky555. Thanks for mentioning this issue. This is a known bug (maybe someone could call it feature) with Div meethod that can produce not exact results. This is strictly related to exponent of the producded result. I will take a look. Thanks!

mwoss avatar Oct 31 '20 12:10 mwoss

@mwoss any luck on this issue?

durango avatar Apr 13 '21 20:04 durango

@durango As many people mentioned this behaviour as a bug I will try to change the way of how Div rounds the result. Hopefully, I will find time at weekend to do it, but I can't promise anything. Lately, I'm quite busy :<

mwoss avatar Apr 15 '21 22:04 mwoss

Are there any updates on this? Thanks

said-saifi avatar Jun 27 '22 12:06 said-saifi