forked from ClimateGlobalChange/tempestremap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathHelper.h
130 lines (112 loc) · 2.75 KB
/
MathHelper.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
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
///////////////////////////////////////////////////////////////////////////////
///
/// \file MathHelper.h
/// \author Paul Ullrich
/// \version July 26, 2010
///
/// <summary>
/// This header file provides access to various mathematical functions
/// that are not normally exposed in the standard C++ libraries.
/// </summary>
/// <remarks>
/// Copyright 2000-2010 Paul Ullrich
///
/// This file is distributed as part of the Tempest source code package.
/// Permission is granted to use, copy, modify and distribute this
/// source code and its documentation under the terms of the GNU General
/// Public License. This software is provided "as is" without express
/// or implied warranty.
/// </remarks>
#ifndef _MATHHELPER_H_
#define _MATHHELPER_H_
///////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Calculate the maximum of two values.
/// </summary>
template <typename _T>
_T Max(_T x1, _T x2) {
return (x1>x2)?(x1):(x2);
}
/// <summary>
/// Calculate the minimum of two values.
/// </summary>
template <typename _T>
_T Min(_T x1, _T x2) {
return (x1<x2)?(x1):(x2);
}
/// <summary>
/// Calculate the sign of a value.
/// </summary>
template <typename _T>
_T Sign(_T x1) {
return (x1 < static_cast<_T>(0))?(static_cast<_T>(-1)):(static_cast<_T>(1));
}
/// <summary>
/// Clamp a value to be within a given range.
/// </summary>
template <typename _T>
_T Clamp(_T y, _T x1, _T x2) {
return (y>x2)?(x2):((y<x1)?(x1):(y));
}
/// <summary>
/// Calculate the integer square root.
/// </summary>
/// <remarks>
/// Source: Crenshaw, Jack. Integer square roots.
/// http://www.embedded.com/98/9802fe2.htm
/// </remarks>
inline unsigned int ISqrt(unsigned int a) {
unsigned int irem = 0;
unsigned int iroot = 0;
for (int i = 0; i < 16; i++) {
iroot <<= 1;
irem = ((irem << 2) + (a >> 30));
a <<= 2;
iroot++;
if (iroot <= irem) {
irem -= iroot;
iroot++;
} else {
iroot--;
}
}
return (static_cast<unsigned int>(iroot >> 1));
}
/// <summary>
/// Calculate the integer power of integers function.
/// </summary>
inline int IntPow(int d, unsigned int p) {
if (p == 0) {
return 1;
}
unsigned int q;
int iPow = d;
for (q = 1; q < p; q++) {
iPow *= d;
}
return iPow;
}
/// <summary>
/// Calculate the integer power function.
/// </summary>
inline double IPow(double d, unsigned int p) {
unsigned int q;
double dPow = 1.0;
for (q = 0; q < p; q++) {
dPow *= d;
}
return dPow;
}
/// <summary>
/// Calculate the integer factorial function.
/// </summary>
inline unsigned int IFact(unsigned int p) {
unsigned int q;
unsigned int iFact = 1;
for (q = 2; q <= p; q++) {
iFact *= q;
}
return iFact;
}
///////////////////////////////////////////////////////////////////////////////
#endif