Skip to content
Snippets Groups Projects
Commit 104e3897 authored by Sam Yates's avatar Sam Yates
Browse files

`math::infinity<>()` wrapper for infinity

parent ddc8537d
No related branches found
No related tags found
No related merge requests found
#pragma once
#include <cmath>
#include <limits>
#include <utility>
namespace nest {
namespace mc {
namespace math {
......@@ -14,6 +14,12 @@ T constexpr pi()
return T(3.1415926535897932384626433832795);
}
template <typename T = float>
T constexpr infinity()
{
return std::numeric_limits<T>::infinity();
}
template <typename T>
T constexpr mean(T a, T b)
{
......
......@@ -12,6 +12,7 @@ set(TEST_SOURCES
test_cell_group.cpp
test_lexcmp.cpp
test_mask_stream.cpp
test_math.cpp
test_matrix.cpp
test_mechanisms.cpp
test_nop.cpp
......
#include <cmath>
#include <limits>
#include "gtest.h"
#include <math.hpp>
using namespace nest::mc::math;
TEST(math, infinity) {
// check values for float, double, long double
auto finf = infinity<float>();
EXPECT_TRUE((std::is_same<float, decltype(finf)>::value));
EXPECT_TRUE(std::isinf(finf));
EXPECT_GT(finf, 0.f);
auto dinf = infinity<double>();
EXPECT_TRUE((std::is_same<double, decltype(dinf)>::value));
EXPECT_TRUE(std::isinf(dinf));
EXPECT_GT(dinf, 0.0);
auto ldinf = infinity<long double>();
EXPECT_TRUE((std::is_same<long double, decltype(ldinf)>::value));
EXPECT_TRUE(std::isinf(ldinf));
EXPECT_GT(ldinf, 0.0l);
// check default value promotes correctly (i.e., acts like INFINITY)
struct {
float f;
double d;
long double ld;
} check = {infinity<>(), infinity<>(), infinity<>()};
EXPECT_EQ(std::numeric_limits<float>::infinity(), check.f);
EXPECT_EQ(std::numeric_limits<double>::infinity(), check.d);
EXPECT_EQ(std::numeric_limits<long double>::infinity(), check.ld);
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment