Provide access to numerator and denominator of fraction via num() and den() helpers
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
numeratoranddenominator. 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.nandf.d(and maybef.s) legal (and then one would probably doz.reandz.imfor 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.
Thanks. I have a slight preference for the short num() and den() too.
@josdejong happy to hop on this!
@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