in reply to Re^5: Euler's identity in Raku
in thread Euler's identity in Raku

There may be a world where a language can do identity / symbol math - indeed there are cpan packages for this kind of thing. But core Raku does not attempt to do that. Raku is (fortunately) pretty simplistic: you get Ints, Rats and Nums (well and Complex but that's another topic). This aligns to mathematics number spaces Integers, Rational and Irrational numbers. Some operations (eg. sqrt(2)) will make an irrational number. Without symbolic math, you lose a little precision in any machine with finite word size and cannot fully restore the initial state with a round trip. Rounding can hide this.
> my $t=sqrt(2) 1.4142135623730951 > $t ** 2 2.0000000000000004
sooooo You are plumbing the edges of IEEE754 here ... and very small numbers are subject to all kinds of influences ... here is a good write up ... Back in the day many FPUs did random seed guess for division/sqrt ... even if that practice has become standardised to make the result deterministic (every pass is the same), it certainly will not require each FPU design to guess the same seed. And this ignores the fact that FPU designers will often shortcut full IEEE754 to save on transistors, various compiler settings. Not to rule out frequent chip logic design errors that do not get caught since the test sw will judge any of the near '0' as zero.

Replies are listed 'Best First'.
Re^7: Euler's identity in Raku
by syphilis (Archbishop) on Jun 08, 2021 at 01:32 UTC
    There may be a world where a language can do identity / symbol math ...

    WRT sin() and cos(), I believe it's actually fairly simple to have them return the exact rational result when applicable - as, UIM, this is only ever applicable for arguments 0, pi/6 (sin only), pi/3 (cos only), pi/2 and pi.
    These cases can be hard coded to return the exact correct value. (Of course, that coding has to also allow for integer multiples of those values.)
    It also calls for a function that takes a rational (p/q) value as it's arg.

    So, instead of calling sin(1 * 3.1415926535897931) and getting a non-zero result, we call (say) sinpi(1) and have it return the hard coded result of zero.
    If the arg given to our sinpi() function is not one of these "special values" (ie 0, 1/6, 1/3, 1/2 or 1 - or a multiple thereof), then sinpi($arg) simply returns sin($arg * 3.1415926535897931)

    ... - indeed there are cpan packages for this kind of thing

    Do you know if there are any there that have sin/cos implementations that return exact results for all of those special values ?