for floats? Here is what intellij shows me the signature is
Copy code
public inline fun <T> Iterable<T>.sumOf(
selector: (T) -> Double
): Double
for instance, this fails
Copy code
entries.sumOf {
5f
}
but it works if I do this
Copy code
entries.sumOf {
5f.toDouble()
}
j
Joffrey
05/06/2024, 8:02 AM
I don't have a true answer to this (I don't work in the Kotlin team), but my guess would be that adding lots of floats together might cumulate the errors due to the floats imprecision. So it might be more accurate to convert them to double, sum, and then concert back if you really need a float.
r
Ray Rahke
05/06/2024, 8:04 AM
Gotcha thanks
Ray Rahke
05/06/2024, 8:04 AM
is that expensive?
Ray Rahke
05/06/2024, 8:04 AM
toDouble() for as many entries there are, and then toFloat() a final time at the end?
e
ephemient
05/06/2024, 1:04 PM
Float is generally only useful to save on storage space, most arithmetic on Double is as fast as or faster than arithmetic on Float on most hardware (except for division and some transcendental functions)