Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiply does not return fraction, it rounds, needs option to return fraction. #3

Open
Light-Wizzard opened this issue Mar 28, 2019 · 2 comments

Comments

@Light-Wizzard
Copy link

Great job, one problem with JavaScript and Floating point:

Example: multiply("241828072282107.5071453596951","666")
expected value: 161057496139883599.758809556936599999999598382266485785407894582022
returned value: 161057496139883600

(ia * ib) / (fa * fb)
(2418280722821075071453596951 * 666) / (10000000000000 / 1)
= 161057496139883599.758809556936599999999598382266485785407894582022
This is what I want it to return,
It is not the math that is failing,
it is JavaScript, it can not handle this large of Float, so it rounds it
toFixed has no effect.

Any idea how to get JavaScript to do this math without Rounding it?

@yannickglt
Copy link
Member

Hey!
Thanks for using it. For so large numbers, I guess we can add an implementation using the new BigInt type to get a more precise result.

(BigInt(2418280722821075071453596951) * BigInt(666)) / (BigInt(10000000000000) / BigInt(1)) === 161057496139883599n

@Light-Wizzard
Copy link
Author

I can not get BigInt to work in Qt Qml JavaScript, it does not use the same Engine as the Web, and BigInt uses "this" and Qml JavaScript engine has issues with "this", your code works great for most uses, its only large numbers, when you do your division, the result has the same issue, its wrong, because FP can not handle that size of a number, I thought about breaking the number down and doing the math in smaller increments to see if I can get around this large number issue, it is not your math, it is right, do it by hand, it is the FP rounding it, and toFixed did not help. I do not know if you use Qt Qml, but you would have to, to figure this out, works great for the Web, I like you approach better than other out there, its clean, easy to understand and it works for the Web. 64 bit Floating Point sucks, 128 bit is much better, I had no luck with a long double, knowing they are the same on most devices, it is a moot point, I use this in Felgo for cross Platform use and Android and IoS, so the code has to work on those devices as well.

To fix your code, you would need to find another way to divide the numbers without precision lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants