why is there not a `sumOf` for floats? Here is wha...
# getting-started
r
why is there not a
sumOf
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
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
Gotcha thanks
is that expensive?
toDouble() for as many entries there are, and then toFloat() a final time at the end?
e
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)
170 Views