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

(100 / 3) * 3 != 100 and (100 / 3) + (100 / 3) + (100 / 3) != 100 #10

Open
77it opened this issue Sep 27, 2020 · 0 comments
Open

(100 / 3) * 3 != 100 and (100 / 3) + (100 / 3) + (100 / 3) != 100 #10

77it opened this issue Sep 27, 2020 · 0 comments

Comments

@77it
Copy link

77it commented Sep 27, 2020

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);

test.txt

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

1 participant