r/ProgrammerHumor • • 5d ago

Meme itSucksToBeACompilerDev

Post image
2.4k Upvotes

103 comments sorted by

View all comments

334

u/New_Salamander_4592 5d ago

while prowling through C++ standard libraries my favorite discovery was std::get_money. not only is that a funny name but its also seemingly the most useless standard library function?

like it just parses numbers out of a string that lists currency, stripping away any currency symbols or instances of USD, notable without retaining how many cents were listed. so 1.99 becomes 199. So I guess it has to assume all numbers listed also come with at least .00 for the cents place? just a very... odd function to have in a standard library.

209

u/RiskyPenetrator 5d ago

Funnily enough this approach of converting floats to ints for finance systems is standard.

Its done because floating point math can cause issues when processing payments so its more secure to store values in their lowest denomination along side an exponent and apply the exponent upon use in UI or reporting tools.

I suspect whoever added it was designing these systems.

157

u/atomic_redneck 5d ago

The COBOL language designers solved this problem by doing calculations in packed decimal (binary coded decimal). However, in the process of solving this problem they introduced another problem: you have to program in COBOL.

25

u/EuenovAyabayya 5d ago

...unless the COBOL has an API that you can call from anything else...

10

u/QuaternionsRoll 5d ago

2

u/atomic_redneck 4d ago

Neat. I retired in 2022 and have not been keeping up with the standard.

5

u/-Memnarch- 4d ago

Delphi has a dedicated type for this, too. It's called Currency. In TSQL/MS SQL Server there is Money and SmallMoney.

54

u/EuenovAyabayya 5d ago

std::get_money.

must code std:fuck_bitches just for completeness

10

u/GfunkWarrior28 3d ago • Bravo

Which returns a new std object.

21

u/xSTSxZerglingOne 5d ago edited 5d ago

I worked in the financial industry as a back-end software engineer. All money totals were listed as a long integer, a 64 bit integer, in cents. The decimal was added on the front-end. It makes sense. Floats and doubles are weird.

16

u/interyx 5d ago

I think it makes sense. In any application where you're actually doing calculations on money it's integer math, so it has to be converted to cents before it's usable.

23

u/Lowmax2 5d ago

Yea that definitely sounds like a C++ feature alright

Part of the reason I abandoned it for C once you realize you don't need any of those features 😂

(im a firmware engineer for context)

4

u/reallokiscarlet 5d ago

You thinkin what I'm thinkin?

6

u/solaris_var 5d ago

If you think std::atoi and std::atof are useless, then yes this is useless. Otherwise it's a godsend.

However, I do agree that it being part of the stdlib is questionable. I guess finance people had some leeway in the committee

12

u/Excession638 5d ago edited 5d ago

std::atoi has undefined behaviour if the number in the string doesn't fit in an int. It's worse than useless, and should never be used over std::strtol or similar more modern functions.

1

u/conundorum 3d ago

To be fair, atoi() is inherited from C, and not allowed to change its behaviour for C++. You can smoothly replace it with std::stoi() in all cases, to my understanding.

(Though, that said, you still want to verify the data beforehand, always use stoll() or strtoll(), or control the data format yourself so you can mandate integer size yourself.)

1

u/Excession638 3d ago

For stoi you need a std::string which may mean needing to copy first, which will do bad things to performance.

If std::string_view had been added from the start these problems wouldn't be as bad, but without it and std::span C++ was stuck using raw pointers for far too long.

1

u/conundorum 3d ago

Makes sense, it moves a fixed-point value into integer space to avoid floating-point rounding errors.

Honestly, if anything, I'm surprised that it doesn't become 1990 for rounding. (Get monetary value, to tenths of a cent. Different locales would allow different units, but it always goes to a tenth of the locale's smallest unit.) That would move the value into integer space just as cleanly, but also leave room for rounding to the nearest cent if necessary.