uzyn / bigdenary Goto Github PK
View Code? Open in Web Editor NEW๐ฆ๐งฎ Arbitrary-length decimal library, implemented with ES2020's native BigInt.
Home Page: https://deno.land/x/bigdenary
License: MIT License
๐ฆ๐งฎ Arbitrary-length decimal library, implemented with ES2020's native BigInt.
Home Page: https://deno.land/x/bigdenary
License: MIT License
Please take a look at https://deno.land/x/bigdenary
Hi,
I found an issue I want to highlight, curious about the position of the maintainer of this library on it.
I found that for bigdenary (100 / 3) * 3 != 100
and (100 / 3) + (100 / 3) + (100 / 3) != 100
the failing test are the following
Deno.test("BigDenary: (100 / 3) + (100 / 3) + (100 / 3) == 100", () => {
const b_d100 = new BigDenary(100);
const b_d33 = b_d100.div(3);
const b_d100sum = new BigDenary(0).add(b_d33).add(b_d33).add(b_d33);
assert(b_d100.eq(b_d100sum));
});
Deno.test("BigDenary: (100 / 3) * 3 == 100", () => {
const b_d100 = new BigDenary(100);
const b_d100mul = new BigDenary(100).div(3).mul(3);
assert(b_d100.eq(b_d100mul));
});
The tests succeed when a BigDenary is converted to number with valueOf()
, because valueOf()
internally convert to string and then uses parseFloat()
for the conversion to number, and because the converted string has more than 14 decimals, the returned number is rounded.
I decided then to test also decimal.js and number
object, and I found that: number
process them correctly (failing on others) while decimal.js
fails at them as this library.
Test file attached.
For completeness, I also tested C #, and using the decimal
type the problem is not present:
decimal d100 = 100;
decimal d33 = d100/ 3;
decimal d100sum = d33 + d33 + d33;
decimal d100mul = d33 * 3;
Console.WriteLine(d33);
Console.WriteLine(d100 == d100sum);
Console.WriteLine(d100sum);
Console.WriteLine(d100 == d100mul);
Console.WriteLine(d100mul);
I think this line in the implementation is not safe it could cause rounding issues:
https://github.com/uzyn/bigdenary/blob/master/mod.ts#L31C7-L31C60
For example the following number will fail: 0.0023537
You can see the problem with this if you try the following calculation:
// 23536.999999999996 instead of 23537
0.0023537 * Math.pow(10, 7)
The response will be a float instead of an integer so it will fail with BigInt.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.