| 1 |
|
| 2 |
// Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide. |
| 3 |
// All rights reserved. |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without modification, |
| 6 |
// are permitted provided that the following conditions are met: |
| 7 |
// |
| 8 |
// 1. Redistributions of source code must retain the above copyright notice, |
| 9 |
// this list of conditions, and the following disclaimer. |
| 10 |
// |
| 11 |
// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
// this list of conditions, and the following disclaimer in the documentation |
| 13 |
// and/or other materials provided with the distribution. |
| 14 |
// |
| 15 |
// 3. Neither the names of the copyright holders nor the names of their contributors |
| 16 |
// may be used to endorse or promote products derived from this software without |
| 17 |
// specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 |
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 |
// THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 24 |
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR |
| 26 |
// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS |
| 28 |
// SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES |
| 29 |
// OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE |
| 30 |
// ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES. |
| 31 |
// |
| 32 |
|
| 33 |
#include "xsmath.h" |
| 34 |
#include <math.h> |
| 35 |
#include <float.h> |
| 36 |
#include "xsfloatmath.h" |
| 37 |
|
| 38 |
/*! \namespace XsMath |
| 39 |
\brief Namespace for mathematical constants and operations. |
| 40 |
*/ |
| 41 |
/*! \addtogroup cinterface C Interface |
| 42 |
@{ |
| 43 |
*/ |
| 44 |
|
| 45 |
//! \brief The value e |
| 46 |
const XsReal XsMath_e = 2.7182818284590452353602874713527; |
| 47 |
//! \brief The value pi |
| 48 |
const XsReal XsMath_pi = 3.1415926535897932384626433832795028841971693993751058209749; |
| 49 |
//! \brief A really small value |
| 50 |
const XsReal XsMath_tinyValue = 1.0e-16; |
| 51 |
//! \brief A convincingly large number |
| 52 |
const XsReal XsMath_hugeValue = 1.0e+16; |
| 53 |
|
| 54 |
#ifdef XSENS_SINGLE_PRECISION |
| 55 |
//! \brief A value related to the precision of floating point arithmetic (1.192092895507813e-07) |
| 56 |
const XsReal XsMath_epsilon = 1.192092895507813e-07; |
| 57 |
//! \brief Square root of XsMath_epsilon |
| 58 |
const XsReal XsMath_sqrtEpsilon = 3.452669830012439e-04; |
| 59 |
//! \brief Value that represents the subnormal number in floating point wizardry |
| 60 |
const XsReal XsMath_denormalized = 1e-37; |
| 61 |
//! \brief Square root of XsMath_denormalized |
| 62 |
const XsReal XsMath_sqrtDenormalized = 3.1622776601683793319988935444327e-19; |
| 63 |
#else |
| 64 |
//! \brief A value related to the precision of floating point arithmetic (2.2204460492503131e-016) |
| 65 |
const XsReal XsMath_epsilon = 2.2204460492503131e-016; |
| 66 |
//! \brief Square root of XsMath_epsilon |
| 67 |
const XsReal XsMath_sqrtEpsilon = 1.4901161193847656e-008; |
| 68 |
//! \brief Value that represents the subnormal number in floating point wizardry |
| 69 |
const XsReal XsMath_denormalized = 1e-307; |
| 70 |
//! \brief Square root of XsMath_denormalized |
| 71 |
const XsReal XsMath_sqrtDenormalized = 3.1622776601683793319988935444327e-154; |
| 72 |
#endif |
| 73 |
|
| 74 |
//! \brief Value to convert radians to degrees by multiplication |
| 75 |
const XsReal XsMath_rad2degValue = 57.295779513082320876798154814105; // (180.0/pi) |
| 76 |
//! \brief Value to convert degrees to radians by multiplication |
| 77 |
const XsReal XsMath_deg2radValue = 0.017453292519943295769236907684886; // (pi/180.0) |
| 78 |
|
| 79 |
//! \brief 0 |
| 80 |
const XsReal XsMath_zero = 0.0; |
| 81 |
//! \brief 0.25 |
| 82 |
const XsReal XsMath_pt25 = 0.25; |
| 83 |
//! \brief 0.5 |
| 84 |
const XsReal XsMath_pt5 = 0.5; |
| 85 |
//! \brief -0.5 |
| 86 |
const XsReal XsMath_minusPt5 = -0.5; |
| 87 |
//! \brief 1.0 |
| 88 |
const XsReal XsMath_one = 1.0; |
| 89 |
//! \brief -1.0 |
| 90 |
const XsReal XsMath_minusOne = -1.0; |
| 91 |
//! \brief 2 |
| 92 |
const XsReal XsMath_two = 2.0; |
| 93 |
//! \brief 4 |
| 94 |
const XsReal XsMath_four = 4.0; |
| 95 |
//! \brief -2 |
| 96 |
const XsReal XsMath_minusTwo = -2.0; |
| 97 |
|
| 98 |
//! \brief -pi/2 |
| 99 |
const XsReal XsMath_minusHalfPi = -1.5707963267948966192313216916397514420985846996875529104874; |
| 100 |
//! \brief pi/2 |
| 101 |
const XsReal XsMath_halfPi = 1.5707963267948966192313216916397514420985846996875529104874; |
| 102 |
//! \brief 2*pi |
| 103 |
const XsReal XsMath_twoPi = 6.2831853071795864769252867665590057683943387987502116419498; |
| 104 |
//! \brief sqrt(2) |
| 105 |
const XsReal XsMath_sqrt2 = 1.4142135623730950488016887242097; |
| 106 |
//! \brief sqrt(0.5) |
| 107 |
const XsReal XsMath_sqrtHalf = 0.5*1.4142135623730950488016887242097; |
| 108 |
|
| 109 |
#ifdef XSENS_SINGLE_PRECISION |
| 110 |
//! \brief infinity value |
| 111 |
const XsReal XsMath_infinity = FLT_MAX; |
| 112 |
#else |
| 113 |
//! \brief infinity value |
| 114 |
const XsReal XsMath_infinity = DBL_MAX; |
| 115 |
#endif |
| 116 |
|
| 117 |
/*! \brief Returns asin(\a x) for -1 < x < 1 |
| 118 |
*/ |
| 119 |
XsReal XsMath_asinClamped(XsReal x) |
| 120 |
{ |
| 121 |
if (x <= XsMath_minusOne) |
| 122 |
return XsMath_minusHalfPi; |
| 123 |
|
| 124 |
if (x >= XsMath_one) |
| 125 |
return XsMath_halfPi; |
| 126 |
|
| 127 |
return asin(x); |
| 128 |
} |
| 129 |
|
| 130 |
/*! \brief Convert radians to degrees |
| 131 |
*/ |
| 132 |
XsReal XsMath_rad2deg(XsReal radians) |
| 133 |
{ |
| 134 |
return XsMath_rad2degValue * radians; |
| 135 |
} |
| 136 |
|
| 137 |
/*! \brief Convert degrees to radians |
| 138 |
*/ |
| 139 |
XsReal XsMath_deg2rad(XsReal degrees) |
| 140 |
{ |
| 141 |
return XsMath_deg2radValue * degrees; |
| 142 |
} |
| 143 |
|
| 144 |
/*! \brief Returns \a a to the power of 2 |
| 145 |
*/ |
| 146 |
XsReal XsMath_pow2(XsReal a) |
| 147 |
{ |
| 148 |
return a*a; |
| 149 |
} |
| 150 |
|
| 151 |
/*! \brief Returns \a a to the power of 3 |
| 152 |
*/ |
| 153 |
XsReal XsMath_pow3(XsReal a) |
| 154 |
{ |
| 155 |
return a*a*a; |
| 156 |
} |
| 157 |
|
| 158 |
/*! \brief Returns non-zero if \a x is finite |
| 159 |
*/ |
| 160 |
int XsMath_isFinite(XsReal x) |
| 161 |
{ |
| 162 |
#ifdef _MSC_VER |
| 163 |
switch (_fpclass(x)) |
| 164 |
{ |
| 165 |
case _FPCLASS_SNAN: |
| 166 |
case _FPCLASS_QNAN: |
| 167 |
case _FPCLASS_NINF: |
| 168 |
case _FPCLASS_PINF: |
| 169 |
return 0; |
| 170 |
|
| 171 |
case _FPCLASS_NN: |
| 172 |
case _FPCLASS_ND: |
| 173 |
case _FPCLASS_NZ: |
| 174 |
case _FPCLASS_PZ: |
| 175 |
case _FPCLASS_PD: |
| 176 |
case _FPCLASS_PN: |
| 177 |
return 1; |
| 178 |
default: |
| 179 |
return _finite(x); |
| 180 |
} |
| 181 |
#elif __GNUC__ |
| 182 |
return isfinite(x); |
| 183 |
#elif defined(isfinite) |
| 184 |
return isfinite(x); |
| 185 |
#elif defined(_ADI_COMPILER) |
| 186 |
return !(isnan(x) || isinf(x)); |
| 187 |
#else |
| 188 |
return 1; |
| 189 |
#endif |
| 190 |
} |
| 191 |
|
| 192 |
/*! \brief Returns \a d integer converted from a single precision floating point value |
| 193 |
*/ |
| 194 |
int32_t XsMath_floatToLong(float d) |
| 195 |
{ |
| 196 |
if (d >= 0) |
| 197 |
return (int32_t) floorf(d+0.5f); |
| 198 |
else |
| 199 |
return (int32_t) ceilf(d-0.5f); |
| 200 |
} |
| 201 |
|
| 202 |
/*! \brief Returns \a d integer converted from a single precision floating point value |
| 203 |
*/ |
| 204 |
int64_t XsMath_floatToInt64(float d) |
| 205 |
{ |
| 206 |
if (d >= 0) |
| 207 |
return (int64_t) floorf(d+0.5f); |
| 208 |
else |
| 209 |
return (int64_t) ceilf(d-0.5f); |
| 210 |
} |
| 211 |
|
| 212 |
/*! \brief Returns \a d integer converted from a double precision floating point value |
| 213 |
*/ |
| 214 |
int32_t XsMath_doubleToLong(double d) |
| 215 |
{ |
| 216 |
if (d >= 0) |
| 217 |
return (int32_t) floor(d+0.5); |
| 218 |
else |
| 219 |
return (int32_t) ceil(d-0.5); |
| 220 |
} |
| 221 |
|
| 222 |
/*! \brief Returns \a d integer converted from a double precision floating point value |
| 223 |
*/ |
| 224 |
int64_t XsMath_doubleToInt64(double d) |
| 225 |
{ |
| 226 |
if (d >= 0) |
| 227 |
return (int64_t) floor(d+0.5); |
| 228 |
else |
| 229 |
return (int64_t) ceil(d-0.5); |
| 230 |
} |
| 231 |
|
| 232 |
/*! @} */ |