thenumbernine / lua-bignumber

a big integer / number library
13 stars 0 forks source link

(BN(1000) * 1.2 * 1.2) - 1000 => 439.999999999 #1

Closed MouJiaoZi closed 3 months ago

MouJiaoZi commented 3 months ago

img_v3_02bi_e4be32b9-dc9f-45a6-b632-fbb4ea2985bg img_v3_02bi_ebb31720-e20d-4136-a709-de093185c71g

MouJiaoZi commented 3 months ago

but, (BN(1000) 12 12 / 100) - 1000 => 440 it's ok

thenumbernine commented 3 months ago

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
MouJiaoZi commented 3 months ago

thank you so much!