Kotlin整数相除精度损失roundToInt
import kotlin.math.roundToInt
fun main() {
val a = 0.0f
val delta = 0.1f
for (i in 0..10) {
val r = a + i * delta
println("float=${r} toInt=${r.toInt()} (+0.5 toInt)=${(r + 0.5).toInt()} round=${Math.round(r)} roundToInt=${r.roundToInt()}")
}
}
float=0.0 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.1 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.2 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.3 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.4 toInt=0 (+0.5 toInt)=0 round=0 roundToInt=0
float=0.5 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=0.6 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=0.7 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=0.8 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=0.90000004 toInt=0 (+0.5 toInt)=1 round=1 roundToInt=1
float=1.0 toInt=1 (+0.5 toInt)=1 round=1 roundToInt=1