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/

@TomSwirly @0xabad1dea well Yes you CAN do compound interest etc calculations using integers only. We did it all the time in COBOL.

The trick is to let the integer represent 1/1.000.000 (.000) th of the "real" value.

@bertkoor @0xabad1dea That's indeed the "fixed point" I'm taking about later in the story, so you're right, I should have been more specific.

(But if you do that, you still have the possibility of pennies appearing and disappearing unless you are careful...)

@TomSwirly @0xabad1dea
In my experience pennies have the tendency to (dis)appear, no matter how you represent them. Being really careful is sometimes not even enough, they take you by surprise.
Every. Single. Effing. Time.

@bertkoor @TomSwirly @0xabad1dea yeah, I was rather surprised today when I implemented some software to try to calculate the interest the same way my bank does. After a bunch of trial and error, I finally got it to work (with one month on one account off by a penny but correct the next month)... as long as I never round anything to a penny except for presentation purposes

I really wish banks had to disclose their calculation methods with much more detail