Hacker News new | ask | show | jobs
by recursive 3996 days ago
Lack of understanding of floating point numbers is one of the most persistent and pervasive problems in programming. Just look at Stack Overflow on any given day. I've also helped co-workers more times than I can count on problems that stem from not understanding how floats work.
2 comments

I was working with someone who thought I was stupid for insisting on integer or fixed-point maths for what I insisted would be a particularly troublesome piece of functionality.

They were more experienced, convinced me I was being pedantic, and I myself wasn't coding on that project anyway. Months later, we had to do a panic refactor as real-world usage immediately made the app fall over.

These are the kinds of bugs I discovered as a kid writing crappy computer games for my friends and I: "floats for everyone! This is way easier!", followed later with: "floats are slow, and I don't understand half my bugs!".

Actually, on this project I found myself explaining several things I'd learnt from recreational games programming. Things that you apparently don't learn in CS (I did EE, so I wouldn't know), or a decade of doing the J2EE business middleware dance.

It's kind of funny that in digital signal processing the situation is sometimes the opposite. Floating point computation is seen as easier and more accurate but much more expensive. VHDL 2008 added synthesizable reals from demand. I've been a part of fixed point DSP on a FPGA and it really made me appreciate both sides of the float/fixed coin.
That lack of understanding also includes unwarranted use of FP when a simpler representation like fixed-point or BCD (or even integers) would work for the application, without introducing all the subtleties of FP math. Of course, this advice doesn't work in a language like JS where all numbers are FP, so in that case understanding them is the only choice.

One of the instantly recognisable symptoms of float-abuse is NaNs appearing in odd places:

https://what.thedailywtf.com/t/i-dont-look-forward-to-having...

http://i.imgur.com/viPztah.jpg