One small tip about printf for floating point numbers. In addition to "%f", you can also print them using "%g". While the precision specifier in %f refers to digits after the decimal period, in %g the precision refers to the number of significant digits. The %g version is also allowed to use exponential notation, which often results in more pleasant-looking output than %f. printf("%.4g", 1.125e10) --> 1.125e+10 print…
0.30000000000000004
51–60 of 422 posts
Re: 0.30000000000000004
#52Earlier quoted context omitted.
Reminds me of this Inigo Quilez article on experimenting with rendering using rational numbers: https://iquilezles.org/www/articles/floatingbar/floatingbar....
I actually ran into a bug recently while implementing my first raytracer, where the point calculated from the sphere-intersect test would just occasionally end up inside the sphere due to floating point imprecision, so the diffuse sample rays would have their origins completely in the dark, leading to randomly black pixels. Solved it by bumping every intersection out by 0.01 in the direction of its normal. And then o…
Re: 0.30000000000000004
#53Edit: Oh wait, it's listed in the main article under Raku. Forgot about the name change.
Re: 0.30000000000000004
#54Earlier quoted context omitted.
Decimal floating point is standardized since 2008: https://en.wikipedia.org/wiki/Decimal_floating_point#IEEE_75... But it's still not much used. E.g. for C++ it was proposed in 2012 for the first time http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n340... then revised in 2014: http://open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3871.ht... ...and... silence? https://www.reddit.com/r/cpp/comments/8d59mr/any_u…
It's not important to most people, because decimal floating point only helps if your UI precision is exactly the same as your internal precision, which almost never happens. Seeing the occasional 0.300000000000004 is a good reminder that your 0.3858372895939229 isn't accurate either.
One can argue that nothing is important to most people.
The correct calculations involving money, up to the last cent, are in fact important for people who do them or who are supposed to use them. I've implemented them in the software I've made to preform some financial stuff even in eighties, in spite of all the software which used binary floating point routines. And, of course, from the computer manufacturers, at least IBM cares too:
https://www.ibm.com/support/pages/decimal-floating-point-use...
Apparently, there are even processors which supports these formats in hardware. It's just still not mainstream.
Re: 0.30000000000000004
#55Earlier quoted context omitted.
> Unless you have a floating point model that supports arbitrary bases See also binary coded decimals. https://en.wikipedia.org/wiki/Binary-coded_decimal
That's not a floating point.
> Programmable calculators manufactured by Texas Instruments, Hewlett-Packard, and others typically employ a floating-point BCD format, typically with two or three digits for the (decimal) exponent.
Re: 0.30000000000000004
#56I still remember when I encountered this and nobody else in the office knew about it either. We speculated about broken CPUs and compilers until somebody found a newsgroup post that explained everything. Makes me wonder why we haven't switched to a better floating point model in the last decades. It will probably be slower but a lot of problems could be avoided.
Re: 0.30000000000000004
#57That’s only formatting. The other (and more important) matter, — that is not even mentioned, — is comparison. E. g. in “rational by default in this specific case” languages (Perl 6), > 0.1+0.2==0.3 True Or, APL (now they are floats there! But comparison is special) 0.1+0.2 0.3 ⎕PP←20 ⋄ 0.1+0.2 0.30000000000000004 (0.1+0.2) ≡ 0.3 1
Re: 0.30000000000000004
#58Re: 0.30000000000000004
#59I still remember when I encountered this and nobody else in the office knew about it either. We speculated about broken CPUs and compilers until somebody found a newsgroup post that explained everything. Makes me wonder why we haven't switched to a better floating point model in the last decades. It will probably be slower but a lot of problems could be avoided.
> Makes me wonder why we haven't switched to a better floating point model in the last decades. It will probably be slower but a lot of problems could be avoided. Pretty much all languages have some sort of decimal number. Few or none have made it the default because they're ignominiously slower than binary floating-point. To the extent that even languages which have made arbitrary precision integers their default fi…
You can strike the "none". Perl 6 uses rationals (Rat) by default, someone once told me Haskell does the same, and Groovy uses BigDecimal.
Re: 0.30000000000000004
#60I remember in college when we learned about this and I had the thought, "Why don't we just store the numerator and denominator?", and threw together a little C++ class complete with (then novel, to me) operator-overloads, which implemented the concept. I felt very proud of myself. Then years later I learned that it's a thing people actually use: https://en.wikipedia.org/wiki/Rational_data_type
But rationals are more expensive to compute with (compared to floating-point; this is another example of the trade-off between performance and accuracy.)