Commit d6666f46 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Adding cspanf to radixmath/util for dealing with periodic intervals

parent cc83c067
......@@ -4,6 +4,34 @@
#include "radixmath/util.hh"
using namespace radix;
TEST(radix, cspanf)
{
Real lon = -190;
Real v = cspanf(lon, -180, 180);
EXPECT_FLOAT_EQ(170, v);
lon = 190;
v = cspanf(lon , -180, 180);
EXPECT_FLOAT_EQ(v, -170);
lon = 360;
v = cspanf(lon , -180, 180);
EXPECT_FLOAT_EQ(v, 0);
lon = -360;
v = cspanf(lon , -180, 180);
EXPECT_FLOAT_EQ(v, 0);
Real lat = -100;
v = cspanf(lat, -90, 90);
EXPECT_FLOAT_EQ(80, v);
lat = 100;
v = cspanf(lat, -90, 90);
EXPECT_FLOAT_EQ(-80, v);
lat = 180;
v = cspanf(lat, -90, 90);
EXPECT_FLOAT_EQ(v, 0);
lat = -180;
v = cspanf(lat, -90, 90);
EXPECT_FLOAT_EQ(v, 0);
}
TEST(radix, greatCircleDistance){
{ // Quadrant 2 north west hemisphere
double delta = 0.05;
......
......@@ -152,4 +152,19 @@ Real cylinderVolume(Real r, Real h)
return (PI * std::pow(r, 2) * h);
}
Real cspanf(Real value, Real begin, Real end)
{
Real first = 0.0, last = 0.0;
first = std::min(begin, end);
last = std::max(begin, end);
value = std::fmod(value - first,last - first);
if(value <= 0)
{
return value + last;
} else
{
return value + first;
}
}
} // namespace radix
......@@ -16,6 +16,18 @@
namespace radix
{
/**
* @brief cspanf returns a value in the interval (begin,end]
* This function is used to reduce periodic variables to a standard range.
* It adjusts for the behaviour of the mod function which provides positive results
* for position input and negative results for negative input.
* @param value number to be reduced to the span
* @param begin first value of the span
* @param end last value of the span
* @return Real the reduced value
*/
Real cspanf(Real value, Real begin, Real end);
/**
* @brief haversine The Haversine formula for calculating great-circle
* @param lat1
......
Markdown is supported
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