-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Make fraction equality less strict #2921
Make fraction equality less strict #2921
Conversation
2fd421f
to
0b798c6
Compare
Thanks Bruno, this PR looks solid. I have some doubts about whether this approach is a good one. The change needed in the unit test
Thinking aloud here: if we want to keep (2) strict, we should see if we can solve the issue the other way around: instead of comparing a number and a Fraction by converting the number into a Fraction and then comparing two Fractions. Maybe we should turn the Fraction into a number and then compare the numbers using Another option would be to create a special case in the `equality functions to handle comparing numbers and Fractions differently, but I'm not sure we should go that way. Alternatively, we could simply accept this limitation: const math = create(all, { number: 'Fraction' })
math.createUnit('rotation', {definition: '360 deg', aliases: ['tour','tr']})
const a1 = math.unit(360, 'deg') // a1 has a value being a number
const a2 = math.unit(math.fraction(360), 'deg') // a2 has a value being a Fraction
const a3 = math.fraction(math.unit(360, 'deg')) // not supported right now but we could implement that
const b = math.unit(1, 'rotation') // b has a value being a Fraction
expect(b.equals(a1)) // Error (cannot convert the internal value of a1 to a Fraction)
expect(b.equals(a2)) // true
math.evaluate('360 deg == 1 rotation') // true We could consider implementing support for I have to think this through a bit more 🤔 . Do you have any thoughts? |
Thank Jos for the suggestions. Your point about not converting irrational numbers into fractions makes sense, that is not mathematically correct. Your suggestion of turning the Fraction into a number and then comparing the numbers using nearlyEqual seems like a possible solution, IMHO, but for now the most straightforward approach would be supporting |
Yes agree. I think there are two underlying issues here. The first is discussed in #2734: mathjs functions do not automatically convert number inputs into the configured numeric data types (like BigNumber or Fraction). And second: the built-in units are created with a For now I think we should keep things simple:
Agree? |
Agree. Let's close this PR and do the |
👍 @brunoSnoww I've created a PR implementing |
This PR fixes equality of fraction when there is loss of precision during conversions as addressed in #2918. It replaces the strict equality when dealing with fractions by the function
nearlyEqual
. It also changes the equals function inside the prototype ofFraction
in order to make consistent with the other changes.