Skip to content

Distinguish "raw" fractions and irreducible fractions #119

@Gouvernathor

Description

@Gouvernathor

Reducing a franction to an irreducible form can be a relatively expensive operation compared to basic math operations (addition, subtraction, multiplication, division).
In another library, which is Fraction.ts (github version), I found that it could be handy to have two distinct types, a "raw" fraction type that does the fastest math available for basic operations, at the cost of the end fraction not being in an irreducible form, and implement a toIrreducible method that converts it to irreducible. And a derived IrreducibleFraction type that returns a RawFraction for most methods but overrides some of them such as toIrreducible.
That would allow, if I have a complex expression using a lot of math operations, to call the reduction to irreducible only at the end of the calculation − or not at all in fact, if not necessary.

As that can be a breaking change, there could be the following solution:

  • the RawFraction, that offers no guarantees of being in irreducible form
    • it could be chosen, or not, to keep the denominator positive, as that is relatively costless compared to the rest
    • its methods return RawFraction objects
    • it has toIrreducible()
  • the IrreducibleFraction, that guarantees being always in irreducible form
    • its methods return RawFraction objects
    • it has a trivial toIrreducible() method (returning self or a clone of self)
  • the Fraction, that guarantees being always in irreducible form, and meant too be used on its own
    • it could derive from IrreducibleFraction for implementation's sake
    • each of its methods and operators basically calls toIrreducible at the end and returns a Fraction
    • it doesn't need to have a public toIrreducible() method, though if it's derived it will be inevitable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions