Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[inkscape.git] / src / 2geom / utils.h
1 #ifndef LIB2GEOM_UTILS_HEADER
2 #define LIB2GEOM_UTILS_HEADER
4 /**
5  * \file
6  * \brief  Various utility functions.
7  *
8  * Copyright 2007 Johan Engelen <goejendaagh@zonnet.nl>
9  * Copyright 2006 Michael G. Sloan <mgsloan@gmail.com>
10  *
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.
18  *
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
24  *
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/
29  *
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.
33  *
34  */
36 #include <cmath>
37 #include <vector>
39 namespace Geom {
41 // proper logical xor
42 inline bool logical_xor (bool a, bool b) { return (a || b) && !(a && b); }
44 /** Sign function - indicates the sign of a numeric type.  -1 indicates negative, 1 indicates
45  *  positive, and 0 indicates, well, 0.  Mathsy people will know this is basically the derivative
46  *  of abs, except for the fact that it is defined on 0.
47  */
48 template <class T> inline int sgn(const T& x) {return (x < 0 ? -1 : (x > 0 ? 1 : 0) );}
50 template <class T> inline T sqr(const T& x) {return x * x;}
51 template <class T> inline T cube(const T& x) {return x * x * x;}
53 /** Between function - returns true if a number x is within a range. The values delimiting the
54  *  range and the number must have the same type.
55  */
56 template <class T> inline const T& between (const T& min, const T& max, const T& x)
57     { return min < x && max > x; }
59 /** Returns x rounded to the nearest integer.  It is unspecified what happens
60  *  if x is half way between two integers: we may in future use rint/round
61  *  on platforms that have them.
62  */
63 inline double round(double const x) { return std::floor(x + .5); }
65 /** Returns x rounded to the nearest \a places decimal places.
67     Implemented in terms of round, i.e. we make no guarantees as to what happens if x is
68     half way between two rounded numbers.
69     
70     Note: places is the number of decimal places without using scientific (e) notation, not the
71     number of significant figures.  This function may not be suitable for values of x whose
72     magnitude is so far from 1 that one would want to use scientific (e) notation.
74     places may be negative: e.g. places = -2 means rounding to a multiple of .01
75 **/
76 inline double decimal_round(double const x, int const places) {
77     //TODO: possibly implement with modulus instead?
78     double const multiplier = std::pow(10.0, places);
79     return round( x * multiplier ) / multiplier;
80 }
83 void binomial_coefficients(std::vector<size_t>& bc, size_t n);
85 }
87 #endif
89 /*
90   Local Variables:
91   mode:c++
92   c-file-style:"stroustrup"
93   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
94   indent-tabs-mode:nil
95   fill-column:99
96   End:
97 */
98 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :