5 ms·
"Try 0.1 + 0.2 == 0.3 in some other language, then try it in Perl 6." Whoa, wtf python: $ python Python 3.4.3 (default, Jul 13 2015, 12:18:23) [G
by natch 11y ago
"Try 0.1 + 0.2 == 0.3 in some other language, then try it in Perl 6."
Whoa, wtf python:
$ python
Python 3.4.3 (default, Jul 13 2015, 12:18:23)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.1 + 0.2 == 0.3
False
>>>
- schoen 11y agoPeople have also very often been freaked out by >>> 1/2 0 >>> 2/3 0 ... and this has changed from Python 2 to Python 3. But the floating-point vs. rational/decimal hasn't. You might get what you want from >>> import decimal >>> decimal.Decimal('0.1') + decimal.Decimal('0.2') == decimal.Decimal('0.3') True Still, that seems like a bunch of extra work compared to the more obvious way to type in numbers!
- natch 11y agoCue the sophomoric replies about IEEE floating point number representation. One of the cool things about Perl, though I haven't used it much for years and have been enjoying my break from it (hello, Python!) is the DWIM philosophy.
- mollmerx 11y agoI had to look that one up: DWIM = Do What I Mean
- chubot 11y agoWhy is this surprising? Floats are defined by the hardware essentially, not the language. And this is how they work: they're binary, not decimal. Computers are binary, not decimal. 0.1 can't be represented exactly as binary. Python has a decimal type, if that's what you want: https://docs.python.org/2/library/decimal.html https://docs.python.org/2/library/decimal.html I consider it a basic point of computer literacy to understand this.
- _pmf_ 11y ago> I consider it a basic point of computer literacy to understand this. Yes, but just because fractional notation defaults to a floating point representation in most languages does not mean that the option of defaulting to decimal/rational notation at the language level should be ruled out generally.
- raiph 11y ago> Floats are defined by the hardware essentially, not the language. Floats are storage formats for approximations of numbers. Floats are, by definition, not relevant if one wants to store an exact number exactly. > 0.1 can't be represented exactly as binary. 1 can be represented exactly as binary. And so can 10. And thus 1 (numerator) divided by 10 (denominator) can be represented by two (binary -- or any-other-ary-for-that-matter) numbers. This isn't about (just) decimals. The same thing applies to non-decimal fractions like 1/3 or 56,123 / 7329. Some languages prefer to store exact representations of rational numbers. Perl 6 is by no means the first.
- haberman 11y ago> Floats are storage formats for approximations of numbers. Floats are, by definition, not relevant if one wants to store an exact number exactly. Floats can store integer values exactly up to an upper limit (2^24 IIRC), which is often useful. And they can store binary fractions exactly up to a point. No number representation can represent all numbers exactly. Each can store some subset of all numbers exactly.
- raiph 11y agoFwiw I can't tell if you're being pedantic for fun or not. I'm going to stick with fun... > Floats can store integer values exactly up to an upper limit (2^24 IIRC) You're right, for relatively small integers of 8 digits or so. Here's a fun place to note that on the machine I just tried the Rakudo Perl 6 compiler will happily exactly store integers with hundreds of thousands of digits. > which is often useful. As in you've written such code in dozens of programs? > And [floats] can store binary fractions exactly up to a point. 0.1 is already "past" that point which is even less impressive than 8 digit integers. > No number representation can represent all numbers exactly. Each can store some subset of all numbers exactly. Well duh. No computer can compute all computable things. Each can compute some things. My machine became unhappy when I asked it to store a number that would require a terabyte of RAM. Are these sorts of thing really worth mentioning when the starting point is just 0.1 + 0.2 == 0.3?