Skip to content

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

@gwhitney

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions