100
top 50 comments
sorted by: hot top new old
[-] markz@suppo.fi 31 points 1 year ago

Stop Using Floats

no shit

or Cents

huh..?

That was a good point.

[-] underscores@lemmy.zip 7 points 1 year ago* (last edited 1 year ago)

I think maybe they meant using integers for cents

[-] Treczoks@lemmy.world 27 points 1 year ago

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.

[-] MonkderVierte@lemmy.zip 12 points 1 year ago

And KSP (rocket exploding game) had ten years worth of floating point errors.

[-] Treczoks@lemmy.world 8 points 1 year ago

Like Minecraft has, too. Just go on a long, long walk in one direction.

[-] LowtierComputer@lemmy.world 3 points 1 year ago
[-] ZILtoid1991@lemmy.world 5 points 1 year ago

The physics starts to glitch out, or at least used to, until it got upgraded to doubles. I also use doubles for my game engine, as well as (optionally) limiting pixel-precise things within int.max and int.min.

[-] LowtierComputer@lemmy.world 2 points 1 year ago

Does the world repeat after a set point?

[-] ZILtoid1991@lemmy.world 3 points 1 year ago

Technically yes, and with tile layers, you can even set them repeating on a shorter area.

[-] Treczoks@lemmy.world 3 points 1 year ago

All kinds of weird things. There is a video explaining the details, and you've got to be far, far out.

[-] LowtierComputer@lemmy.world 2 points 1 year ago

I'll have to look it up after work. Sounds interesting.

[-] ICastFist@programming.dev 2 points 1 year ago

Used to*, it was fixed in some version or another, where the procgen no longer evaluated how far you were from the origin

[-] Treczoks@lemmy.world 2 points 1 year ago

OK, I have not played it for AGES. Nice to see something like that fixed, as it was a bit system-inherent.

[-] kuberoot@discuss.tchncs.de 2 points 1 year ago

The game, including worldgen, will still bug out at longer distances - the issues were reduced and a world limit was added to prevent you going too far, and IIRC past a certain point the world turns into non-stop ocean, but I'm pretty sure if you bypass those limits you'll encounter chunks that outright fail to generate.

[-] Womble@piefed.world 9 points 1 year ago* (last edited 1 year ago)

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.

[-] Treczoks@lemmy.world 20 points 1 year ago

Nope. With about a hundred thousand factored items, things easily run off the rails. I've seen it. Just count cents, and see that rounding errors are kept in close, deterministic confines.

[-] jasory@programming.dev 4 points 1 year ago

You can use Kahan summation to mitigate floating point errors. A mere 100 thousand floating point operations is a non-issue.

As a heads up computational physics and mathematics tackle problems trillions of times larger than any financial computation, that's were tons of algorithms have been developed to handle floating point errors. Infact essentially any large scale computation specifically accounts for it.

[-] Treczoks@lemmy.world 6 points 1 year ago

Yep. And in accounting this is done with integers. In my field (not accounting), calculations are done either in integer or in fixed-point arithmetic - which is basically the same in the end. Other fields work with floats. This variety exists because every field has its own needs and preferences. Forcing "One size fits all" solutions was never a good idea, especially when certain areas have well-defined requirements and standards.

[-] soc@programming.dev 3 points 1 year ago

Yeah, but compared to counting money, nobody cares if some physics paper got its numbers wrong. :-)

(Not to mention that would require the paper to have reproducible artifacts first.)

[-] azolus@slrpnk.net 2 points 1 year ago* (last edited 1 year ago)

We're using general relativity to calculate sattelite orbits - fuck your point of sale system if our sattelites come crashing down we're gonna have much bigger problems lol.

[-] jasory@programming.dev 1 points 1 year ago

Physics modeling is arguably the most important task of computers. That was the original impetus for building them; artillery calculations in WW2.

All engineering modeling uses physics modeling, almost always linear algebra (which involves large summations). Nuclear medicine—physics, weather forecasting—physics, molecular dynamics and computational chemistry—physics.

Physics modeling is the backbone of modern technology, it's why so much research has been done on doing it efficiently and accurately.

load more comments (12 replies)
[-] entwine@programming.dev 7 points 1 year ago

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.

[-] randy@lemmy.ca 16 points 1 year ago* (last edited 1 year ago)

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.

[-] nimpnin@sopuli.xyz 24 points 1 year ago

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.

[-] KRAW@linux.community 22 points 1 year ago

The IEEE standard actually does not dictate a rounding policy

[-] a1studmuffin@aussie.zone 13 points 1 year ago

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.

[-] randy@lemmy.ca 4 points 1 year ago

Thanks, that's an excellent article, and it's exactly what I was looking for.

[-] pinball_wizard@lemmy.zip 2 points 1 year ago

The real standard is whatever Katherine in accounting got out of the Excel nightmare sheets they reconcile against.

[-] bleistift2@sopuli.xyz 0 points 1 year ago* (last edited 1 year ago)

If you count the programming language you use as ‘platform’, then yes. Python rounds both 11.5 and 12.5 to 12.

[-] WolfLink@sh.itjust.works 4 points 1 year ago

This is a common rounding strategy because it doesn’t consistently overestimate like the grade school rounding strategy of always rounding up does.

[-] FizzyOrange@programming.dev 2 points 1 year ago

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.

[-] mceldritch@lemmy.world 7 points 1 year ago* (last edited 1 year ago)

I think using millicents is pretty standard in fin-tech.

[-] Kissaki@programming.dev 4 points 1 year ago

Scroll to the second paragraph, get a subscribe popover. So annoying. I haven't even read any reasonable amount of content yet.

[-] Vulwsztyn@programming.dev 1 points 10 months ago

"stop using medium"

[-] eah@programming.dev 3 points 1 year ago

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?

[-] jasory@programming.dev 3 points 1 year ago

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.

[-] Vulwsztyn@programming.dev 1 points 10 months ago* (last edited 10 months ago)

Stop using ~~floats and cents for money~~ medium ffs

[-] DarkAri@lemmy.blahaj.zone 0 points 1 year ago* (last edited 1 year ago)

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.

[-] TehPers@beehaw.org 3 points 1 year ago* (last edited 1 year ago)

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.

[-] DarkAri@lemmy.blahaj.zone 2 points 1 year ago

Oh nice I'm far from a professional programmer. Thxs for the explanation.

load more comments
view more: next ›
this post was submitted on 24 Sep 2025
100 points (97.2% liked)

Programming

28579 readers
134 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 3 years ago
MODERATORS