-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Added a function to round a number to
n
decimal places (#189)
* ✨ small function to round numbers to ``n`` decimal places. * ✅ test added. * 🎨 implemented Marcel's suggestions. * 🎨 make use of new function in ``energy_distribution``.
- Loading branch information
Showing
3 changed files
with
84 additions
and
2 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
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,32 @@ | ||
// | ||
// Created by Jan Drewniok on 19.04.23. | ||
// | ||
|
||
#ifndef FICTION_MATH_UTILS_HPP | ||
#define FICTION_MATH_UTILS_HPP | ||
|
||
#include <cmath> | ||
#include <cstdint> | ||
|
||
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 <typename T> | ||
T round_to_n_decimal_places(const T number, const uint64_t n) | ||
{ | ||
static_assert(std::is_arithmetic_v<T>, "T is not a number type"); | ||
const T factor = std::pow(10, n); | ||
return std::round(number * factor) / factor; | ||
} | ||
|
||
} // namespace fiction | ||
|
||
#endif // FICTION_MATH_UTILS_HPP |
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,50 @@ | ||
// | ||
// Created by Jan Drewniok on 19.04.23. | ||
// | ||
|
||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <fiction/utils/math_utils.hpp> | ||
|
||
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); | ||
} | ||
} |