3 ms·
In python, 0.3 prints as 0.3, but it's a double, so it should be 0.299999999999999988897769753748434595763683319091796875 (according to the article, and the 0.1
by wodenokoto 6y ago
In python, 0.3 prints as 0.3, but it's a double, so it should be 0.299999999999999988897769753748434595763683319091796875 (according to the article, and the 0.1+0.2 != 0.3 trick also works)
What controls this rounding?
e.g., in an interactive python prompt i get:
>>> b = 0.299999999999999988897769753748434595763683319091796875
>>> b
0.3
- has2k1 6y agoIt depends on how many decimal places you are printing >>> f'{b:.54f}' 0.299999999999999988897769753748434595763683319091796875 >>> f'{x:.16g}' 0.3 >>> f'{x:.17g}' 0.29999999999999999
- lifthrasiir 6y agoIt is the shortest decimal number that converts back to that exact FP number. There are tons of complex algorithms for that [1]. [1] See my past comment for the overview: https://news.ycombinator.com/item?id=26054079 https://news.ycombinator.com/item?id=26054079
- young_unixer 6y agoIsn't that essentially lying to the user?
- lifthrasiir 6y ago0.299999999999999988897769753748434595763683319091796875 suggests 54 fractional digits of precision, which is misleading. 0.29999999999999998 or 0.29999999999999999 are less misleading but wasteful. Remember they are not only visible to users but can be serialized in decimal representations (thanks to, e.g. JSON). In fact, most uses of FP are essentially lies, providing an illusion of real numbers with a limited storage. It is just a matter of choosing which lie to keep.