• html nazi (unregistered)

    1000000000000000000st

    CAPTCHA: abigo - comment written abigoing the rules in the code.

  • QJo (unregistered)

    (coughbignumcough)

  • (cs)

    funnily they use a StringBuilder but still use the + for building the string

    it would have been easier if they had just done

    DecimalFormat df2 = new DecimalFormat("000000");

    sb.append(df2.format(ab100000)); sb.append(df2.format(cd100000)); //etc.

  • Felipe (unregistered)

    Is he is worried about precision he should be using BigDecimals anyway.

  • Liam (unregistered)

    Not even BigDecimals, this looks like C#, which has had a native base-10 decimal type since day one.

  • gnasher729 (unregistered)

    This is utterly ridiculous. I don't know what that programmer thought he was doing, but he hasn't been writing code to print decimal numbers - he has written highly complicated code that prints floating-point numbers.

  • nobulate (unregistered)

    Obviously this code was ported from a Pascal program.

    But seriously, don't they know the best way to store a decimal is as a string?

    foo.ToString();

    ZING

  • (cs) in reply to Liam
    Liam:
    this looks like C#
    Well it isn't.
  • faoileag (unregistered)
    the developer comments::
    B - bytes 1-5 decimal portion of field x (range: 00000-99999 to represent 0.00000-9.99999)
    This is a nice one - use five digits to represent a six-digit number ;-)
  • MightyM (unregistered) in reply to dkf
    dkf:
    Liam:
    this looks like C#
    Well it isn't.
    Easily noticed by the lower case "toString".
  • Charles F. (unregistered) in reply to gnasher729

    When I read gnasher729's comment, there was a line break here:

    gnasher729:
    ...he has written highly

    I thought the comment was suggesting that the code was written in the manner of someone who was high (on drugs). Although the sentence continued, I still think that speculation has merit.

    CAPTCHA: minim The minimal portion of "minimal" that you need to convey the concept of "minimal."

  • faoileag (unregistered)

    I'm still trying to see any sense in what is finally produced as a string for the file...

    Take ab = 1.23, cd = 1.23, e = 1.23

    df3.format(Math.floor(d.getAB())) = "1" df2.format((d.getAB() - Math.floor(d.getAB()))*100000) = "-99998" (or is the sign dropped?).

    So I get "1-99998" or "199998" for processing ab.

    So I would end up with either 1-999981-999981.23 or 1999981999981.23 (when the sign is droppend).

    Am I missing something?

  • Xyon (unregistered)

    TRWTF is how the preview of this WTF was just as long as the article itself

  • Mike5 (unregistered)

    It's funny how doubles are good enough to store number in memory, but not in the file. Or maybe all languages have two internal type fdouble and mdouble? Who was hiding this from us? And why?!!!

    ingenium indeed

  • Oppo (unregistered) in reply to faoileag

    Watch your parentheses!

    d.getAB() - Math.floor(d.getAB()) = 1.23 - 1 = .23 .23 * 100000 = 23000

    So it'll print 1230001230001.23000

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered)

    TRRWTF here is using Double at all. Remove the decimal point and you can easily store those numbers in a longint. Then you just need integer division and modulus to print the number.

  • anonymous (unregistered)

    As someone who works on a system that deals with financial data coded with doubles in a most moron-ish way, I loathe every bit of stupid code that does all sort of math-magic to avoid precision errors, while a standard library class is available for such tasks since day one.

    But I must admit, this one is a bit over the top. This guy must be a highly paid consultant... or a self proclaimed guru.

  • faoileag (unregistered) in reply to Oppo
    Oppo:
    Watch your parentheses!

    d.getAB() - Math.floor(d.getAB()) = 1.23 - 1 = .23 .23 * 100000 = 23000

    So it'll print 1230001230001.23000

    Oh. Thank you. (>_<)

    Though I'm still wondering why anybody woud choose "1230001230001.23000" as a storage format for three floats.

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered)

    I'm going to add this: if your decimal point is not FLOATING, then don't fucking use FLOATING point numbers. Use fixed point and scale your numbers input/printing or when doing multiplication or division by other fixed-point numbers.

    Example: represent money as an integer number of pennies, not as a floating point number of dollars. ".10" is a repeating fraction in binary, just like 1/3 is in decimal, which is why you don't use floats.

  • (cs) in reply to faoileag
    faoileag:
    the developer comments::
    B - bytes 1-5 decimal portion of field x (range: 00000-99999 to represent 0.00000-9.99999)
    This is a nice one - use five digits to represent a six-digit number ;-)
    It would have been better, I suspect, to say X.00000-X.99999 because the B part is for the decimal-portion (aka the fractional part). And five digits works well for the given requirement.
  • Raymond (unregistered) in reply to ¯\(°_o)/¯ I DUNNO LOL

    ... The assumption that all money amounts are in the form of pennies is easily dis-proven. The "cost" per gallon of gas in the US is typically to the tenth of a penny. Using integers to hold fractions of dollars is problematic at best, and difficult to properly extend in the future to higher precision levels.

  • faoileag (unregistered) in reply to Steve The Cynic
    Steve The Cynic:
    faoileag:
    the developer comments::
    B - bytes 1-5 decimal portion of field x (range: 00000-99999 to represent 0.00000-9.99999)
    This is a nice one - use five digits to represent a six-digit number ;-)
    It would have been better, I suspect, to say X.00000-X.99999
    Definitely. But that would have deprived the quibbler in me of a wonderful possibility for a post in which he could point out some completely irrelevant detail in the artice ;-)
  • faoileag (unregistered) in reply to Raymond
    Raymond:
    ... The assumption that all money amounts are in the form of pennies is easily dis-proven. The "cost" per gallon of gas in the US is typically to the tenth of a penny.
    Although usually presented in the form "use cents instead of dollars", what is meant is "use the smallest fraction used as a base", so when dealing with gas station prices, you would use something as
    var pricePerGallonInTenthOfPennies = 5969;
    (UK prices, Imperial gallon)
  • anonymous (unregistered)

    It's a rather ugly hacky way of creating fixed-precision decimal numbers. The format ("ABBBBB...") is not a WTF; the code that converts from "ABBBBB" to "A.BBBBB" is a bit of a WTF, but on the whole, not as bad as it could have been.

  • faoileag (unregistered) in reply to anonymous
    anonymous:
    It's a rather ugly hacky way of creating fixed-precision decimal numbers. The format ("ABBBBB...") is not a WTF; the code that converts from "ABBBBB" to "A.BBBBB" is a bit of a WTF, but on the whole, not as bad as it could have been.
    Well, of course we don't know what system reads the file that is generated by the code snippet, but parsing "1230001230001.23" is weird.

    The system seems to be able to parse floats as the last number in every line is a stringified float.

    And yet the first two ones a integers.

    And there is no separator between the three numbers, so you absolutely have to know the format: 6 digits for each of the first two numbers, then comes a float.

    Weird.

  • anonymous (unregistered) in reply to faoileag
    faoileag:
    (UK prices, Imperial gallon)
    Imperial units are the über-WTF.

    Captcha: sagaciter... yep, sagacious to avoid them.

  • Mijzelf (unregistered) in reply to faoileag
    faoileag:
    the developer comments::
    B - bytes 1-5 decimal portion of field x (range: 00000-99999 to represent 0.00000-9.99999)
    This is a nice one - use five digits to represent a six-digit number ;-)
    The code is genial, yet unreadable. I can't find a decompression algorithm.
  • (cs) in reply to faoileag
    faoileag:
    Raymond:
    ... The assumption that all money amounts are in the form of pennies is easily dis-proven. The "cost" per gallon of gas in the US is typically to the tenth of a penny.
    Although usually presented in the form "use cents instead of dollars", what is meant is "use the smallest fraction used as a base", so when dealing with gas station prices, you would use something as
    var pricePerGallonInTenthOfPennies = 5969;
    (UK prices, Imperial gallon)
    Now I feel old. I can remember a time when 5969 would have been feasible for "pricePerGallonInHundredthsOfPennies". That is, when the actual price would have been 59.69 pence per gallon... And it would have been expensive petrol at that price when I was e.g. eight years old. (That was a time when a packet of crisps could be had for three or four (new) pence, mind.)
  • faoileag (unregistered) in reply to anonymous
    anonymous:
    faoileag:
    (UK prices, Imperial gallon)
    Imperial units are the über-WTF.
    Well, you get more out of an imperial gallon than out of a us gallon, so I don't know why you are complaining ;-)
  • anon (unregistered) in reply to faoileag
    faoileag:
    anonymous:
    faoileag:
    (UK prices, Imperial gallon)
    Imperial units are the über-WTF.
    Well, you get more out of an imperial gallon than out of a us gallon, so I don't know why you are complaining ;-)

    Let's just agree that TRWTF is gallons then and start using litres then.

  • faoileag (unregistered) in reply to Steve The Cynic
    Steve The Cynic:
    faoileag:
    Raymond:
    ... The assumption that all money amounts are in the form of pennies is easily dis-proven. The "cost" per gallon of gas in the US is typically to the tenth of a penny.
    Although usually presented in the form "use cents instead of dollars", what is meant is "use the smallest fraction used as a base", so when dealing with gas station prices, you would use something as
    var pricePerGallonInTenthOfPennies = 5969;
    (UK prices, Imperial gallon)
    Now I feel old. I can remember a time when 5969 would have been feasible for "pricePerGallonInHundredthsOfPennies". That is, when the actual price would have been 59.69 pence per gallon... And it would have been expensive petrol at that price when I was e.g. eight years old.
    Well, when I started driving in the mid 80s the liter was just below 50 cent (or 227 cent/gallon), that's the lowest I can remember. But service stations in the UK still quoted prices in gallon back then if I recall correctly, and you had stuff like **** petrol.

    What I actually miss is the old measures for spirits, given in fractions of a gill :-)

  • faoileag (unregistered) in reply to anon
    anon:
    faoileag:
    anonymous:
    faoileag:
    (UK prices, Imperial gallon)
    Imperial units are the über-WTF.
    Well, you get more out of an imperial gallon than out of a us gallon, so I don't know why you are complaining ;-)

    Let's just agree that TRWTF is gallons then and start using litres then.

    Me personally, I prefer pints - litres get flat too fast for my taste ;-)

  • (cs) in reply to faoileag

    drink faster

  • Jeff Dege (unregistered)

    Actually, I've done this sort of thing, before. For good reason.

    Consider, for example, a legacy system that was written in a language or on a platform in which their either wasn't a system-standard mechanism for printing decimal numbers, or the system-standard mechanism was inadequate. It'd have been only reasonable for the implementers to have provided their own.

    Now consider someone writing a new system either to replace or to interact with that legacy system. The new system may have a perfectly adequate system-standard mechanism for printing decimal numbers - but if it was important that the new system print decimals in exactly the same way that the legacy system did, it's be not at all unusual to duplicate the decimal printing mechanism of the legacy system.

  • Adrian (unregistered) in reply to Charles F.

    I'll bite. You are female and I have fallen in love.

  • Paul Neumann (unregistered) in reply to Adrian
    Adrian:
    I'll bite. You are female and I have fallen in love.
    While there is at least a small number of participants in any fetish, biting ranks only slightly higher than oculolinctus. I would suggest a different first approach to love.
  • Christian (unregistered) in reply to ratchet freak
    ratchet freak:
    funnily they use a StringBuilder but still use the + for building the string

    it would have been easier if they had just done

    DecimalFormat df2 = new DecimalFormat("000000");

    sb.append(df2.format(ab100000)); sb.append(df2.format(cd100000)); //etc.

    Actually it does not matter as java will optimize those string operations and replace them with a stringbuilder anyway. With + you get a nicer looking code and java does the stringBuilder optimization for you anyway.

  • (cs) in reply to faoileag
    faoileag:
    **** petrol.

    Yeah! Fuck petrol!

    ...Stupid petrol.

  • (cs)

    One time when I went to the UK, I noticed the "cheap" prices for gasoline (petrol). Then I took a reality check. The prices weren't in $$/gal, they were ££/liter. Then I gagged and muttered to myself, glad to be live in the USA.

    As for using floating point for money: Don't. I learned the "hard way" when I submitted invoices and the totals didn't add up at all. Went back to the drawing board, and converted to double precision, and thanked my stars that the amounts didn't get mucked up since I was only using about 1/2 of the precision (which is about all you can use in a floating point number for money!).

    As for conversion from internal machine representation to external printouts: 'printf' and its friends are most helpful. Use them!

  • (cs) in reply to herby
    herby:
    As for using floating point for money: Don't. I learned the "hard way" when I submitted invoices and the totals didn't add up at all. Went back to the drawing board, and converted to double precision, and thanked my stars that the amounts didn't get mucked up since I was only using about 1/2 of the precision (which is about all you can use in a floating point number for money!).

    JavaScript uses 32-bit int and IEEE-754 64-bit binary floating point. I was developing a front-end for a systemlater cancelled, and I wanted to be able to do decimal arithmetic. To get enough precision, I used floating-point with a scaling factor of powers of 10.

    Floating-point can represent some values exactly. If you stick with those values, then you do not have the problem. (Instead, you have other minor problems.)

    Sincerely,

    Gene Wirchenko

  • (cs)

    They need to make sure the function headers are copyrighted in case oracle actually succeeds in their lawsuit against google over android.

  • anonymous (unregistered) in reply to Paul Neumann
    Paul Neumann:
    Adrian:
    I'll bite. You are female and I have fallen in love.
    While there is at least a small number of participants in any fetish, biting ranks only slightly higher than oculolinctus. I would suggest a different first approach to love.
    Tell that to the chick I screwed at a party - I was literally afraid to let her mouth anywhere near my junk. It was hard enough just trying to extract pieces of my face out from between her jaws.
  • (cs)

    Token "Java is TRWTF"

  • anonymous (unregistered) in reply to faoileag
    faoileag:
    anonymous:
    faoileag:
    (UK prices, Imperial gallon)
    Imperial units are the über-WTF.
    Well, you get more out of an imperial gallon than out of a us gallon, so I don't know why you are complaining ;-)
    Will I?

    I mean... if it's not as reasonable as a gallon X kilogallon X microgallon, then I won't bother to learn the difference. Have fun remembering how many pints make a gallon (or is it the other way around?), I'll just keep multiplying and dividing by powers of 10 ;-)

    Captcha: distineo... extinction?

  • (cs) in reply to anonymous
    anonymous:
    Will I?

    I mean... if it's not as reasonable as a gallon X kilogallon X microgallon, then I won't bother to learn the difference. Have fun remembering how many pints make a gallon (or is it the other way around?), I'll just keep multiplying and dividing by powers of 10 ;-)

    Captcha: distineo... extinction?

    "OK, Google. How many pints are there in a gallon?*"

      • Very useful in second grade, which is the last time that I had to do this conversion.
  • anonymous (unregistered) in reply to anonymous
    anonymous:
    faoileag:
    anonymous:
    faoileag:
    (UK prices, Imperial gallon)
    Imperial units are the über-WTF.
    Well, you get more out of an imperial gallon than out of a us gallon, so I don't know why you are complaining ;-)
    Will I?

    I mean... if it's not as reasonable as a gallon X kilogallon X microgallon, then I won't bother to learn the difference. Have fun remembering how many pints make a gallon (or is it the other way around?), I'll just keep multiplying and dividing by powers of 10 ;-)

    Captcha: distineo... extinction?

    Pff, powers of 10? Liquid measure is (mostly) powers of two, very easy when you need to double or cut something in half. Doubling upward, you have: tablespoon, eighth cup, quarter cup, half cup, cup, pint, quart, half gallon, gallon. The only outlier is the teaspoon, which is 1/3 of a tablespoon.

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered) in reply to Raymond
    Raymond:
    ... The assumption that all money amounts are in the form of pennies is easily dis-proven. The "cost" per gallon of gas in the US is typically to the tenth of a penny. Using integers to hold fractions of dollars is problematic at best, and difficult to properly extend in the future to higher precision levels.
    Those extra decimal places stay inside the gas pump, and only that dollars/pennies shown on the display come out of it.

    I actually know this to be a fact because I was writing code to talk to gas pumps/terminals back in the late '90s. (TRWTF is the goofy 5787 baud rate that Gilbarco pumps used. Good thing we used an SCC chip which had a variable baud rate generator. I think at one point just for fun I got my Powerbook 145's RS-422 talking to Gilbarco's RS-485, or maybe I was trying to make a pump simulator.)

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered) in reply to anonymous
    anonymous:
    eighth cup
    More commonly known as an "ounce". But not to be confused with 28.35 grams.
  • (cs) in reply to Jeff Dege
    Jeff Dege:
    Actually, I've done this sort of thing, before. For good reason.

    Consider, for example, a legacy system that was written in a language or on a platform in which their either wasn't a system-standard mechanism for printing decimal numbers, or the system-standard mechanism was inadequate. It'd have been only reasonable for the implementers to have provided their own.

    Now consider someone writing a new system either to replace or to interact with that legacy system. The new system may have a perfectly adequate system-standard mechanism for printing decimal numbers - but if it was important that the new system print decimals in exactly the same way that the legacy system did, it's be not at all unusual to duplicate the decimal printing mechanism of the legacy system.

    Good explanation!

  • (cs) in reply to ¯\(°_o)/¯ I DUNNO LOL
    ¯\(°_o)/¯ I DUNNO LOL:
    or maybe I was trying to make a pump simulator

    whoa, whoa. TMI.

Leave a comment on “Printing Decimal Numbers is HARD!”

Log In or post as a guest

Replying to comment #:

« Return to Article