Averages/Pythagorean means: Difference between revisions

→‎{{header|Kotlin}}: kotlin/js compatability, harmonic mean: reduce to fold since first element is not divided
(added Arturo)
(→‎{{header|Kotlin}}: kotlin/js compatability, harmonic mean: reduce to fold since first element is not divided)
Line 1,708:
 
=={{header|Kotlin}}==
<lang kotlin>import kotlin.math.round
<lang scala>fun Collection<Double>.geometricMean() =
import kotlin.math.pow
if (isEmpty()) Double.NaN
else Math.pow(reduce { n1, n2 -> n1 * n2 }, 1.0 / size)
 
<lang scala>fun Collection<Double>.geometricMean() =
if (isEmpty()) Double.NaN
else Math.pow(reduce { n1, n2 -> n1 * n2 }, ).pow(1.0 / size)
fun Collection<Double>.harmonicMean() =
if (isEmpty() || contains(0.0)) Double.NaN
else size / reducefold(0.0) { n1, n2 -> n1 + 1.0 / n2 }
 
fun mainDouble.toFixed(argslen: Array<String>Int = 6) {=
round(this * 10.0.pow(len)) / 10.0.pow(len)
fun main() {
val list = listOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
val a = list.average() // arithmetic mean
val g = list.geometricMean()
val h = list.harmonicMean()
println("A = %f$a G = %f${g.toFixed()} H = %f"${h.formattoFixed(a, g, h)}")
println("A >= G is %b${a >= g}, G >= H is %b".format(a >= g, ${g >= h)}")
require(g in h..a)
}</lang>
2

edits