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

[BUG] Reciprocal of a negative fraction has invalid state #27

Closed
lipchev opened this issue Mar 31, 2024 · 1 comment · Fixed by #28
Closed

[BUG] Reciprocal of a negative fraction has invalid state #27

lipchev opened this issue Mar 31, 2024 · 1 comment · Fixed by #28

Comments

@lipchev
Copy link
Contributor

lipchev commented Mar 31, 2024

Consider the following scenario:

  Name Value Type
  new Fraction(-1, 10).Reciprocal().ToDecimal() -10 decimal
  Expression Value Type
  new Fraction(-1, 10).Reciprocal().IsEquivalentTo(new Fraction(-10)) false bool

Normally this should have been true for the Equals as well, but I think this illustrates the issue better.

This problem stems from the fact that the sign of the fraction is not handled here

    public static Fraction Reciprocal(Fraction fraction) =>
        new Fraction(fraction.Denominator, fraction.Numerator, fraction.State);

According to GetReducedFraction a Normalized fraction should have it's signs normalized:

            if (denominator.Sign == -1) {
                // Denominator must not be negative after normalization
                numerator = BigInteger.Negate(numerator);
                denominator = BigInteger.Negate(denominator);
            }

I'd be willing to create a PR, but I was hoping that we could merge my "cleanup" of the test project first, if you don't mind..

@danm-de
Copy link
Owner

danm-de commented Mar 31, 2024

Oh, thanks.

I didn't even think about that when I merged #21 :-)

danm-de added a commit that referenced this issue Mar 31, 2024
danm-de added a commit that referenced this issue Mar 31, 2024
- Code cleanup
- use new language features
- use method body (if possible)
- use var statement instead of type declaration (if possible)
- fix #27 [BUG] Reciprocal of a negative fraction has invalid state (#27)
- fix typos
danm-de added a commit that referenced this issue Mar 31, 2024
- Code cleanup
- use new language features
- use expression body for methods (if possible)
- use var statement instead of type declaration (if possible)
- fix #27 [BUG] Reciprocal of a negative fraction has invalid state (#27)
- fix typos
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

Successfully merging a pull request may close this issue.

2 participants