A few times I have told the anecdote that the singly most baffling thing I ever saw in a code review — not the most insecure, just the most “how could a real programmer have written this? how could this ever make sense?” thing — was simply a C++ variable “number_of_trucks” … declared as float. Unambiguously referring to real physical trucks in a fleet.

Reader, it’s been over ten years and I am blowing the gods damn whistle. I had edited that story to protect the guilty: the variable was named number_of_planes. It was shipped by a company whose name begins with “B” and rhymes with “GOING out of business.”

Also if you don’t know what a float is (because you’re not a programmer or have only worked with very high level languages that try to conceal these details) then my horror makes no sense so I will break it down for you

If you declare a number value to be a float, that is telling the computer two things:

  • first, that the value is allowed to be fractional and not just whole numbers (integers). This means that if someone reports that the fleet gained 2.3 planes or lost 0.01 planes, the computer would be like yeah, sure, that makes sense, let me write that down.

  • second, doing math with fractions is much more computationally complicated than whole numbers. Telling the computer a value is a “float” is explicitly telling it that you want it to use the fastest methods it has available to do the math, even if they’re not 100% accurate down to the smallest fraction of a fraction. You are saying it’s okay to lose track of a hundredth of a plane here or there as long as you count the planes really fast.

The intended use of floats is things like video game graphics: you can’t tell if the height of the enemy is 6.200002 meters or 6.2 meters exactly, and letting the computer not worry about the difference makes the calculations much faster. You should never, ever use floats for things like money, because over the course of many successive adds and subtracts, entire cents or dimes will just pop in and out of existence. Or, like, some unspecified fraction of a plane, which is probably not what you want.

@0xabad1dea A quibble: floating point numbers give exact values for addition, multiplication and subtraction involving integers if the results aren't too large.

In 32-bit floating point, you can exactly represent all integers up to 16,777,217.

> You should never, ever use floats for things like money

You can't do things like compound interest and mortgage payments with integer arithmetic! 😁

---

I have a relevant story here that you might find amusing.

1/

@0xabad1dea I worked for Ripple, not nice people at all. I got put on a project with a young man with very strong beliefs about his ability to write computer programs who had "written" a trading backend in "C++" - really C.

(One of his beliefs, for example, was that making all variables static and global made the program run faster. Indeed, he had a lot of weird beliefs about what made programs faster, but since he refused to do any form of benchmark, I was skeptical.)

2/

@0xabad1dea The project was specc'ed to use integer arithmetic, so I spent a week writing a nice 64-bit fixed point arithmetic system.

But this nice young man refused to cooperate. One of the things I'd been brought in to do was write unit tests, something he was completely against, so he'd made all these changes and never run the tests.

I got fired. But they were too stupid to remove my privileges for the repo, so I watched his "work".

3/

@0xabad1dea
First he ripped out my 64-bit fixed point arithmetic, and replaced it with `int128_t` - which works by using two hardware 64-bit ints!

My quick benchmarks showed me that this slowed all arithmetic down by a factor of about five.

But then, even worse, since he had no understanding of how computer arithmetic worked, all his intermediate calculations were in 64-bit floating point, which then got converted back into `int128_t`.

Of course, the product never came out.

/thread