/usr/include/libcoyotl/realutil.h is in libcoyotl-dev 3.1.0-6ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | //---------------------------------------------------------------------
// Algorithmic Conjurings @ http://www.coyotegulch.com
//
// realutil.h (libcoyotl)
//
// A collection of useful functions for working with numbers.
//---------------------------------------------------------------------
//
// Copyright 1990-2005 Scott Robert Ladd
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.
// 59 Temple Place - Suite 330
// Boston, MA 02111-1307, USA.
//
//-----------------------------------------------------------------------
//
// For more information on this software package, please visit
// Scott's web site, Coyote Gulch Productions, at:
//
// http://www.coyotegulch.com
//
//-----------------------------------------------------------------------
#if !defined(LIBCOYOTL_REALUTIL_H)
#define LIBCOYOTL_REALUTIL_H
#include <cstddef>
#include <cmath>
#include <limits>
namespace libcoyotl
{
//! Round to nearest value
/*!
Rounds a value to nearest integer, rounding to even for
exact fractions of 0.5.
*/
template <typename T>
T round_nearest(T x)
{
T result, fraction, dummy;
fraction = fabs(modf(x,&result));
if (fraction == T(0.0))
return result;
if (fraction == T(0.5))
{
if (modf(result / T(2.0), &dummy) != T(0.0))
{
if (x < T(0.0))
result -= T(1.0);
else
result += T(1.0);
}
}
else
{
if (fraction > T(0.5))
{
if (x < T(0.0))
result -= T(1.0);
else
result += T(1.0);
}
}
return result;
}
// Set number of significant digits in a floating-point value
/*!
Returns the given value rounded for the number of specified decimal
digits of precision.
\param x - Original value to be rounded
\param n - Number of significant digits
\return The value <i>x</i> rounded to <i>n</i> digits of precision
*/
template <typename T>
T sigdig(T x, unsigned short n)
{
T scale_factor, result;
// is asking for no digits, or more digits than in double, simply return x
if ((n == 0) || (n > std::numeric_limits<T>::digits10))
result = x;
else
{
// find a factor of ten such that all significant digits will
// be in the integer part of the double
scale_factor = pow(T(10.0),T((int)n - 1 - (int)floor(log10(fabs(x)))));
// scale up, round, and scale down
result = round_nearest(x * scale_factor) / scale_factor;
}
return result;
}
//! Approximately equality
/*!
Determines if one value is approximately equal to another. This is primarily
intended for floating-point types, where two computed values may not be
precisely equal in terms of encoded floating-point bits, even though "hand"
calculations would produce equal results.
\param a - First operand
\param b - Second operand
\param tolerance - The size of the range used to determine equivalence. By
default, this value is 1. To be considered equal to <i>b</i>, <i>a</i>
must fall in the range (b - (tolerance * ε)) - (b + (tolerance * ε)),
where ε is the minimum possible diffeence between two floating-point
values of the given type.
\return true if <i>a</i> falls in the range of equivalence with <i>b</i>; false otherwise
*/
template <typename T>
bool are_equal(T a, T b, T tolerance = T(1.0))
{
// find the range of tolerance
T adjustment = tolerance * std::numeric_limits<T>::epsilon();
// set high and low bounds on a's value
T low = b - adjustment;
T hi = b + adjustment;
// compare a to range
return ((a >= low) && (a <= hi));
}
// Lowest common multiple
/*!
Calculates the lowest common multiple for two values.
*/
unsigned long lcm(unsigned long x, unsigned long y);
// Greatest common denominator
/*!
Calculates the greatest common denominator for two values.
*/
unsigned long gcd(unsigned long x, unsigned long y);
//! Generic absolute value template function
/*!
Calculates the absolute value for any numeric type.
*/
template <class T> inline T abs_val(T value)
{
return (value < 0 ? (-value) : value);
}
//! Absolute value function, specialization
/*!
Specialization of template function for <i>unsigned long</i> values.
*/
inline unsigned long abs_val(unsigned long value)
{
return value;
}
//! Absolute value function, specialization
/*!
Specialization of template function for <i>unsigned int</i> values.
*/
inline unsigned int abs_val(unsigned int value)
{
return value;
}
//! Absolute value function, specialization
/*!
Specialization of template function for <i>unsigned short</i> values.
*/
inline unsigned short abs_val(unsigned short value)
{
return value;
}
//! Absolute value function, specialization
/*!
Specialization of template function for <i>unsigned char</i> values.
*/
inline unsigned char abs_val(unsigned char value)
{
return value;
}
//! Minimum value function template
/*!
Returns the lesser of two values.
*/
template <class T> inline T min_of(T x1,T x2)
{
return (x1 < x2 ? x1 : x2);
}
//! Maximum value function template
/*!
Returns the greater of two values.
*/
template <class T> inline T max_of(T x1,T x2)
{
return (x1 > x2 ? x1 : x2);
}
//! Hyperbolic arcsine
/*!
Calculates sinh<sup>-1</sup>.
*/
inline double asinh(const double& x)
{
return log(x + sqrt(x * x + 1.0));
}
//! Hyperbolic arccosine
/*!
Calculates cosh<sup>-1</sup>.
*/
inline double acosh(const double& x)
{
return log(x + sqrt(x * x - 1.0));
}
//! Hyperbolic arctangent
/*!
Calculates tanh<sup>-1</sup>.
*/
inline double atanh(const double& x)
{
return log((1.0 + x) / (1.0 - x)) / 2.0;
}
} // end namespace libcoyotl
#endif
|