Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b7ceff
commit b5572e8
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
`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")); | ||
----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters