- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Feed it "1.0e+38" and see what the multiply-by-100 will do ... (On most modern machines, nothing good.)
Admin
I feel like this WTF wasn't emphasized enough.
Admin
I sure hope those input strings don't include leading currency symbols. ato* wouldn't get very far with those.
Admin
The trick I love is computing the return value in a variable with the name lower-case L, but then returning the digit 1 which looks identical to the casual observer
Admin
If one had done the float-to-integer conversion between the multiply and the divide (with the divide converting back to float) then we would remove fractional cents. (Assuming the value is sufficiently small to avoid losing precision in the floating point format.)
Perhaps this code was copied from an earlier function which had this feature and the multiply/divide/convert ordering was an attempt to remove it.
Admin
Maybe I'm showing my age with this statement, but if I encountered this at work, I'd be looking around for Alan Funt or a hidden camera. And, Tim, my first thought when glancing at the return statement was, "why is this returning one?"
Admin
To be fair, I rather doubt that anyone anywhere has encoded money in scientific format, let alone more money than the entire world has even if you include all the fake money in cryptocurrency and NFTs.
But -- what about those countries which use ',' as the decimal point?
Admin
CodeSOD? more like Coded Smorgasbord! After reading through all the WTFs in that code, I'm full!
Admin
I agree that this is a botched attempt to get rid of fractional pennies.
That being said, while overwriting the input certainly doesn't violate the principle of least surprises it's not utterly insane. It's a clean-this-string-up-for-me function--just written by someone who doesn't know what they are doing.
Admin
You've never read all the rants about using floating point variables for money? (I could cite examples here on this site, of which the most recent is "Padding the Amount"...) The actual notation inside a typical(1)
float
ordouble
is an exponent (usually but not(2) universally indicating a power of two) and a "mantissa", which sounds a lot like scientific notation to me.(1) There's probably some goofy format somewhere that does it differently.
(2) The primary guilty party here is IBM's System/360 and its descendants, including today's zSystems, whose native floating point formats have an exponent that indicates a power of sixteen.
Admin
Germany does. And it’s not a decimal point, it’s a decimal separator. You should also look for thousands separators and currency, like €1.234,56.
Admin
Extra points there for using 'l' as a name in code that is dealing with numbers. I read a couple of those ''l's (letters) as 1 (digit).
Admin
I got caught by the "l" vs "1" thing too. At first I thought "l" was holding the value in a long, and they were returning that. In which case, all the string manipulation was for nothing. But no, the whole point is to manipulate the char array provided as a parameter directly, and then return 1 in all cases.
Oh, and the multiply/divide by 100 doesn't remove the fractional cents. All of the cents are removed when cast to int (which may be the same as a long, if using M$). I think they were worried about rounding errors - e.g. 100.00 being represented as 99.99999999. But no rounding is every applied, so that was completely useless, and may have just introduced more rounding errors if anything.
Admin
The return 1 is a c style error code return. Yes, error code. Because the convention is to return 0 if success and > 0 to tell you what error happened otherwise. So this code is at least self aware enough to always tell the caller it screwed up.
Admin
Of course, modern Z systems have floating point support for exponent bases of 2, 10 and 16. I would argue that from S/360 through modern Z systems, the native decimal formats gave proper support for arithmetic on currency values.
Admin
That's ok, the caller probably never checks the return value anyway.
Admin
Overall this code was an hilarious treat to read. It's the code equivalent of stand-up comedy -- complete improv surprise.
Admin
Well that's their problem than. :) This code correctly informed the end user of the result. As Pilat put it "I wash my hands.". lol
Admin
TRWTF is writing business logic in C.