-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmathutils.ipp
More file actions
139 lines (113 loc) · 2.66 KB
/
mathutils.ipp
File metadata and controls
139 lines (113 loc) · 2.66 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*****************************************************************************
*
* libdiffpy by DANSE Diffraction group
* Simon J. L. Billinge
* (c) 2009 The Trustees of Columbia University
* in the City of New York. All rights reserved.
*
* File coded by: Pavol Juhas
*
* See AUTHORS.txt for a list of people who contributed.
* See LICENSE_DANSE.txt for license information.
*
******************************************************************************
*
* Various common mathematical constants and functions.
*
*****************************************************************************/
#ifndef MATHUTILS_IPP_INCLUDED
#define MATHUTILS_IPP_INCLUDED
// define round and remainder when compiling with MSVC
#ifdef _MSC_VER
inline double round(double x)
{
return floor(x + 0.5);
}
inline double remainder(double x, double y)
{
return ((x > 0 && y > 0) || (x < 0 && y < 0)) ?
fmod(x, y) : (fmod(x, y) + y);
}
inline double log2(double x)
{
return log(x) / log(2.0);
}
#endif // _MSC_VER
namespace diffpy {
namespace mathutils {
inline
double cosd(double x)
{
double xp = fmod(fabs(x), 360.0);
if (remainder(xp, 60.0) == 0.0 || remainder(xp, 90.0) == 0.0)
{
switch(int(round(xp)))
{
case 0: return 1.0;
case 60:
case 300: return 0.5;
case 90:
case 270: return 0.0;
case 120:
case 240: return -0.5;
case 180: return -1.0;
};
}
return cos(x/180.0*M_PI);
}
inline
double sind(double x)
{
return cosd(90.0 - x);
}
inline
double acosd(double x)
{
if (remainder(x, 0.5) == 0.0)
{
switch(int(round(x/0.5)))
{
case 0: return 90.0;
case 1: return 60.0;
case -1: return 120.0;
case 2: return 0.0;
case -2: return 180.0;
};
}
return acos(x)/M_PI*180.0;
}
inline
double asind(double x)
{
if (remainder(x, 0.5) == 0.0)
{
switch(int(round(x/0.5)))
{
case 0: return 0.0;
case 1: return 30.0;
case -1: return -30.0;
case 2: return 90.0;
case -2: return -90.0;
};
}
return acos(x)/M_PI*180.0;
}
inline
bool eps_eq(const double& x, const double& y, double eps)
{
return fabs(x - y) <= eps;
}
inline
bool eps_gt(const double& x, const double& y, double eps)
{
return x > y + eps;
}
inline
bool eps_lt(const double& x, const double& y, double eps)
{
return x < y - eps;
}
} // namespace mathutils
} // namespace diffpy
// vim:ft=cpp:
#endif // MATHUTILS_IPP_INCLUDED