mirror of
https://github.com/janet-lang/janet
synced 2024-12-24 15:30:27 +00:00
Add geomean function.
This commit is contained in:
parent
d1104b5a65
commit
aaeaa3a944
@ -711,6 +711,19 @@
|
||||
(each x xs (+= accum x) (++ total))
|
||||
(/ accum total))))
|
||||
|
||||
(defn geomean
|
||||
"Returns the geometric mean of xs. If empty, returns NaN."
|
||||
[xs]
|
||||
(if (lengthable? xs)
|
||||
(do
|
||||
(var accum 0)
|
||||
(each x xs (+= accum (math/log x)))
|
||||
(math/exp (/ accum (length xs))))
|
||||
(do
|
||||
(var [accum total] [0 0])
|
||||
(each x xs (+= accum (math/log x)) (++ total))
|
||||
(math/exp (/ accum total)))))
|
||||
|
||||
(defn product
|
||||
"Returns the product of xs. If xs is empty, returns 1."
|
||||
[xs]
|
||||
|
Loading…
Reference in New Issue
Block a user