Big Decimals: Stop Using Floats or Cents for Money
Big Decimals: Stop Using Floats or Cents for Money
Just a moment...
Big Decimals: Stop Using Floats or Cents for Money
Just a moment...
I stopped using floats 30 years ago when I learned what rounding errors can do if you only deal with big enough numbers of items to tally. My employer turned around 25M a year, and it had to add up to the cent for the audits.
There's a good documentary about this.
Fun fact: This is actually called the Salami Shaving Scam. Basically, shave off tiny pieces of a bunch of large chunks, and eventually you’ll have a massive amount. Like taking a single slice of salami from every sausage that is sold.
Single floats sure, but doubles give plenty of accuracy unless you absolutely need zero error.
For example geting 1000 random 12 digit ints, multiplying them by 1e9 as floats, doing pairwise differences between them and summing the answers and dividing by 1e9 to get back to the ints gives a cumulative error of 1 in 10^16. assuming your original value was in dollars thats roughly 0.001cent in a billion dollar total error. That's going deliberately out of the way to make transactions as perverse as possible.
Stop Using Floats
no shit
or Cents
huh..?
That was a good point.
I think maybe they meant using integers for cents
Link a free copy or none at all please
The medium (lol) is annoying, but it didn't ask me to pay. Is the article not free for you?
I got hung up on this line:
This requires deterministic math with explicit rounding modes and precision, not the platform-dependent behavior you get with floats.
Aren't floats mostly standardized these days? The article even mentions that standard. Has anyone here seen platform-dependent float behaviour?
Not that this affects the article's main point, which is perfectly reasonable.
Mostly standardized? Maybe. What I know is that float summation is not associative, which means that things that are supposed to be equal (x + y + z = y + z + x) are not necessarily that for floats.
The IEEE standard actually does not dictate a rounding policy
Floating-Point Determinism | Random ASCII - tech blog of Bruce Dawson https://randomascii.wordpress.com/2013/07/16/floating-point-determinism/
The short answer to your questions is no, but if you're careful you can prevent indeterminism. I've personally ran into it encoding audio files using the Opus codec on AMD vs Intel processors (slightly different binary outputs for the exact same inputs). But if you're able to control your dev environment from platform choice all the way down to the assembly instructions being used, you can prevent it.
Thanks, that's an excellent article, and it's exactly what I was looking for.
The real standard is whatever Katherine in accounting got out of the Excel nightmare sheets they reconcile against.
If you count the programming language you use as ‘platform’, then yes. Python rounds both 11.5 and 12.5 to 12.
This is a common rounding strategy because it doesn’t consistently overestimate like the grade school rounding strategy of always rounding up does.
That is default IEEE behaviour: https://en.wikipedia.org/wiki/Rounding#Rounding_half_to_even
This is the default rounding mode used in IEEE 754 operations for results in binary floating-point formats.
Though it's definitely a bad default because it's so surprising. Javascript and Rust do not do this.
Not really anything to do with determinism though.
I become suspicious when I see a Medium user posting well-written deep articles as frequently as this user appears to be doing. How can we tell whether this is AI slop or not?
Their articles aren't that deep and they mostly focus on similar topics.
I think it's perfectly possible for someone to have a backlog of work/experience that they are just now writing about.
If it were AI spam, I would expect many disparate topics at a depth slightly more than a typical blog post but clearly not expert. The user page shows the latter, but not the former.
However, the Rubik's cube article does seem abnormal. The phrasing and superficiality makes it seem computer-generated, a real Rubik's afficionado would have spent some time on how they cube.
Of course I say this as someone much more into mathematics than "normal" software engineering. So maybe their writing on those topics is abnormal.
I think using millicents is pretty standard in fin-tech.
You just know they will either take an oath to defend the Tangerine Torquemada or lose their command.
You could just limit the precision of the float. If you are writing banking software or something you could just add a special case to flip flop the remainder or something. I think pretty much all modern languages support this. You should also be using doubles for any numbers that could potentially grow really large like fiat currencies. The issues with floats really is that it will often favor precision over range. You could end up with lots of numbers after a decimal and very little integer range which could cause overruns or something.
The article goes into depth about what you should be using. Floats and doubles are not designed for use with base 10 fractions. They're good at estimating them, but not accurate enough for real financial use.
There's also not much reason to reinvent the wheel for an already solved problem. Many languages have this data type already built into the language, and the rest usually have it available through a package.
Oh nice I'm far from a professional programmer. Thxs for the explanation.
Scroll to the second paragraph, get a subscribe popover. So annoying. I haven't even read any reasonable amount of content yet.