royNiladri / js-big-decimal

Work with large numbers on the client side with high precision.
https://www.npmjs.com/package/js-big-decimal
MIT License
156 stars 28 forks source link

Wrong answer calculated #40

Closed ChristopherDrum closed 3 years ago

ChristopherDrum commented 3 years ago

Describe the bug bigDecimal.divide('123456789.123456','.0123456',18) returns 1000.006391940902021773 but should return 10000063919.40902021773

Decimal position is wrong.

Additional context Using npm install in node; reports version 1.3.1 Was working on an translation of js-big-decimal to Lua (for Pico8) and my code returned the incorrect answer. Verified my answer against js-big-decimal and realized I had perfectly implemented it, down to even this bug. 🐛

royNiladri commented 3 years ago

Thanks for reporting.will look into it and get back within 48 hours.

ChristopherDrum commented 3 years ago

As I look further, here are more incorrect cases.

bigDecimal.divide('2', '.0383292')) Expected: 52.17953935902653851371800089748 Actual: 0.0000052179

bigDecimal.divide('.102', '.0383292',8)) Expected: 2.66115650731035 Actual: 0.00000027

So far, it seems to occur when the divisor has more decimal places than the dividend 🤔 But there may be more cases I've yet to discover.

I was able to repair all known issues in my own (Lua) code around the parsing of dividend decimal shift around decimal.ts, Line 29. My fix can't translate well to what you're doing, but conceptually that was the issue.

royNiladri commented 3 years ago

I am looking into it, but for now, I have found a workaround. If you add the leading zeros, like 0.102 or 0.123456, it seems to work fine. image

royNiladri commented 3 years ago

Checkout the latest patch v1.3.2

ChristopherDrum commented 3 years ago

Leading zero... ah, sorry, that just didn't cross my mind to try. Thanks for the repair, looks good 👍