0xabad1dea,

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.”

0xabad1dea,

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.

dcoderlt,
@dcoderlt@ohai.social avatar

@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.” 🙃

BradRubenstein,

Nobody has ever returned a float that was made with butter brickle ice cream and root beer, just sayin'.

@dcoderlt @0xabad1dea

wasmitnetzen,

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

john,
@john@sauropods.win avatar

@0xabad1dea I really hope this was in some business inventory thingamajig and not in maintenance or flight software.

JHein,
@JHein@mastodon.social avatar

@0xabad1dea

A float makes the bits not sink in water.

jonathanavt,

@0xabad1dea It isn’t quite like that. Floats are still bound by hard rules. The heuristic that results are off by a millionth or so is not one of those rules. All integers up to ±2^24 have exact representations in binary32. Floats might as well be native integers under that constraint. Still, it’s very questionable to say the least.

0xabad1dea,

@jonathanavt my sibling in posting, pasting the entire Wikipedia article on IEEE floating point into my thread would not help non-programmers understand the fundamental general issue

syntaxseed,
@syntaxseed@phpc.social avatar

@0xabad1dea The bigger horror here is that this represents a programmer who didn't know the basics behind dealing with numbers in code. Where else did they make this mistake? And how did no one catch it. 😱

aris,

@0xabad1dea Javascript developers intensively sweating

TomSwirly,
@TomSwirly@toot.community avatar

@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,
@TomSwirly@toot.community avatar

@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/

TomSwirly,
@TomSwirly@toot.community avatar

@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/

TomSwirly,
@TomSwirly@toot.community avatar

@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

adb,

@0xabad1dea Having spent too much of my career dealing with floating-point numbers in computers, I'm delighted to note that a float is also a type of steel file used in shaping wood. Also I'm considering that a Guinness-and-cherry-ice-cream float could be a research topic.

sophieschmieg,

@0xabad1dea it would make sense for ships, though. Because you know, they float.

juandesant,
@juandesant@astrodon.social avatar
chux0r,

@0xabad1dea My mind was blown when it was suggested to me to just calculate everything to desired-precision power of 10, and just use int. Cheat in a decimal point as a matter of print formatting. Oh- YEAH! Life changing, yo =)

Lulukaros,
@Lulukaros@mastodon.social avatar

@0xabad1dea i thought floats were supposed to be very accurate and can be used for money, and doubles are the ones that aren't accurate

0xabad1dea,

@Lulukaros oh no oh no who told you that

A double is a more accurate subtype of float and you can’t use either for money

Lulukaros,
@Lulukaros@mastodon.social avatar

@0xabad1dea ohhh, i definitely remember some C tutorial on youtube stating otherwise, what to use for money then?

0xabad1dea,
reverseics,

@0xabad1dea a plane thats missing a door is only 0.97879 planes…

ill see myself out.

blinken,
@blinken@hachyderm.io avatar

deleted_by_author

drdnar,
@drdnar@hachyderm.io avatar

@blinken @0xabad1dea This hurts so much. It needs a trigger warning.

vriesk,
@vriesk@hachyderm.io avatar

@0xabad1dea but, have you seen timestamps (expressed as seconds since the Epoch) declared as a 32-bit float?

Because I did. In production code. In a BIG company.

The result was a 2-minute precision of said timestamps, which triggered interesting bugs elsewhere.

0xabad1dea,

@vriesk ada lovelace christ

sabik,
@sabik@rants.au avatar

@0xabad1dea @vriesk
The famous one is timestamps as float, seconds since boot

So it works in demo and training, but not in the field

jernej__s,

@sabik @0xabad1dea @vriesk So it works for about half a year, then weird stuff starts happening?

jbaggs,

@vriesk @0xabad1dea That's appalling.

Legit_Spaghetti,
@Legit_Spaghetti@mastodo.neoliber.al avatar

@0xabad1dea Everyone talking about how silly it is to use a float when they shoulda used an int, and I'm over here thinking unsigned int probably would have made even more sense, unless company was expecting to have fewer than zero planes at some point.

0xabad1dea,

