mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Provide access to numerator and denominator of fraction via num() and den() helpers

Open gwhitney opened this issue 1 month ago • 5 comments

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).

gwhitney avatar Nov 14 '25 18:11 gwhitney

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.

josdejong avatar Nov 19 '25 16:11 josdejong

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.

gwhitney avatar Nov 20 '25 20:11 gwhitney

Thanks. I have a slight preference for the short num() and den() too.

josdejong avatar Nov 26 '25 09:11 josdejong

@josdejong happy to hop on this!

AnslemHack avatar Dec 02 '25 23:12 AnslemHack

@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

AnslemHack avatar Dec 03 '25 00:12 AnslemHack