mirror of
https://github.com/dlang/phobos.git
synced 2025-05-05 01:20:40 +03:00
27 lines
719 B
Text
27 lines
719 B
Text
`mean` Was Added To `std.algorithm`
|
|
|
|
$(REF mean, std, algorithm, iteration) accurately finds the
|
|
mean (a.k.a the average) of any range of number-like elements.
|
|
|
|
-----
|
|
import std.algorithm.iteration : mean;
|
|
import std.math : approxEqual;
|
|
|
|
int[] arr1 = [1, 2, 3];
|
|
real[] arr2 = [1.5, 2.5, 12.5];
|
|
|
|
assert(arr1.mean.approxEqual(2));
|
|
assert(arr2.mean.approxEqual(5.5));
|
|
|
|
// user defined number types also work
|
|
import std.bigint : BigInt;
|
|
|
|
auto bigint_arr = [
|
|
BigInt("1_000_000_000_000_000_000"),
|
|
BigInt("2_000_000_000_000_000_000"),
|
|
BigInt("3_000_000_000_000_000_000"),
|
|
BigInt("6_000_000_000_000_000_000")
|
|
];
|
|
auto seed = BigInt(0);
|
|
assert(bigint_arr.mean(seed) == BigInt("3_000_000_000_000_000_000"));
|
|
-----
|