@Legit_Spaghetti unfortunately it’s extremely common to use the negative integer space to encode error states, which tends to cause its own problems, but seeing them do that would have fallen within the normal range of incorrect for me

freemin7,
@freemin7@mast.hpc.social avatar

@0xabad1dea @Legit_Spaghetti With floats we can do NaN encoding fun ♥️
like NaN because it's not a plane.

glyph,
@glyph@mastodon.social avatar
cvtsi2sd,
@cvtsi2sd@hachyderm.io avatar

@Legit_Spaghetti @0xabad1dea in C++ generally you don't want to have unsigned integers around unless they are used essentially as bitfields or you really need that extra bit of range: they provide no range checking anyhow (you can do unsigned x = -1; and you get UINT_MAX, no questions asked), and, as they "win" in integral promotions, you can easily get in trouble (e.g. (number_of_planes - 10 < 0) is always false if number_of_planes is an unsigned).

0xabad1dea,

@cvtsi2sd @Legit_Spaghetti as a security auditor my stance is the exact opposite: don’t used signed ints, you can’t be trusted to handle signed ints correctly, if your program has signed ints anywhere at all it is almost certainly logically flawed somewhere

postmodern, (edited )

@0xabad1dea @cvtsi2sd @Legit_Spaghetti what if your program needs to represent negative values? There's another camp that insists on only ever using signed integers, in case you have to deal with negative numbers; I call this camp the Java camp. Or you could just use a Strong Typed language like Crystal (or Rust, of course) which has special methods for doing arithmetic between signed and unsigned integers, with guards for underflow/overflow.
https://crystal-lang.org/api/1.11.2/Int64.html

Archivist,

@0xabad1dea @cvtsi2sd @Legit_Spaghetti

Ada range integers. You want Ada range integers

Archivist,

@0xabad1dea @cvtsi2sd @Legit_Spaghetti

(I said "you", but I want them everywhere I go)

petealexharris,
@petealexharris@mastodon.scot avatar

@0xabad1dea

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

wilbr,
@wilbr@glitch.social avatar

@petealexharris @0xabad1dea joke of the month right here

Sylvhem,
@Sylvhem@eldritch.cafe avatar

@0xabad1dea Experience recently thaugh us that fractional Boeing planes are a thing.

steverocky,

@0xabad1dea

You would be amazed at how many coders declare house address numbers as 'number' instead of the correct as 'Text'...!

StuartGray,

@0xabad1dea my best code review WTF story, is the time I reviewed the code from an large multi-national outsourcing partners new on-site developer.

I forget the precise details of the change they were tasked with, except it was mostly adding a new feature with minor changes to integrate.

They wrote a new class with 20+ functions all named function1, function2, etc...

And zero comments describing their purpose.

They didn’t last long.

jsbarretto,
@jsbarretto@social.coop avatar

@0xabad1dea "Hello, sir, I'd like NaN planes please"

Statements dreamed up by the insane

bazscott,
@bazscott@mastodon.social avatar

@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 😂

TomF,
@TomF@mastodon.gamedev.place avatar

@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.

deFractal, (edited )

@0xabad1dea Tbh, I suppose float makes sense for a company which sometimes has a non-integer number of aircraft airborne.

jfmezei,
@jfmezei@mstdn.ca avatar

@0xabad1dea I’ll be devil’s advocate for a second. If the program deals with aircraft production , then having fractions can make sense if goal is to calculate production rate which factors that at any point in time you have partly assembled planes on floor or deliver 5 planes every 2 months or 2.5 planes per month. If you declare it as int but involve it in math that will implicitly convert it to float then it still lives with inaccuracy of float during that line of code

notjimtuck,

@0xabad1dea If it was in shipping or logistics software I could see the float being left as an artifact of load sharing or reduced capacity.

IE, they sold half a load's worth to someone else, so they only have 'half a plane' left.

bees,

@0xabad1dea lemme guess this was somewhere within EFB code.

KeithDevlin,
@KeithDevlin@fediscience.org avatar

@0xabad1dea OTOH, given that bits of Boeing planes fall off, maybe that declaration was appropriate. Just saying 🤔

