Ancestors

Toot

Written by Alanna on 2025-01-08 at 17:54

JFC please don't use floating point types for calculating financial transactions or storing monetary values. I just got asked to review some code by someone and it is the first thing I spotted.

[#]roundingerrors #floatingpoint #softwareengineering

=> More informations about this toot | More toots from kelpana@mastodon.ie

Descendants

Written by Alanna on 2025-01-08 at 19:39

Turns out the person who asked me to review this tax calculation code genuinely had never heard of this problem before. He's accepted my reasons for highlighting it. Unfortunately it's going to take a while to refactor.

=> More informations about this toot | More toots from kelpana@mastodon.ie

Written by @haitchfive on 2025-01-08 at 17:55

@kelpana Isn't it always.

=> More informations about this toot | More toots from haitchfive@oldbytes.space

Written by Graham Sutherland / Polynomial on 2025-01-08 at 17:57

@kelpana "it's one banana, Michael, what could it cost, €NaN?"

=> More informations about this toot | More toots from gsuberland@chaos.social

Written by Kristopher Johnson on 2025-01-08 at 18:27

@kelpana Amount due: $1.2376e-11

=> More informations about this toot | More toots from oldmankris@mastodon.xyz

Written by mkj on 2025-01-08 at 18:28

@kelpana Oh heavens.

Both decimal and fixed-point were invented for a reason...

Yes, JFC indeed.

=> More informations about this toot | More toots from mkj@social.mkj.earth

Written by Chloé Raccoon on 2025-01-08 at 18:38

@mkj @kelpana @VulpineAmethyst I'd ask what were they thinking, but not sure there was any...

=> More informations about this toot | More toots from chloeraccoon@mastodonapp.uk

Written by Alan Langford on 2025-01-08 at 19:03

@kelpana Hey, four significant figures is enough, right?

Also, don't go trusting your rounding functions either. If both sides of a transaction round 0.5 cents up, someone just manufactured or destroyed a cent.

=> More informations about this toot | More toots from alan@mindly.social

Written by Infrapink (he/his/him) on 2025-01-08 at 19:41

@kelpana I wonder if this is why, when I filled out self-assessed tax returns, it only let me enter integer numbers of euros.

=> More informations about this toot | More toots from Infrapink@mastodon.ie

Written by Robee? Na! 🌈 on 2025-01-08 at 21:23

@kelpana What’s a floating point rounding error between financial institutions 😂

=> More informations about this toot | More toots from RobeeShepherd@mastodon.art

Written by LisPi on 2025-01-08 at 21:11

@kelpana @becomethewaifu Having never done banking, I wonder what monetary unit they use for their basic integer values.

It'd need to handle conversion between USD and hyperinflated currencies, after all.

=> More informations about this toot | More toots from lispi314@udongein.xyz

Written by jeremiah on 2025-01-08 at 22:58

@lispi314 @kelpana @becomethewaifu haven't worked at a bank but on a team that handles transactions and we rely on the USD penny as the unit.

=> More informations about this toot | More toots from jeremiah@tldr.nettime.org

Written by LisPi on 2025-01-09 at 00:00

@jeremiah @becomethewaifu @kelpana Wouldn't that start to have precision problems the minute something even as mundane as the Yen gets involved?

Though I suppose if the company only does business in USA then it's irrelevant.

=> More informations about this toot | More toots from lispi314@udongein.xyz

Written by Jeff Grigg on 2025-01-09 at 00:09

@lispi314 @kelpana @becomethewaifu @jeremiah

Java BigDecimal can handle it all, precisely, regardless of scale.

But do realize that currency conversion factors are barely good to two decimal places, on a good day.

Rounding is always a critically important business issue.

And so is accumulating currencies for batch conversion to avoid loss.

There are "banker's rounding" tricks (that predate computers) to ensure that the totals, when computed independently, add up exactly.

=> More informations about this toot | More toots from JeffGrigg@mastodon.social

Written by LisPi on 2025-01-09 at 01:04

@JeffGrigg @jeremiah @becomethewaifu @kelpana So ultimately, rounding errors past a certain point are considered acceptable.

And yeah, proper arbitrary precision reals/decimals/rationals libraries are an option, though I thought that maybe they didn't use them due to the performance peculiarities they tend to involve.

=> More informations about this toot | More toots from lispi314@udongein.xyz

Written by Jeff Grigg on 2025-01-09 at 01:09

@lispi314 @kelpana @becomethewaifu @jeremiah

Java BigDecimal is essentially BigInteger with logic to keep track of decimal places. Exactly. To any number of decimal places that will fit in memory.

Like more than needed to track the number of particles in the universe. I've had it out over 200 places to the right of the decimal, computed exactly.

But then you must apply business logic to round to something sane, like pennies. The rounding will also be exact. Like exactly half a cent.

=> More informations about this toot | More toots from JeffGrigg@mastodon.social

Written by LisPi on 2025-01-09 at 01:56

@JeffGrigg @jeremiah @becomethewaifu @kelpana > Like more than needed to track the number of particles in the universe. I've had it out over 200 places to the right of the decimal, computed exactly.

Ah good, so it is the Java equivalent of those libraries I was familiar with.

=> More informations about this toot | More toots from lispi314@udongein.xyz

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113794013552079982
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
337.241871 milliseconds
Gemini-to-HTML Time
3.040146 milliseconds

This content has been proxied by September (3851b).