I'm finding a strange behaviour with floats in Kot...
# getting-started
m
I'm finding a strange behaviour with floats in Kotlin 1.5 multiplatform
1F - 0.9F == 0.100000024F
anyone know why the precision is getting messed up. I would expect
1F - 0.9F == 0.1F
you’re better off doing FP comparisons using abs(target, current) < EPS EPS can be a small value like 1e-9
m
awesome thanks for the resource
i had assumed it was a kotlin bug, looks like it's inherent to floats though huh?
p
correct
r
If you're on the JVM use BigDecimal if you need precise arithmetic.
it’s inherent to floats though
Or more precisly it’s a behaviour of floats according to IEEE 754 standard