Thread
#stdlib
    g

    guenther

    3 years ago
    Floating Point Rounding: With the JVM it's a mess to round an e.g.
    Double (12.456778)
    to a certain precision. This would be something really useful like
    12.34567.roundToPrecision(...)
    (don't nail me on the function name, I'd just want to express that rounding would be useful. What do you think?
    karelpeeters

    karelpeeters

    3 years ago
    You want to round a floating point to a given decimal precision? That's simply not possible, eg.
    0.3
    simply isn't representable as a floating point number.
    pavel

    pavel

    3 years ago
    well, you could “round” it to a string representation
    karelpeeters

    karelpeeters

    3 years ago
    There's a string formatting syntax for rounding though.
    Ah got ninja'd 😒imple_smile:
    g

    guenther

    3 years ago
    😞 I hate to convert it to String. It just feels that there should be a simpler way as
    Float -> String -> Float
    karelpeeters

    karelpeeters

    3 years ago
    I don't think you understood. What you want is completely impossible!
    Float -> String -> Float
    is wrong!
    Take a look at this site: https://0.30000000000000004.com/
    g

    guenther

    3 years ago
    True. Got your point. I hate floating point problems.
    karelpeeters

    karelpeeters

    3 years ago
    Well maybe look into using
    BigDecimal
    instead.
    pavel

    pavel

    3 years ago
    For most usecases,
    Double
    is good enough
    karelpeeters

    karelpeeters

    3 years ago
    Not if you're thinking about rounding.
    pavel

    pavel

    3 years ago
    yeah, but that’s typically done when printing things and then it’s all strings anyway
    Hullaballoonatic

    Hullaballoonatic

    3 years ago
    Relevant:

    https://youtu.be/PZRI1IfStY0

    elizarov

    elizarov

    3 years ago
    If you need to round, for example, to 2 decimal digits (so that
    .toString()
    prints at most two digits) then just write:
    round(x * 100) / 100
    .
    I wrote a related story a couple of years ago: https://medium.com/@elizarov/floating-point-for-decimals-fc2861898455
    (and that has nothing to do with JVM for that matter, all modern languages are based on IEEE754)
    Hullaballoonatic

    Hullaballoonatic

    3 years ago
    I would've expected the example would've worked just as much for
    1f + 2f
    isn't
    1f+2f == 3.00000000000000000000000001
    or something?
    elizarov

    elizarov

    3 years ago
    Please refer to my story. It has more detailed explanation of why it works
    Hullaballoonatic

    Hullaballoonatic

    3 years ago
    yeah, i'll read through it all. thanks!
    elizarov

    elizarov

    3 years ago
    (and 1f + 2f is always equal to 3f, no problems there, since all three numbers are exactly representable in IEEE754 double and float)
    Hullaballoonatic

    Hullaballoonatic

    3 years ago
    ah, cool
    it was beat into my head a decade ago to avoid equating floating points like the plague, so I just literally never even try
    i always do
    abs(b - a) < epsilon
    elizarov

    elizarov

    3 years ago
    That was the exactly the reason I wrote this story. Too many people are taught this like a religious dogma instead of teaching them how IEEE754 works
    Hullaballoonatic

    Hullaballoonatic

    3 years ago
    then your article is a great read for me!
    elizarov

    elizarov

    3 years ago
    (it would not really teach you full IEEE754 glory, but it has some references for further study)
    The purpose of this story was to break the dogma
    Hullaballoonatic

    Hullaballoonatic

    3 years ago
    a proof of concept that the ieee754 works, and thus the dogma is not justified
    go learn more on your own, etc
    karelpeeters

    karelpeeters

    3 years ago
    I feel like it's still misleading: sure it prints
    0.3
    but that's a feature of Java string formatting, and it's not actually
    0.3
    . That what the dogma is about. Of course if you remember to round after every operation and you're dealing with nice numbers it doesn't matter, but it's about the edge cases that always tend to show up.
    Hullaballoonatic

    Hullaballoonatic

    3 years ago
    I appreciate the general desire of the OP. floats are scientific notation, and it'd be nice to have easier access to controlling the number of sig figs you're dealing with
    but it's simply never a bad thing for the language to be storing more sig figs than necessary under the hood anyways