marshray,

@0xabad1dea I must be a programmer because my mind immediately starts brainstorming possible scenarios in which one might need a non-c++-integral type for number of planes.

gsuberland,
@gsuberland@chaos.social avatar

@0xabad1dea lmao, I uh, yeah, cannot say that I am surprised '>_>

gsuberland,
@gsuberland@chaos.social avatar

@0xabad1dea and props for putting this out there :D

AE35,
@AE35@hal9.ooo avatar

@0xabad1dea I've seen float with auto increment being used as the primary key in a database.

C222,

@0xabad1dea When I was a new developer one of my responsibilities was interfacing with industrial equipment via SCADA protocols. One fledgling manufacturer decided that everything needed to be 32-bit floats.

watthours_imported? float, that's fine.
batteries_installed? float
is_activated? float! (0.0 was off)
serial_number? was a list of 10-some floats (e.g. 65.0 was ascii "A")

They're out of business now.

0xabad1dea,

@C222 aaaaaaAAAAAAAAAAAAA

C222,

@0xabad1dea OOH. Also. The byte order for the floats wasn't little or big endian. It was something like C,D,A,B because the 16-bit words were big-endian, but the bytes were little.

I asked their "developer" why I was getting garbage and they replied with the wiki page for IEEE754.

pointingdevice,

@C222 @0xabad1dea That's exciting, because that would be yet another variation on the classic PDP-11 middle-endian format (B,A,D,C – two little endian 16-bit words, but with the high word first).

C222,

@pointingdevice @0xabad1dea It could have been that for all I remember. But that also deserves to stay in the 1970s.

scottmichaud,
@scottmichaud@mastodon.gamedev.place avatar

@0xabad1dea So... the only time I could see that being useful is if you needed to interop with something else (likely a device) that heavily expects decimals, and you are VERY careful to only do operations on it that are guaranteed to yield integers.

The only two things I can think of are Web and GPUs, and Web would be doubles so that still makes no sense. And of course I can't even begin to see why a GPU would need to know number of planes, so yeah...

0xabad1dea,

@scottmichaud to my recollection it was very ordinary business logic that wasn’t interacting with any special snowflake hardware

Oroshimarru,
@Oroshimarru@mastodon.social avatar

@0xabad1dea the planes floats beacause of
Archimedes' principle, no?

magnetic_tape,

@0xabad1dea
If you have 1.0 of a plane with a part of it going away (say a door plug for example), then you have 0.97 plane left

soulcutter,
@soulcutter@ruby.social avatar

@0xabad1dea

The general public: Surely critical corporate software is written to the highest standards and will not ever fail.

Veteran Developers: I’ve seen things that make me wonder how anything works at all - especially in critical systems.

yes, there are exceptions, this is hyperbole

postmodern,

@0xabad1dea there are some reasons why you'd want to keep a value a float, except only one seems to make sense in this context.

  1. Premature Optimizations. It takes extra cycles to convert floats or doubles back to integers and round them up or down. However this begs the question why didn't they simple do integer division and keep the number as an integer?
  2. Preserving accuracy. If you have an equation that already involves floats, and outputs a float, and the float value is used later on in other calculations, it might make sense to keep it a float to preserve any fractional bits that might have gotten into the number. One real-world example of this is keeping pixel color values or PCM values as floats or doubles after manipulating them, instead of converting them back to integers in order to preserve accuracy and avoid premature rounding. Although, if they wanted to preserve accuracy, why not use a double?
  3. Academic programmer.
mansr,
@mansr@society.oftrolls.com avatar

@0xabad1dea Ever heard of a semi?

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • kavyap
  • tacticalgear
  • Durango
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • thenastyranch
  • Youngstown
  • khanakhh
  • slotface
  • vwfavf
  • everett
  • rosin
  • osvaldo12
  • provamag3
  • modclub
  • GTA5RPClips
  • ethstaker
  • InstantRegret
  • cisconetworking
  • cubers
  • ngwrru68w68
  • tester
  • normalnudes
  • Leos
  • anitta
  • megavids
  • JUstTest
  • All magazines