Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Average of Array
- The average is defined as the sum of each value divided by count.
- */
- template< class T >
- FORCEINLINE auto Avg(const TArray<T>& Values) -> decltype(T() / double())
- {
- decltype(T() / double()) sum;
- if (ensure(Values.Num() > 0))
- {
- auto iter = begin(Values);
- double num = Values.Num();
- //initialize the result as the partial result from first element because the element type could be uninitialized, e.g. FVector
- sum = *iter / num;
- ++iter;
- do
- {
- sum += *iter / num;
- ++iter;
- } while (iter != end(Values));
- }
- else
- {
- sum = T() / 1.0; //fall back to whatever default value, exception handled in ensure
- }
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement