You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide access to numerator and denominator of fraction via num() and den() helpers #3595
Am I missing something, or is it the case that there is no way to get the numerator and denominator of a Fraction in the mathjs expression language? For Complex numbers, we have re(z) and im(z) but I don't think that there's anything analogous for Fractions, and if f is a fraction, then f.n and f.d are disallowed property accesses, just like z.re and z.im are for Complex. But these are important properties of a rational number, that we should definitely provide some way of obtaining.
If I am correct that there is currently no way of obtaining them, we should definitely make them available, possibly by one (or both?) of these routes: (would definitely love a clear opinion on which way to go)
Add functions numerator and denominator. I think they would only be defined on Fractions (and I guess bigints) though, which is a little weird for mathjs, although they would then also work on any types that have automatic conversion to Fraction (not sure what types those are, if any).
In the safety code gatekeeping property access, open up specific allowances that would make f.n and f.d (and maybe f.s) legal (and then one would probably do z.re and z.im for Complex while at it).
It would be useful indeed to be able to have access to the numerator and denominator of fractions. It's indeed limited right now due to a quite strict access control (for security reasons). I'm both fine with allowing access to the properties on Fraction and Complex. Or indeed create a helper function numerator(fraction) and denominator(fraction). I think I have a slight preference for helper functions numerator(fraction) and denominator(fraction) but both can be fine.
In case we go for opening up access to properties, we need to be careful not to introduce security holes and we need to check what the performance impact of the changes is.
Ok, we can go via helper functions. Which do you prefer:
numerator() and denominator()? -- Very clear full words, but lengthy.
Or
num() and den()? -- arguably more in the spirit of existing helper functions re() and im() for Complex.
I have an extremely slight preference for the latter, given the Complex precedent, but happy to go either way. Just post a decision, and then one of us can change the labels to make clear this is ready for implementation.
changed the title [-]No access to numerator and denominator of fraction in expressions?[/-][+]Provide access to numerator and denominator of fraction via num() and den() helpers[/+]on Nov 26, 2025
@josdejong@gwhitney I have now created a pr that introduces the num and den operators using (re) and (Im) as a a benchmark for consistency sake, also included extensive tests for possible scenarios. please take a look, happy to accommodate any feedback for this feature
Activity
josdejong commentedon Nov 19, 2025
It would be useful indeed to be able to have access to the numerator and denominator of fractions. It's indeed limited right now due to a quite strict access control (for security reasons). I'm both fine with allowing access to the properties on Fraction and Complex. Or indeed create a helper function
numerator(fraction)anddenominator(fraction). I think I have a slight preference for helper functionsnumerator(fraction)anddenominator(fraction)but both can be fine.In case we go for opening up access to properties, we need to be careful not to introduce security holes and we need to check what the performance impact of the changes is.
gwhitney commentedon Nov 20, 2025
Ok, we can go via helper functions. Which do you prefer:
numerator() and denominator()? -- Very clear full words, but lengthy.
Or
num() and den()? -- arguably more in the spirit of existing helper functions re() and im() for Complex.
I have an extremely slight preference for the latter, given the Complex precedent, but happy to go either way. Just post a decision, and then one of us can change the labels to make clear this is ready for implementation.
josdejong commentedon Nov 26, 2025
Thanks. I have a slight preference for the short
num()andden()too.[-]No access to numerator and denominator of fraction in expressions?[/-][+]Provide access to numerator and denominator of fraction via num() and den() helpers[/+]AnslemHack commentedon Dec 2, 2025
@josdejong happy to hop on this!
AnslemHack commentedon Dec 3, 2025
@josdejong @gwhitney I have now created a pr that introduces the num and den operators using (re) and (Im) as a a benchmark for consistency sake, also included extensive tests for possible scenarios. please take a look, happy to accommodate any feedback for this feature
feat: Fraction numerator and denominator helper functions (#3605)