*Floating Point Rounding*: With the JVM it's a mes...
# stdlib
g
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?
k
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.
p
well, you could “round” it to a string representation
k
There's a string formatting syntax for rounding though.
Ah got ninja'd simple smile
😁 1
g
😞 I hate to convert it to String. It just feels that there should be a simpler way as
Float -> String -> Float
k
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
True. Got your point. I hate floating point problems.
k
Well maybe look into using
BigDecimal
instead.
p
For most usecases,
Double
is good enough
k
Not if you're thinking about rounding.
p
yeah, but that’s typically done when printing things and then it’s all strings anyway
h
Relevant:

https://youtu.be/PZRI1IfStY0

e
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
.
👍 2
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)
h
I would've expected the example would've worked just as much for
1f + 2f
isn't
1f+2f == 3.00000000000000000000000001
or something?
e
Please refer to my story. It has more detailed explanation of why it works
h
yeah, i'll read through it all. thanks!
e
(and 1f + 2f is always equal to 3f, no problems there, since all three numbers are exactly representable in IEEE754 double and float)
h
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
e
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
h
then your article is a great read for me!
e
(it would not really teach you full IEEE754 glory, but it has some references for further study)
👍 2
The purpose of this story was to break the dogma
h
a proof of concept that the ieee754 works, and thus the dogma is not justified
go learn more on your own, etc
k
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.
h
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