3
* \brief Low level math functions and compatibility wrappers
6
* Johan Engelen <goejendaagh@zonnet.nl>
7
* Michael G. Sloan <mgsloan@gmail.com>
8
* Krzysztof Kosiński <tweenk.pl@gmail.com>
9
* Copyright 2006-2009 Authors
11
* This library is free software; you can redistribute it and/or
12
* modify it either under the terms of the GNU Lesser General Public
13
* License version 2.1 as published by the Free Software Foundation
14
* (the "LGPL") or, at your option, under the terms of the Mozilla
15
* Public License Version 1.1 (the "MPL"). If you do not alter this
16
* notice, a recipient may use your version of this file under either
17
* the MPL or the LGPL.
19
* You should have received a copy of the LGPL along with this library
20
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
* You should have received a copy of the MPL along with this library
23
* in the file COPYING-MPL-1.1
25
* The contents of this file are subject to the Mozilla Public License
26
* Version 1.1 (the "License"); you may not use this file except in
27
* compliance with the License. You may obtain a copy of the License at
28
* http://www.mozilla.org/MPL/
30
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
31
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
32
* the specific language governing rights and limitations.
36
#ifndef LIB2GEOM_SEEN_MATH_UTILS_H
37
#define LIB2GEOM_SEEN_MATH_UTILS_H
40
#include <math.h> // sincos is usually only available in math.h
42
#include <utility> // for std::pair
46
/** @brief Sign function - indicates the sign of a numeric type.
47
* Mathsy people will know this is basically the derivative of abs, except for the fact
48
* that it is defined on 0.
49
* @return -1 when x is negative, 1 when positive, and 0 if equal to 0. */
50
template <class T> inline int sgn(const T& x) {
51
return (x < 0 ? -1 : (x > 0 ? 1 : 0) );
54
template <class T> inline T sqr(const T& x) {return x * x;}
55
template <class T> inline T cube(const T& x) {return x * x * x;}
57
/** Between function - returns true if a number x is within a range: (min < x) && (max > x).
58
* The values delimiting the range and the number must have the same type.
60
template <class T> inline const T& between (const T& min, const T& max, const T& x)
61
{ return (min < x) && (max > x); }
63
/** @brief Returns @a x rounded to the nearest multiple of \f$10^{p}\f$.
65
Implemented in terms of round, i.e. we make no guarantees as to what happens if x is
66
half way between two rounded numbers.
68
Note: places is the number of decimal places without using scientific (e) notation, not the
69
number of significant figures. This function may not be suitable for values of x whose
70
magnitude is so far from 1 that one would want to use scientific (e) notation.
72
places may be negative: e.g. places = -2 means rounding to a multiple of .01
74
inline double decimal_round(double x, int p) {
75
//TODO: possibly implement with modulus instead?
76
double const multiplier = ::pow(10.0, p);
77
return ::round( x * multiplier ) / multiplier;
80
/** @brief Simultaneously compute a sine and a cosine of the same angle.
81
* This function can be up to 2 times faster than separate computation, depending
82
* on the platform. It uses the standard library function sincos() if available.
84
* @param sin_ Variable that will store the sine
85
* @param cos_ Variable that will store the cosine */
86
inline void sincos(double angle, double &sin_, double &cos_) {
88
::sincos(angle, &sin_, &cos_);
95
/* Temporary fix for various misdefinitions of isnan().
96
* isnan() is becoming undef'd in some .h files.
97
* #include this last in your .cpp file to get it right.
99
* The problem is that isnan and isfinite are part of C99 but aren't part of
100
* the C++ standard (which predates C99).
104
# define IS_NAN(_a) (__isnan(_a))
105
#elif defined(__APPLE__) && __GNUC__ == 3
106
# define IS_NAN(_a) (__isnan(_a)) /* MacOSX/Darwin definition < 10.4 */
107
#elif defined(WIN32) || defined(_isnan)
108
# define IS_NAN(_a) (_isnan(_a)) /* Win32 definition */
109
#elif defined(isnan) || defined(__FreeBSD__) || defined(__osf__)
110
# define IS_NAN(_a) (isnan(_a)) /* GNU definition */
111
#elif defined (SOLARIS_2_8) && __GNUC__ == 3 && __GNUC_MINOR__ == 2
112
# define IS_NAN(_a) (isnan(_a)) /* GNU definition */
114
# define IS_NAN(_a) (std::isnan(_a))
116
/* If the above doesn't work, then try (a != a). */
119
#if defined(__isfinite)
120
# define IS_FINITE(_a) (__isfinite(_a))
121
#elif defined(__APPLE__) && __GNUC__ == 3
122
# define IS_FINITE(_a) (__isfinite(_a)) /* MacOSX/Darwin definition < 10.4 */
124
# define IS_FINITE(_a) (_isfinite(_a))
125
#elif defined(isfinite)
126
# define IS_FINITE(_a) (isfinite(_a))
127
#elif defined(__osf__)
128
# define IS_FINITE(_a) (finite(_a) && !IS_NAN(_a))
129
#elif defined (SOLARIS_2_8) && __GNUC__ == 3 && __GNUC_MINOR__ == 2
131
#define IS_FINITE(_a) (finite(_a) && !IS_NAN(_a))
133
# define IS_FINITE(_a) (std::isfinite(_a))
136
} // end namespace Geom
138
#endif // LIB2GEOM_SEEN_MATH_UTILS_H
143
c-file-style:"stroustrup"
144
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
149
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :