9 ms·
FWIW and for all its other shortcomings, Raku (nee Perl 6) handles common arithmetic in a manner that wouldn't surprise a mathematician. Or non-mathematician, f
by emptybits 7y ago
FWIW and for all its other shortcomings, Raku (nee Perl 6) handles common arithmetic in a manner that wouldn't surprise a mathematician. Or non-mathematician, for that matter. Witness:
$ python3 -c 'print(0.3 == 0.1 + 0.2)'
False
$ ruby -e 'puts 0.3 == 0.1 + 0.2'
false
$ perl6 -e 'say 0.3 == 0.1 + 0.2'
True
- SamReidHughes 7y agoFor your amusement, Go: fmt.Print(0.3 == 0.1 + 0.2) // => true https://repl.it/repls/SubtleQuarrelsomeMaintenance https://repl.it/repls/SubtleQuarrelsomeMaintenance
- tomtomtom777 7y agoInteresting. I thought Go used standard IEEE-754 floats. Why does it have this result?
- SamReidHughes 7y agoThey evaluate constant expressions exactly at compile time, then convert to float.
- WalterBright 7y agoThat works great until people complain that it works differently at compile time, and they have a point.
- SamReidHughes 7y agoYeah. On the other hand, they won’t get any surprises when cross-compiling. Edit: Okay, maybe Go devs saved themselves the work of having floating point emulation. Edit: No, they are folding both constant expressions exactly and actual floating point operations, going by assembly output.
- gpderetta 7y agoGCC at least evaluates constant FP expressions using a model of the target machine FP unit, so that compile time and runtime expressions behave identically.
- tomtomtom777 7y agoGo uses IEEE-754. The behaviour of these may be odd for those new to the quirks of the float, but I thought the behaviour is nowadays standardized and reproducable across platforms.
- tomtomtom777 7y ago> No, they are folding both constant expressions exactly and actual floating point operations, going by assembly output. Yes. The behaviour is quite odd and unexpected. It seems they have different rules for when to apply the constant version of floating math than what constitutes actual compile time evaluation. const a float64 = 0.1; const b float64 = 0.2; const x float64 = 0.1 + 0.2; // Const evaluation, "rational" math const y float64 = a + b; // Const evaluation, IEEE-754 math fmt.Println(x == y) // false ??!? This seems quite a horrible approach. EDIT: Or how about this one: const x float64 = 0; fmt.Println(x + 0.1 + 0.2 == 0.1 + 0.2 + x ); // false! fmt.Println(x + 0.1 + 0.2 == x + (0.1 + 0.2) ); // false!
- SamReidHughes 7y agoThe literal folding is specified language behavior, the other is just an optimization.
- jancsika 7y agoRaku: Here's a trivial example that shows a sophisticated approach to getting you the precise numbers you want for all your numeric needs. Go: Here's a trivial example.
- enriquto 7y agoAs a mathematician, I am actually very surprised by perl's 6 behavior here. What the hell is going on? Does it use fixed point arithmetic or what?
- SamReidHughes 7y agoThey have this totally irrational idea of having "rational" numbers. https://docs.perl6.org/language/numerics#Rational https://docs.perl6.org/language/numerics#Rational By irrational I am not joking, these sorts of number systems are complicated and glitchy, in this case with program-destroying magic like autoconversion to floating point when you hit a 2^64 denominator, and, don't click this URL, please, just read it: https://docs.perl6.org/language/numerics#Zero-denominator_rationals https://docs.perl6.org/language/numerics#Zero-denominator_ra...
- lizmat 7y ago> totally irrational idea Could you expand on why it is a totally irrational idea? Seems pretty rational to me. But then I'm not really a mathematician. > autoconversion to floating point when you hit a 2^64 denominator Please note that this is after normalization. And it has come from practicality: having the denominator also be an BigInt, slows down the use of the Rational number significantly. However, if you do need that type of precision, then you can by using infectious FatRats: any expression with a FatRat, will result in a FatRat (unless explicitly coerced to something else, of course). With regards to the Zero-denominator Rats: they are just special cases just as IEEE has special cases for -Inf, Inf and NaN. So what's the problem there?
- SamReidHughes 7y agoIt adds gratuitous corner cases to the language.
- lizmat 7y agoFWIW, Perl 6 prefers to be called Raku now.
- 7y ago
- mansoor_ 7y agoArguable that this is a good thing.