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 float makes the bits not sink in water.

@0xabad1dea Sounds like the perfect use case for Boeing then. That door did pop out of existence, after all.

@wasmitnetzen
@0xabad1dea

Only 0.999999999 of the plane landed.

@wryanfisher @wasmitnetzen @0xabad1dea

Less than 0.999, assuming the door weighed 200kg

@naught101
@wasmitnetzen @0xabad1dea

Fair. Also, all of the plane landed. Just, not together. ;D

@wasmitnetzen @0xabad1dea The door plug is maybe 0.002 planes.
@wasmitnetzen @0xabad1dea the door size was set to 0.99989 and the port size to 1.0000003
@0xabad1dea
Floats also allow some languages to write cool documentation like “If <language> encounters a number beyond the bounds of the int type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the int type will return a float instead.” 🙃
@0xabad1dea Well, I suppose Alaska Airlines did lose approximately 0.01 planes recently ✈️

@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

@TomSwirly @0xabad1dea

Many Many years ago, I have written systems using "double" types/variables for integer values -- in languages where there was no "long integer" type. In those cases, where you need to store exact integer values over 32,767, ... Well, the "double" type can do that. Sure, it's slow and introduces risks. But when you don't have any viable "long int" type, you use what you have.

@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 @0xabad1dea You ain't joking, I have gotten bit with that before. though not in Cobol. The 64-bit fixed point project was fun though - partly because I'd done this before, partly because I knew no one else would be able to find my mistakes and by that point I was pretty sure it wasn't ever going to market... ah well!

(I never wrote any Cobol but I did help an awful lot of undergrads debug their Cobol programs a long time ago!)

@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

@TomSwirly @0xabad1dea

You can't do them with integers, except you *CAN* if you kinda... cheat!

You definitely don't use floats.

You use fixed-point with as many decimal places as you need to make sure your calculations will always be as accurate as you need.

It's basically two integers superglued together.

@nyankat @TomSwirly @0xabad1dea also, bankers rounding is a thing to prevent error from accumulating, and can be used to make fixed point more practical

(bankers rounding basically special cases the .5 situation, rounding it to the nearest *even* number, instead of always up. so, rounding 0.5 to the nearest integer produces 0, rounding 1.5 to the nearest integer produces 2.)

@bhtooefr @0xabad1dea @TomSwirly Oh yeah! I did know about that, but had forgotten. Geez, I think I learned that in fuckin' Junior High advanced math and then used it never lol

Fuck, that was literally 30 years ago

@TomSwirly @0xabad1dea Sure you can, it's called "fixed point" and it's quite common.
@landley @0xabad1dea Ah, sorry, yes, later in this thread I tell a funny story about writing a fixed point system even...
@TomSwirly @0xabad1dea Navigating reply threads in mastodon is... not ideal.
@landley @0xabad1dea Yes, indeed, but at least this place is, well, not Musk-free but at least Musk-lite.
@0xabad1dea sometimes you don't have a truck yet, but you need one now, so you borrow one. that's how you get a floated truck.

@Xkeeper @0xabad1dea

Sometimes you drive your CyberTruck into deep water, because EM said it would be fine. Aaaaaand, ... It's probably *not* going to turn out fine. 🙄

@JeffGrigg @Xkeeper @0xabad1dea are the batteries lithium? asking for a friend,

@0xabad1dea

But now it makes sense. Number of planes can go from 1.0 to 0.99 in flight.

@petealexharris @0xabad1dea

If a sufficient number of parts falls off in flight all that eventually remains is a floating point. Sound plausible 😝

@bluish_gecko

If too many parts fall off, it stops floating.

@petealexharris @0xabad1dea

@BenAveling @petealexharris @0xabad1dea

Mathematically speaking a point is an infinitesimally small fraction of space.
So if there’s nothing left to float it can’t fall 😄
So it comes down to the question which „epsilon“ is the acceptable round-off for „zero planes“!

@bluish_gecko A sufficiently small part of a plane could be small enough to float for quite a long time. But we're getting into the Jumbo-Max-Of-Theseus at this point.

@petealexharris @0xabad1dea

@0xabad1dea who amongst us hasn't found that we've got 3874.297 planes?
@0xabad1dea I was about to laugh along with you, but from the clues you gave, that is the exact company where a fractional number of planes is a very useful everyday metric.
@TomF @0xabad1dea "don't worry, we're still flying half a plane"
@TomF @0xabad1dea Actually, that's true. Iirc they have at some point been in the business of selling time shares of planes.
@0xabad1dea Oooh, that's smart! They made sure to have the software ready for partial planes, losing emergency doors and stuff. Ahead of their time, really.
@macberg @0xabad1dea you'd think so, but then again that sort of information tends to be more useful when you know where it's missing from. or that it's missing from your plane, and not anybody else's.
@0xabad1dea my irrational number of completed planes - Pi or e planes completed, we're not really sure how complete the partial one is

@0xabad1dea I worked at a large investment bank years ago where all currencies were stored as floats EXCEPT GBP which was store as Ints in pence. (Yes, even Yen that has no fractional value).

Of course, on occasion, some programmer would forget to * or / by 100 to get GBP and all hell would break loose 😂

It rhymed with Reddit and a European Alpine country with a square flag who DID go bust 😂

@bazscott Currency as floats 😬 Isn't that part of floats vs ints 101
@bazscott @0xabad1dea aaah floats for currency!?! That's specifically what BCD was invented for!
@0xabad1dea I'm just waiting for you to learn about javascript and lua :p
@sotolf I am more aware than you could possibly imagine
@0xabad1dea oh no. Dare I ask?
@drdnar I’ve been a professional source code security reviewer since 2011, that’s all 😂 cursed all the way down
@0xabad1dea That one truck got hit by a train and only a fraction remained...
@0xabad1dea Are you certain that Boeing planes can't exist as fractions?

@bruce @0xabad1dea

Well, I do remember a few MAXes that lost doors and at least a 757 that lost a good third of the roof of the cabin