MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / arithmetic_mean

Function arithmetic_mean

Examples/NoModules/Chapter 11/Ex11_07/math.cpp:14–25  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12namespace math::averages
13{
14 double arithmetic_mean(std::span<const double> data)
15 {
16 // The arithmetic mean, the most commonly used average,
17 // is defined as the sum of all elements divided by the number of elements.
18 double sum {};
19 for (auto value : data)
20 sum += value;
21
22 return data.empty()
23 ? std::numeric_limits<double>::quiet_NaN() // Or std::nan("")
24 : sum / data.size();
25 }
26}
27
28// Option 2: define in nested namespace blocks

Callers 1

mainFunction · 0.70

Calls 2

emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected