NEURON
math.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1991 Stanford University
3  * Copyright (c) 1991 Silicon Graphics, Inc.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that (i) the above copyright notices and this permission notice appear in
8  * all copies of the software and related documentation, and (ii) the names of
9  * Stanford and Silicon Graphics may not be used in any advertising or
10  * publicity relating to the software without the specific, prior written
11  * permission of Stanford and Silicon Graphics.
12  *
13  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
18  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22  * OF THIS SOFTWARE.
23  */
24 
25 #ifndef os_math_h
26 #define os_math_h
27 
28 #ifdef WIN32
29 #ifdef max
30 #undef max
31 #endif
32 #ifdef min
33 #undef min
34 #endif
35 #endif
36 
37 #include <OS/enter-scope.h>
38 
39 /*
40  * Common math operations on built-in types.
41  */
42 
43 #define declare_binary_minmax(Type) \
44  static Type min(Type a, Type b); \
45  static Type max(Type a, Type b)
46 
47 #define implement_binary_minmax(Type) \
48  inline Type Math::min(Type a, Type b) { return a < b ? a : b; } \
49  inline Type Math::max(Type a, Type b) { return a > b ? a : b; }
50 
51 #define declare_4_minmax(Type) \
52  static Type min(Type a, Type b, Type c, Type d); \
53  static Type max(Type a, Type b, Type c, Type d)
54 
55 /*
56  * Compiler isn't smart enough to figure out how to do a 4-way min inline
57  * with single nested if-then-else.
58  */
59 
60 #define implement_4_minmax(Type) \
61  inline Type Math::min(Type a, Type b, Type c, Type d) { \
62  Type r1 = min(a, b), r2 = min(c, d); \
63  return min(r1, r2); \
64  } \
65 \
66  inline Type Math::max(Type a, Type b, Type c, Type d) { \
67  Type r1 = max(a, b), r2 = max(c, d); \
68  return max(r1, r2); \
69  }
70 
71 class Math {
72 public:
76  declare_binary_minmax(unsigned long);
82 
83  static int abs(int);
84  static long abs(long);
85  static double abs(double);
86 
87  static int round(float);
88  static int round(double);
89 
90  static bool equal(float x, float y, float e);
91  static bool equal(double x, double y, double e);
92 };
93 
97 implement_binary_minmax(unsigned long)
100 
102 implement_4_minmax(float)
103 implement_4_minmax(double)
104 
105 inline int Math::round(float x) { return x > 0 ? int(x+0.5) : -int(-x+0.5); }
106 inline int Math::round(double x) { return x > 0 ? int(x+0.5) : -int(-x+0.5); }
107 
108 inline bool Math::equal(float x, float y, float e) {
109  return x - y < e && y - x < e;
110 }
111 
112 inline bool Math::equal(double x, double y, double e) {
113  return x - y < e && y - x < e;
114 }
115 
116 #endif
Definition: math.h:71
declare_binary_minmax(double)
declare_binary_minmax(long)
declare_4_minmax(double)
declare_binary_minmax(unsigned)
declare_binary_minmax(unsigned long)
static int round(float)
static bool equal(float x, float y, float e)
Definition: math.h:108
declare_binary_minmax(float)
declare_binary_minmax(int)
declare_4_minmax(float)
declare_4_minmax(int)
static int abs(int)
Definition: math.cpp:43
#define implement_4_minmax(Type)
Definition: math.h:60
#define implement_binary_minmax(Type)
Definition: math.h:47
#define e
Definition: passive0.cpp:22