6 ms·
Just adding 0.1 to 0.2 in the language of your choice can be misleading because printing the number might just cut off the digits at some point. Here's an exam
by Tepix 11y ago
Just adding 0.1 to 0.2 in the language of your choice can be misleading because printing the number might just cut off the digits at some point.
Here's an example using perl5:
perl -e 'print 0.1+0.2' yields '0.3', which looks OK, however
perl -e 'print 0.1+0.2-0.3' reveals '5.55111512312578e-17'
So, it's better to print the result of 0.1+0.2-0.3
For what its worth, perl6 passes this test with flying colours.
- static_noise 11y ago> For what its worth, perl6 passes this test with flying colours. Do we really expect calculations on floating point numbers to be the same as calcualtions with real numbers? The only problem I see is that often the floating point format is not specified properly. When one writes "0.3", what does it actually mean? Does the format the compiler uses internally conform to IEEE 754? Is it 32bit, 64bit, 80bit or something else?
- Piskvorrr 11y agoIn other words, there are about 5648798746465 possible interpretations of the "(\+|-|)[0-9]+.[0-9]+" format, and the problem is with mismatched assumptions ("of course I'm using the One Correct Interpretation, like everyone else!").
- panic 11y agoThe only problem I see is that often the floating point format is not specified properly. When one writes "0.3", what does it actually mean? It's perfectly well-specified. If you are using double-precision floating point, "0.3" means 0x3fd3333333333333 (as bits), or 0.299999999999999988897769753748434595763683319091796875 (as an exact number: the closest representable number to 0.3).
- static_noise 11y agoWhen your compiler does its internal calculations in double precision float you are right. But why should it do so? Do all languages specify the details of how floating point input is handled by the compiler/interpreter?
- tinco 11y agoI think so, yes.
- gsnedders 11y agoMost languages do (though implementation bugs are common—plenty of languages where 64-bit precision is required actually end up being implemented in such a way that 80-bit precision is used on x86). That said, a lot of low-level languages don't: C doesn't guarantee this, for example. C doesn't even mandate IEEE 754, and it merely requires that "float" is a subset of the values of "double" which is itself a subset of the values of "long double".
- amyjess 11y agoPerl 6 doesn't use IEEE 754 by default at all. It internally stores decimal literals as rational numbers, with numerator and denominator in separate fields, and it only resolves back to a decimal number when you convert it to a string.
- marcosdumay 11y agoThose 0.1, 0.2, and 0.3 numbers do not have any floating point representation at all. Computers just can not use them.
- Grue3 11y agoBut these numbers are perfectly computable! Sure they cannot be represented in the commonly used binary implementation of floating point numbers, but that doesn't mean computers cannot operate with these numbers at all.
- amalcon 11y agoWell, they can't be specified as IEEE 754 floats. A computer can still use those numbers in the form of decimal fixed-point or numerator-denominator formats.
- Kristine1975 11y agoSince 2008 IEEE-754 also supports decimal floating-point: https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange... >There are three binary floating-point basic formats (encoded with 32, 64 or 128 bits) and two decimal floating-point basic formats (encoded with 64 or 128 bits)
- skrebbel 11y agoOf course they do, just not a binary floating point representation. They can be represented with decimal floating point just fine and there are plenty of programs and libraries that can do this. It's very commonly used in finance, for example.
- philh 11y ago> printing the number might just cut off the digits at some point. I'm not sure, but I think it's not really about cutting off the digits (although that's the effect it has). At least with python, I think what happens is: * repr gives you the decimal number with the exact value of the floating-point number, i.e. 0.30000000000000004 * print gives you the least-precise decimal number whose closest floating-point representation is equal to the floating-point number That is, there are (infinitely) many decimal numbers such that the closest floating-point representation has the value 0.30000000000000004. One of these decimal numbers is 0.3, and of all such decimal numbers, this has the fewest digits. So that's the one that gets printed.
- deleted 11y ago[deleted]
- philh 11y agoSomeone pointed out that this isn't correct, and then deleted their comment. (?) I now think the behiviour I ascribed to print, is actually what repr does. I'm not sure about print, but I guess it is just rounding to a certain number of places. >>> from decimal import Decimal >>> Decimal(0.1+0.2) Decimal('0.3000000000000000444089209850062616169452667236328125') >>> Decimal(0.3) Decimal('0.299999999999999988897769753748434595763683319091796875') >>> Decimal(0.300000000000000000000000000001) Decimal('0.299999999999999988897769753748434595763683319091796875') >>> print 0.300000000000000000000000000001 0.3 >>> repr(0.300000000000000000000000000001) '0.3' >>> D(0.30000000000000004) Decimal('0.3000000000000000444089209850062616169452667236328125') >>> repr(0.299999999999999988897769753748434595763683319091796875) '0.3' >>> repr(0.30000000000000004) '0.30000000000000004' >>> print 0.30000000000000004 0.3
- jamesdutc 11y agoI believe this is relevant: https://bugs.python.org/issue1580 https://bugs.python.org/issue1580
- evanpw 11y agoThere's some discussion here: https://docs.python.org/3.1/tutorial/floatingpoint.html https://docs.python.org/3.1/tutorial/floatingpoint.html. Basically: eval(repr(x)) == x is guaranteed, and some attempt is made to give the shortest such string representation for Python 3.1+ (and 2.7?), but it's not guaranteed on all platforms.
- taneq 11y agoWhat does it mean to "pass" this test? Just that the software is massaging its output to match human expectations rather than giving the most accurate (if not the prettiest) answer?
- poizan42 11y agoJust say (0.1+0.2)*10-3. That works everywhere no matter the amount of rounding.
- static_noise 11y agoYou've just invented fixed point numbers.
- kazinator 11y ago> Just adding 0.1 to 0.2 in the language of your choice can be misleading because printing the number might just cut off the digits at some point. It's amazing that someone went to the trouble to register a custom domain for this and put up a nicely formatted page, yet doesn't explain the above: that it's not (just) a matter of representation, but printing precision.
- erikwiffin 11y agoThat's interesting, I had no idea. There's probably a way more detailed Rosetta Code style project that could cover all these nuances, but for a single page demo, there's only so much detail I can get into before it would be more confusing than helpful. If you'd like to flesh out the perl section a bit though, maybe include the differences between 5 and 6, I'd be happy to merge a pull request.
- hahainternet 11y agoYou've had a perl6 pull request outstanding for a while. The differences are far too big to list simply though. Ultimately Perl6 uses Rationals by default, not Floats.
- amyjess 11y ago> For what its worth, perl6 passes this test with flying colours. This is because Perl 6 decimal literals are stored in a built-in rational type called Rat, which internally stores numbers as ratios between integers. So that 0.1, 0.2, 0.3 are internally stored as the tuples (1,10), (2,10), and (3,10), and the decimal value you see is just the string representation and not the actual internal value. If you want your decimal literal to be floating-point (called Num in Perl 6), you want to add e0 to the end. To wit: perl6 -e 'say 0.1+0.2-0.3' reveals '0' perl6 -e 'say 0.1e0+0.2e0-0.3e0' reveals '5.55111512312578e-17'
- deleted 11y ago[deleted]