AdamWhiteHat / BigRational

Arbitrary precision rational number class
MIT License
24 stars 2 forks source link

BUG: Numbers between ]-1, 0[ have a wrong sign. #13

Closed SGStino closed 1 year ago

SGStino commented 1 year ago

Description numbers like -0.0001 have a whole part of 0 and a fractional part of 1 /10000, but whole and fractional are positive (sign = 1). So nothing stores the sign of the complete number.

Error/Exception Message If you try the following in RoslynPad:

#r "nuget: ExtendedNumerics.BigRational, 2023.162.546"

using ExtendedNumerics;
var value = new BigRational(0.000001);
value.Dump();
(-value).Dump();

you'll get: image

Expected Behavior (-value).Sign should be -1 and value.Sign should be 1 And value.Whole.Sign can remain 0, but then value.Fraction.Sign should become -1 in the negative case. Actual Behavior Both signs are 0 due to the sign being taken entirely from the whole part.

Notes Explicitly creating a negative fraction results in Sign = 0

new BigRational(0, -1, 1000000).Dump();

Because the sign of the Fractional isn't even preserved when negating the entire number.

AdamWhiteHat commented 1 year ago

This should be fixed now. Thank you for bringing it to my attention.

BigRational.Negate was not handling the case where the WholePart was zero. BigRational.Compare and the explicit conversion methods from BigRational to double and to decimal also needed to consider this case and so their logic was also updated.

A new NuGet version reflecting these changes have been uploaded and is now available: 2023.179.1529

You can close this issue if you are satisfied, otherwise I will probably close this issue in about week or so if I don't hear anything else from you.

SGStino commented 1 year ago

Using the new nuget in my test project my test cases also pass. Although they aren't as exhaustive as yours. Thanks.