in reply to Re^4: Behaviour of int() unexpected
in thread Behaviour of int() unexpected

IIRC do some languages offer a data type for fractions.

So 4/6 is stored as a tuple (2,3)

I think Raku is one of them.

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^6: Behaviour of int() unexpected
by syphilis (Archbishop) on Mar 11, 2025 at 10:06 UTC
    Yes, Raku is one such language.
    Perl also provides this through the Math::BigRat module, and also the bigrat pragma:
    > >perl -Mbigrat -le "$x = 4; print $x/6;" 2/3
    Math::GMPq is another module that provides for rational arithmetic.

    Cheers,
    Rob
Re^6: Behaviour of int() unexpected
by LanX (Saint) on Mar 13, 2025 at 03:30 UTC
    > data type for fractions.

    I should have mentioned that any periodic number can be represented loss free as fraction, and vice versa.

    (I already covered this in school in 7th or 8th grade but curriculums may vary.)

    So, within the boundaries of integer precision you can have loss free calculations.

    But with some overhead for normalization. (I suppose they can't be delayed ...🤔 ;)

    This could be sufficient for financial applications, though I'd rather prefer to calculate with the smallest unit in integer. ( Like cent, or deci-cent or whatever depending on legal specifications)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery