https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

Michael Evans

01/26/2022, 11:59 PM
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

Michael Evans

01/27/2022, 12:23 AM
awesome thanks for the resource
i had assumed it was a kotlin bug, looks like it's inherent to floats though huh?
p

Pratik Tandel

01/27/2022, 12:33 AM
correct
r

Rob Elliot

01/27/2022, 12:46 AM
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
3 Views