Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Added a function to round a number to n decimal places #189

Merged
merged 13 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}