https://kotlinlang.org logo
#stdlib
Title
# stdlib
g

guenther

06/27/2019, 3:24 PM
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

karelpeeters

06/27/2019, 3:28 PM
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

pavel

06/27/2019, 3:29 PM
well, you could “round” it to a string representation
k

karelpeeters

06/27/2019, 3:29 PM
There's a string formatting syntax for rounding though.
Ah got ninja'd simple smile
😁 1
g

guenther

06/27/2019, 3:42 PM
😞 I hate to convert it to String. It just feels that there should be a simpler way as
Float -> String -> Float
k

karelpeeters

06/27/2019, 3:43 PM
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

06/27/2019, 3:53 PM
True. Got your point. I hate floating point problems.
k

karelpeeters

06/27/2019, 3:53 PM
Well maybe look into using
BigDecimal
instead.
p

pavel

06/27/2019, 3:54 PM
For most usecases,
Double
is good enough
k

karelpeeters

06/27/2019, 3:55 PM
Not if you're thinking about rounding.
p

pavel

06/27/2019, 3:56 PM
yeah, but that’s typically done when printing things and then it’s all strings anyway
h

Hullaballoonatic

06/27/2019, 6:50 PM
Relevant:

https://youtu.be/PZRI1IfStY0

e

elizarov

06/27/2019, 9:30 PM
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

Hullaballoonatic

06/27/2019, 9:33 PM
I would've expected the example would've worked just as much for
1f + 2f
isn't
1f+2f == 3.00000000000000000000000001
or something?
e

elizarov

06/27/2019, 9:34 PM
Please refer to my story. It has more detailed explanation of why it works
h

Hullaballoonatic

06/27/2019, 9:34 PM
yeah, i'll read through it all. thanks!
e

elizarov

06/27/2019, 9:34 PM
(and 1f + 2f is always equal to 3f, no problems there, since all three numbers are exactly representable in IEEE754 double and float)
h

Hullaballoonatic

06/27/2019, 9:35 PM
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

elizarov

06/27/2019, 9:36 PM
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

Hullaballoonatic

06/27/2019, 9:37 PM
then your article is a great read for me!
e

elizarov

06/27/2019, 9:37 PM
(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

Hullaballoonatic

06/27/2019, 9:38 PM
a proof of concept that the ieee754 works, and thus the dogma is not justified
go learn more on your own, etc
k

karelpeeters

06/27/2019, 9:39 PM
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

Hullaballoonatic

06/27/2019, 9:41 PM
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
40 Views