From 9622e834ffc98d84bbae4eae9001c1bfbe00b105 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 2 Apr 2023 15:38:54 +0200 Subject: [PATCH 1/6] :arrow_up: Bump libs/Catch2 from `6783411` to `1f881ab` (#27) Bumps [libs/Catch2](https://github.com/catchorg/Catch2) from `6783411` to `1f881ab`. - [Release notes](https://github.com/catchorg/Catch2/releases) - [Commits](https://github.com/catchorg/Catch2/compare/6783411349db5a87718c9b7ba8b02e90e39d783e...1f881ab4641b866304b818a063164dae976dba5f) --- updated-dependencies: - dependency-name: libs/Catch2 dependency-type: direct:production ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> From 8f5f52fb110636dbf817ae754fc4a23e9d0e9f4d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:02:59 +0200 Subject: [PATCH 2/6] :arrow_up: Bump libs/parallel-hashmap from `7883cb6` to `d2bed96` (#33) Bumps [libs/parallel-hashmap](https://github.com/greg7mdp/parallel-hashmap) from `7883cb6` to `d2bed96`. - [Release notes](https://github.com/greg7mdp/parallel-hashmap/releases) - [Commits](https://github.com/greg7mdp/parallel-hashmap/compare/7883cb6cd1a1b3dbe80a8f340cd9ff8167e5bdba...d2bed96d2947c13b8fc90337198c315b2200e058) --- updated-dependencies: - dependency-name: libs/parallel-hashmap dependency-type: direct:production ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> From b987003e4f4a12b12b9d0d463009ab59952b29dd Mon Sep 17 00:00:00 2001 From: Drewniok Date: Wed, 19 Apr 2023 10:55:15 +0200 Subject: [PATCH 3/6] :sparkles: small function to round numbers to ``n`` decimal places. --- .../fiction/utils/round_n_decimal_places.hpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 include/fiction/utils/round_n_decimal_places.hpp diff --git a/include/fiction/utils/round_n_decimal_places.hpp b/include/fiction/utils/round_n_decimal_places.hpp new file mode 100644 index 000000000..7bdfa44c3 --- /dev/null +++ b/include/fiction/utils/round_n_decimal_places.hpp @@ -0,0 +1,31 @@ +// +// Created by Jan Drewniok on 19.04.23. +// + +#ifndef FICTION_ROUND_N_DECIMAL_PLACES_HPP +#define FICTION_ROUND_N_DECIMAL_PLACES_HPP + +#include + +namespace fiction +{ + +/** + * Rounds a number to a specified number of decimal places. + * + * @tparam T the type of the number to round. + * @param number the number to round. + * @param n the number of decimal places to round to. + * @return the number rounded to n decimal places. + */ + +template +T round_n_decimal_places(T number, const uint64_t n) +{ + T factor = std::pow(10, n); + return std::round(number * factor) / factor; +} + +} // namespace fiction + +#endif // FICTION_ROUND_N_DECIMAL_PLACES_HPP From 68c054016e4b6e38be1e3bc7a65af19a4f6262a4 Mon Sep 17 00:00:00 2001 From: Drewniok Date: Wed, 19 Apr 2023 10:55:31 +0200 Subject: [PATCH 4/6] :white_check_mark: test added. --- test/utils/round_n_decimal_places.cpp | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/utils/round_n_decimal_places.cpp diff --git a/test/utils/round_n_decimal_places.cpp b/test/utils/round_n_decimal_places.cpp new file mode 100644 index 000000000..fa6131129 --- /dev/null +++ b/test/utils/round_n_decimal_places.cpp @@ -0,0 +1,50 @@ +// +// Created by Jan Drewniok on 19.04.23. +// + +#include + +#include + +using namespace fiction; + +TEST_CASE("round_n_decimal_places should round an input number to n decimal places", "[round_n_decimal_places]") +{ + SECTION("int64_t") + { + CHECK(round_n_decimal_places(-1LL, 0) == -1LL); + CHECK(round_n_decimal_places(-1LL, 10) == -1LL); + CHECK(round_n_decimal_places(1LL, 0) == 1LL); + CHECK(round_n_decimal_places(1LL, 10) == 1LL); + } + + SECTION("double") + { + const double value_positive = 3.145926; + CHECK(round_n_decimal_places(value_positive, 0) == 3); + CHECK(round_n_decimal_places(value_positive, 1) == 3.1); + CHECK(round_n_decimal_places(value_positive, 2) == 3.15); + CHECK(round_n_decimal_places(value_positive, 3) == 3.146); + CHECK(round_n_decimal_places(value_positive, 4) == 3.1459); + CHECK(round_n_decimal_places(value_positive, 5) == 3.14593); + CHECK(round_n_decimal_places(value_positive, 6) == 3.145926); + + const double value_negative = -3.145926; + CHECK(round_n_decimal_places(value_negative, 0) == -3); + CHECK(round_n_decimal_places(value_negative, 1) == -3.1); + CHECK(round_n_decimal_places(value_negative, 2) == -3.15); + CHECK(round_n_decimal_places(value_negative, 3) == -3.146); + CHECK(round_n_decimal_places(value_negative, 4) == -3.1459); + CHECK(round_n_decimal_places(value_negative, 5) == -3.14593); + CHECK(round_n_decimal_places(value_negative, 6) == -3.145926); + } + + SECTION("Edge cases") + { + CHECK(round_n_decimal_places(1.005, 2) == 1.0); + CHECK(round_n_decimal_places(0.000001, 6) == 0.000001); + CHECK(round_n_decimal_places(0.0000001, 6) == 0); + CHECK(round_n_decimal_places(-0.000001, 6) == -0.000001); + CHECK(round_n_decimal_places(-0.0000001, 6) == 0); + } +} From 1c4c5dcd4407fdff45bb6aefda1cefdfbb544b71 Mon Sep 17 00:00:00 2001 From: Drewniok Date: Wed, 19 Apr 2023 14:03:54 +0200 Subject: [PATCH 5/6] :art: implemented Marcel's suggestions. --- ...nd_n_decimal_places.hpp => math_utils.hpp} | 13 ++--- test/utils/math_utils.cpp | 50 +++++++++++++++++++ test/utils/round_n_decimal_places.cpp | 50 ------------------- 3 files changed, 57 insertions(+), 56 deletions(-) rename include/fiction/utils/{round_n_decimal_places.hpp => math_utils.hpp} (61%) create mode 100644 test/utils/math_utils.cpp delete mode 100644 test/utils/round_n_decimal_places.cpp diff --git a/include/fiction/utils/round_n_decimal_places.hpp b/include/fiction/utils/math_utils.hpp similarity index 61% rename from include/fiction/utils/round_n_decimal_places.hpp rename to include/fiction/utils/math_utils.hpp index 7bdfa44c3..813c140a9 100644 --- a/include/fiction/utils/round_n_decimal_places.hpp +++ b/include/fiction/utils/math_utils.hpp @@ -2,10 +2,11 @@ // Created by Jan Drewniok on 19.04.23. // -#ifndef FICTION_ROUND_N_DECIMAL_PLACES_HPP -#define FICTION_ROUND_N_DECIMAL_PLACES_HPP +#ifndef FICTION_MATH_UTILS_HPP +#define FICTION_MATH_UTILS_HPP #include +#include namespace fiction { @@ -18,14 +19,14 @@ namespace fiction * @param n the number of decimal places to round to. * @return the number rounded to n decimal places. */ - template -T round_n_decimal_places(T number, const uint64_t n) +T round_to_n_decimal_places(const T number, const uint64_t n) { - T factor = std::pow(10, n); + static_assert(std::is_arithmetic_v, "T is not a number type"); + const T factor = std::pow(10, n); return std::round(number * factor) / factor; } } // namespace fiction -#endif // FICTION_ROUND_N_DECIMAL_PLACES_HPP +#endif // FICTION_MATH_UTILS_HPP diff --git a/test/utils/math_utils.cpp b/test/utils/math_utils.cpp new file mode 100644 index 000000000..3ee42f4da --- /dev/null +++ b/test/utils/math_utils.cpp @@ -0,0 +1,50 @@ +// +// Created by Jan Drewniok on 19.04.23. +// + +#include + +#include + +using namespace fiction; + +TEST_CASE("round_to_n_decimal_places should round an input number to n decimal places", "[round_to_n_decimal_places]") +{ + SECTION("int64_t") + { + CHECK(round_to_n_decimal_places(-1LL, 0) == -1LL); + CHECK(round_to_n_decimal_places(-1LL, 10) == -1LL); + CHECK(round_to_n_decimal_places(1LL, 0) == 1LL); + CHECK(round_to_n_decimal_places(1LL, 10) == 1LL); + } + + SECTION("double") + { + const double value_positive = 3.145926; + CHECK(round_to_n_decimal_places(value_positive, 0) == 3); + CHECK(round_to_n_decimal_places(value_positive, 1) == 3.1); + CHECK(round_to_n_decimal_places(value_positive, 2) == 3.15); + CHECK(round_to_n_decimal_places(value_positive, 3) == 3.146); + CHECK(round_to_n_decimal_places(value_positive, 4) == 3.1459); + CHECK(round_to_n_decimal_places(value_positive, 5) == 3.14593); + CHECK(round_to_n_decimal_places(value_positive, 6) == 3.145926); + + const double value_negative = -3.145926; + CHECK(round_to_n_decimal_places(value_negative, 0) == -3); + CHECK(round_to_n_decimal_places(value_negative, 1) == -3.1); + CHECK(round_to_n_decimal_places(value_negative, 2) == -3.15); + CHECK(round_to_n_decimal_places(value_negative, 3) == -3.146); + CHECK(round_to_n_decimal_places(value_negative, 4) == -3.1459); + CHECK(round_to_n_decimal_places(value_negative, 5) == -3.14593); + CHECK(round_to_n_decimal_places(value_negative, 6) == -3.145926); + } + + SECTION("Edge cases") + { + CHECK(round_to_n_decimal_places(1.005, 2) == 1.0); + CHECK(round_to_n_decimal_places(0.000001, 6) == 0.000001); + CHECK(round_to_n_decimal_places(0.0000001, 6) == 0); + CHECK(round_to_n_decimal_places(-0.000001, 6) == -0.000001); + CHECK(round_to_n_decimal_places(-0.0000001, 6) == 0); + } +} diff --git a/test/utils/round_n_decimal_places.cpp b/test/utils/round_n_decimal_places.cpp deleted file mode 100644 index fa6131129..000000000 --- a/test/utils/round_n_decimal_places.cpp +++ /dev/null @@ -1,50 +0,0 @@ -// -// Created by Jan Drewniok on 19.04.23. -// - -#include - -#include - -using namespace fiction; - -TEST_CASE("round_n_decimal_places should round an input number to n decimal places", "[round_n_decimal_places]") -{ - SECTION("int64_t") - { - CHECK(round_n_decimal_places(-1LL, 0) == -1LL); - CHECK(round_n_decimal_places(-1LL, 10) == -1LL); - CHECK(round_n_decimal_places(1LL, 0) == 1LL); - CHECK(round_n_decimal_places(1LL, 10) == 1LL); - } - - SECTION("double") - { - const double value_positive = 3.145926; - CHECK(round_n_decimal_places(value_positive, 0) == 3); - CHECK(round_n_decimal_places(value_positive, 1) == 3.1); - CHECK(round_n_decimal_places(value_positive, 2) == 3.15); - CHECK(round_n_decimal_places(value_positive, 3) == 3.146); - CHECK(round_n_decimal_places(value_positive, 4) == 3.1459); - CHECK(round_n_decimal_places(value_positive, 5) == 3.14593); - CHECK(round_n_decimal_places(value_positive, 6) == 3.145926); - - const double value_negative = -3.145926; - CHECK(round_n_decimal_places(value_negative, 0) == -3); - CHECK(round_n_decimal_places(value_negative, 1) == -3.1); - CHECK(round_n_decimal_places(value_negative, 2) == -3.15); - CHECK(round_n_decimal_places(value_negative, 3) == -3.146); - CHECK(round_n_decimal_places(value_negative, 4) == -3.1459); - CHECK(round_n_decimal_places(value_negative, 5) == -3.14593); - CHECK(round_n_decimal_places(value_negative, 6) == -3.145926); - } - - SECTION("Edge cases") - { - CHECK(round_n_decimal_places(1.005, 2) == 1.0); - CHECK(round_n_decimal_places(0.000001, 6) == 0.000001); - CHECK(round_n_decimal_places(0.0000001, 6) == 0); - CHECK(round_n_decimal_places(-0.000001, 6) == -0.000001); - CHECK(round_n_decimal_places(-0.0000001, 6) == 0); - } -} From e5d75ccccec702e35a45d8391549a4d52f4ae1c8 Mon Sep 17 00:00:00 2001 From: Drewniok Date: Wed, 19 Apr 2023 14:05:59 +0200 Subject: [PATCH 6/6] :art: make use of new function in ``energy_distribution``. --- .../algorithms/simulation/sidb/energy_distribution.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/fiction/algorithms/simulation/sidb/energy_distribution.hpp b/include/fiction/algorithms/simulation/sidb/energy_distribution.hpp index 9fafd752d..f06427573 100644 --- a/include/fiction/algorithms/simulation/sidb/energy_distribution.hpp +++ b/include/fiction/algorithms/simulation/sidb/energy_distribution.hpp @@ -6,6 +6,7 @@ #define FICTION_ENERGY_DISTRIBUTION_HPP #include "fiction/technology/charge_distribution_surface.hpp" +#include "fiction/utils/math_utils.hpp" #include #include @@ -34,8 +35,7 @@ std::map energy_distribution(const std::vector