• (nodebb)

    Feed it "1.0e+38" and see what the multiply-by-100 will do ... (On most modern machines, nothing good.)

  • (nodebb)
    Now that they've got their truncated value, they use sprintf to overwrite the input value data and replace it with the truncated value.

    I feel like this WTF wasn't emphasized enough.

  • Brian (unregistered)

    I sure hope those input strings don't include leading currency symbols. ato* wouldn't get very far with those.

  • Tim (unregistered)

    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

  • (nodebb)

    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.

  • Argle (unregistered)

    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?"

  • cellocgw and I am logged... or not (unregistered) in reply to Steve_The_Cynic

    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?

  • Airdrik (unregistered)

    CodeSOD? more like Coded Smorgasbord! After reading through all the WTFs in that code, I'm full!

  • (nodebb)

    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.

  • (nodebb) in reply to cellocgw and I am logged... or not

    I rather doubt that anyone anywhere has encoded money in scientific format,

    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 or double 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.

  • Gnasher729 (unregistered) in reply to cellocgw and I am logged... or not

    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.

  • (nodebb)

    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).

  • (nodebb)

    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.

  • LZ79LRU (unregistered)

    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.

  • (nodebb) in reply to Steve_The_Cynic

    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.

  • (nodebb) in reply to LZ79LRU

    The return 1 is a c style error code return.

    That's ok, the caller probably never checks the return value anyway.

  • (nodebb)

    Overall this code was an hilarious treat to read. It's the code equivalent of stand-up comedy -- complete improv surprise.

  • LZ79LRU (unregistered) in reply to Ralf

    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

  • Duke of New York (unregistered) in reply to Ralf
    Comment held for moderation.
  • Maia Everett (github)

    TRWTF is writing business logic in C.

Leave a comment on “Padding the Amount”

Log In or post as a guest

Replying to comment #:

« Return to Article