Skip to content

bigfraction.js: rounding fractions with very large numbers #48

Closed
@andrefs

Description

@andrefs

Hi, I'm not sure whether this has already been reported, but I couldn't find it on the other issues.

Currently the round method relies on Math.round, and for this it casts the fraction components to Numbers.
This seems to break if the fraction components are large BigInts.
Example:

const f = Fraction(
  '4096521364329291093171203267930457883773806471006747502090792325598781148452473209851945763411024272894391273488328322013368343609851019491145409276190539874605556064161234242385897557393344605006326305605571158871450061093373390954679799512035955892117049982513332807024354592989233228381581210555649737459420836464060796645449', 
  '63723676445298091081155215910708472223644182207704751442955434135446516060522326437723594988807307437639337877044454216957092285156250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
);

console.log(f.round());

// RangeError: The number NaN cannot be converted to a BigInt because it is not an integer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions