Provide access to numerator and denominator of fraction via num() and den() helpers · Issue #3595 · josdejong/mathjs · GitHub
Skip to content

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

Closed

Description

@gwhitney

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

Activity

  1. josdejong commented on Nov 19, 2025

    @josdejong
    Owner

    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.

  2. gwhitney commented on Nov 20, 2025

    @gwhitney
    CollaboratorAuthor

    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.

  3. josdejong commented on Nov 26, 2025

    @josdejong
    Owner

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

  4. 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
  5. AnslemHack commented on Dec 2, 2025

    @AnslemHack
    Contributor

    @josdejong happy to hop on this!

  6. AnslemHack commented on Dec 3, 2025

    @AnslemHack
    Contributor

    @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

  7. added a commit that references this issue on Mar 5, 2026
    f7c10b1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions