Closed MouJiaoZi closed 3 months ago
but, (BN(1000) 12 12 / 100) - 1000 => 440 it's ok
This isn't an issue with my library. You are just discovering how floating-point numbers work.
> 1.2
1.2
> ('%.50f'):format(1.2)
1.19999999999999995559107901499373838305473327636719
This is exactly why I created the library: to work around this limitation of floating-point representation. This library supports string-constructors and table-constructors, so you can avoid constructing a BigNumber from an imperfect fraction floating-point number:
> big'1.2'
1.2
> big{[-1]=2, [0]=1}
1.2
local BigNumber = require 'bignumber'
local a = BigNumber'1000' * BigNumber'1.2' * BigNumber'1.2'
print(a) -- prints 1440
local b = a - 1000 -- subtraction using coerced float-point Lua-Number of 1000 is an exact representation and won't incur rounded fractions
print(b) -- prints 440
thank you so much!