Last active
February 27, 2020 15:30
-
-
Save pepepper/47845fb298bf92c1ab92bfe0b53fb5c9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <Siv3D.hpp> // OpenSiv3D v0.4.2 | |
// 参考:https://cpprefjp.github.io/reference/numeric/accumulate.html | |
// https://cpprefjp.github.io/reference/iterator/distance.html | |
// https://qiita.com/shirakawa4756/items/f4cc65c6b2b412b10c0c | |
namespace s3d | |
{ | |
namespace Stat | |
{ | |
/// <summary> | |
/// 数値が入った配列のイテレータから平均値を計算します。 | |
/// </summary> | |
/// <param name="begin"> | |
/// 開始イテレータ | |
/// </param> | |
/// <param name="last"> | |
/// 終端イテレータ | |
/// </param> | |
/// <returns> | |
/// 配列内の数値の平均値 | |
/// </returns> | |
template <class Iterator> | |
[[nodiscard]] constexpr double Mean(Iterator begin, Iterator last) | |
{ | |
const double sum = std::accumulate(begin, last, 0.0); | |
const size_t count = std::distance(begin, last); | |
return sum / count; | |
} | |
/// <summary> | |
/// 数値が入った配列のイテレータから最大値を抽出します。 | |
/// </summary> | |
/// <param name="begin"> | |
/// 開始イテレータ | |
/// </param> | |
/// <param name="last"> | |
/// 終端イテレータ | |
/// </param> | |
/// <returns> | |
/// 配列内の数値の最大値 | |
/// </returns> | |
template <class Iterator> | |
[[nodiscard]] constexpr double Max(Iterator begin, Iterator last) | |
{ | |
return *std::max_element(begin, last); | |
} | |
/// <summary> | |
/// 数値が入った配列のイテレータから最小値を抽出します。 | |
/// </summary> | |
/// <param name="begin"> | |
/// 開始イテレータ | |
/// </param> | |
/// <param name="last"> | |
/// 終端イテレータ | |
/// </param> | |
/// <returns> | |
/// 配列内の数値の最小値 | |
/// </returns> | |
template <class Iterator> | |
[[nodiscard]] constexpr double Min(Iterator begin, Iterator last) | |
{ | |
return *std::min_element(begin, last); | |
} | |
} | |
} | |
void MeanTest(double a, double b, double epsilon = 1e-5) { | |
if (std::abs(a - b) < epsilon) { | |
Console << U"OK"; | |
} | |
else { | |
Console << U"Failed! expectd:{:.5f}, actual:{:.5f}"_fmt(b, a); | |
} | |
} | |
void MinMaxTest(double a, double b) { | |
if (a==b) { | |
Console << U"OK"; | |
} | |
else { | |
Console << U"Failed! expectd:{:.5f}, actual:{:.5f}"_fmt(b, a); | |
} | |
} | |
void Main() { | |
Console << U"[Mean]"; | |
{ | |
const Array<double> v = { 1.0, 2.0, 3.0, 4.0 }; | |
const std::array<float, 3> a = { 1.1f, 2.2f, 3.3f }; | |
const float b[2] = { 1.01f, 3.03f }; | |
MeanTest(Stat::Mean(std::begin(v), std::end(v)), 2.5); | |
MeanTest(Stat::Mean(std::begin(a), std::end(a)), 2.2); | |
MeanTest(Stat::Mean(std::begin(b), std::end(b)), 2.02); | |
} | |
Console << U"[Max]"; | |
{ | |
const Array<double> v = { 1.0, 2.0, 3.0, 4.0 }; | |
const std::array<float, 3> a = { 1.1f, 2.2f, 3.3f }; | |
const float b[3] = { 1.01f, 2.02f, 3.03f }; | |
MinMaxTest(Stat::Max(std::begin(v), std::end(v)), 4.0); | |
MinMaxTest(Stat::Max(std::begin(a), std::end(a)), 3.3f); | |
MinMaxTest(Stat::Max(std::begin(b), std::end(b)), 3.03f); | |
} | |
Console << U"[Min]"; | |
{ | |
const Array<double> v = { 1.0, 2.0, 3.0, 4.0 }; | |
const std::array<float, 3> a = { 1.1f, 2.2f, 3.3f }; | |
const float b[3] = { 1.01f, 2.02f, 3.03f }; | |
MinMaxTest(Stat::Min(std::begin(v), std::end(v)), 1.0); | |
MinMaxTest(Stat::Min(std::begin(a), std::end(a)), 1.1f); | |
MinMaxTest(Stat::Min(std::begin(b), std::end(b)), 1.01f); | |
} | |
while (System::Update()) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment