Skip to content

Commit

Permalink
✨ Added a function to round a number to n decimal places (#189)
Browse files Browse the repository at this point in the history
* ✨ 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
Drewniok committed Apr 19, 2023
1 parent 6d3bf1e commit 329e749
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define FICTION_ENERGY_DISTRIBUTION_HPP

#include "fiction/technology/charge_distribution_surface.hpp"
#include "fiction/utils/math_utils.hpp"

#include <cmath>
#include <cstdint>
Expand Down Expand Up @@ -34,8 +35,7 @@ std::map<double, uint64_t> energy_distribution(const std::vector<charge_distribu

for (const auto& lyt : input_vec)
{
const auto energy =
std::round(lyt.get_system_energy() * 1'000'000) / 1'000'000; // rounding to 6 decimal places.
const auto energy = round_to_n_decimal_places(lyt.get_system_energy(), 6); // rounding to 6 decimal places.

distribution[energy]++;
}
Expand Down
32 changes: 32 additions & 0 deletions include/fiction/utils/math_utils.hpp
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
50 changes: 50 additions & 0 deletions test/utils/math_utils.cpp
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);
}
}

0 comments on commit 329e749

Please sign in to comment.