-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircHelper.h
62 lines (51 loc) · 1.91 KB
/
CircHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// ==========================================================================
// Copyright (C) 2011 Lior Kogan ([email protected])
// ==========================================================================
#pragma once
// ==========================================================================
static const double _2Pi = 6.2831853071795864769252867665590057683943387987502116419498891846156328125724179972560696;
// ==========================================================================
// square (x*x)
template <typename T>
T Sqr(const T& x)
{
return x*x;
}
// ==========================================================================
// Floating-point modulo
// The result (the remainder) has the same sign as the divisor.
// Similar to Matlab's mod(); Not similar to fmod() - Mod(-3,4)= 1 fmod(-3,4)= -3
template<typename T>
T Mod(T x, T y)
{
static_assert(!std::numeric_limits<T>::is_exact , "Mod: floating-point type expected");
if (0. == y)
return x;
double m = x - y * floor(x/y);
// handle boundary cases resulting from floating-point limited accuracy:
if (y > 0) // modulo range: [0..y)
{
if (m >= y) // Mod(-1e-16 , 360. ): m= 360.
return 0;
if (m < 0 )
{
if (y + m == y)
return 0 ; // just in case...
else
return y + m; // Mod(106.81415022205296 , _TWO_PI ): m= -1.421e-14
}
}
else // modulo range: (y..0]
{
if (m <= y) // Mod(1e-16 , -360. ): m= -360.
return 0;
if (m > 0 )
{
if (y + m == y)
return 0 ; // just in case...
else
return y + m; // Mod(-106.81415022205296, -_TWO_PI): m= 1.421e-14
}
}
return m;
}