Commit 7e304b2f authored by Paul Schütze's avatar Paul Schütze
Browse files

Merge branch 'p-fix-287' into 'master'

Units: round integral types before casting to not truncate

Closes #287

See merge request allpix-squared/allpix-squared!1116
parents 12f486e0 b771c6c1
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -11,6 +11,8 @@

#include "text.h"

#include <cmath>

namespace allpix {
    /**
     * @throws std::overflow_error If the converted unit overflows the requested type
@@ -24,6 +26,9 @@ namespace allpix {
           out < static_cast<UnitType>(std::numeric_limits<T>::lowest())) {
            throw std::overflow_error("unit conversion overflows the type");
        }
        if constexpr(std::is_integral_v<T>) {
            out = std::round(out);
        }
        return static_cast<T>(out);
    }

@@ -34,6 +39,9 @@ namespace allpix {
           out < static_cast<UnitType>(std::numeric_limits<T>::lowest())) {
            throw std::overflow_error("unit conversion overflows the type");
        }
        if constexpr(std::is_integral_v<T>) {
            out = std::round(out);
        }
        return static_cast<T>(out);
    }
    template <typename T> T Units::getSingleInverse(T inp, std::string str) {
@@ -42,6 +50,9 @@ namespace allpix {
           out < static_cast<UnitType>(std::numeric_limits<T>::lowest())) {
            throw std::overflow_error("unit conversion overflows the type");
        }
        if constexpr(std::is_integral_v<T>) {
            out = std::round(out);
        }
        return static_cast<T>(out);
    }
    template <typename T> T Units::getInverse(T inp, const std::string& str) {
@@ -50,6 +61,9 @@ namespace allpix {
           out < static_cast<UnitType>(std::numeric_limits<T>::lowest())) {
            throw std::overflow_error("unit conversion overflows the type");
        }
        if constexpr(std::is_integral_v<T>) {
            out = std::round(out);
        }
        return static_cast<T>(out);
    }