Path: blob/master/3rdparty/openexr/Imath/ImathMath.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////33343536#ifndef INCLUDED_IMATHMATH_H37#define INCLUDED_IMATHMATH_H3839//----------------------------------------------------------------------------40//41// ImathMath.h42//43// This file contains template functions which call the double-44// precision math functions defined in math.h (sin(), sqrt(),45// exp() etc.), with specializations that call the faster46// single-precision versions (sinf(), sqrtf(), expf() etc.)47// when appropriate.48//49// Example:50//51// double x = Math<double>::sqrt (3); // calls ::sqrt(double);52// float y = Math<float>::sqrt (3); // calls ::sqrtf(float);53//54// When would I want to use this?55//56// You may be writing a template which needs to call some function57// defined in math.h, for example to extract a square root, but you58// don't know whether to call the single- or the double-precision59// version of this function (sqrt() or sqrtf()):60//61// template <class T>62// T63// glorp (T x)64// {65// return sqrt (x + 1); // should call ::sqrtf(float)66// } // if x is a float, but we67// // don't know if it is68//69// Using the templates in this file, you can make sure that70// the appropriate version of the math function is called:71//72// template <class T>73// T74// glorp (T x, T y)75// {76// return Math<T>::sqrt (x + 1); // calls ::sqrtf(float) if x77// } // is a float, ::sqrt(double)78// // otherwise79//80//----------------------------------------------------------------------------8182#include "ImathPlatform.h"83#include "ImathLimits.h"84#include <math.h>8586namespace Imath {878889template <class T>90struct Math91{92static T acos (T x) {return ::acos (double(x));}93static T asin (T x) {return ::asin (double(x));}94static T atan (T x) {return ::atan (double(x));}95static T atan2 (T x, T y) {return ::atan2 (double(x), double(y));}96static T cos (T x) {return ::cos (double(x));}97static T sin (T x) {return ::sin (double(x));}98static T tan (T x) {return ::tan (double(x));}99static T cosh (T x) {return ::cosh (double(x));}100static T sinh (T x) {return ::sinh (double(x));}101static T tanh (T x) {return ::tanh (double(x));}102static T exp (T x) {return ::exp (double(x));}103static T log (T x) {return ::log (double(x));}104static T log10 (T x) {return ::log10 (double(x));}105static T modf (T x, T *iptr)106{107double ival;108T rval( ::modf (double(x),&ival));109*iptr = ival;110return rval;111}112static T pow (T x, T y) {return ::pow (double(x), double(y));}113static T sqrt (T x) {return ::sqrt (double(x));}114static T ceil (T x) {return ::ceil (double(x));}115static T fabs (T x) {return ::fabs (double(x));}116static T floor (T x) {return ::floor (double(x));}117static T fmod (T x, T y) {return ::fmod (double(x), double(y));}118static T hypot (T x, T y) {return ::hypot (double(x), double(y));}119};120121122template <>123struct Math<float>124{125static float acos (float x) {return ::acosf (x);}126static float asin (float x) {return ::asinf (x);}127static float atan (float x) {return ::atanf (x);}128static float atan2 (float x, float y) {return ::atan2f (x, y);}129static float cos (float x) {return ::cosf (x);}130static float sin (float x) {return ::sinf (x);}131static float tan (float x) {return ::tanf (x);}132static float cosh (float x) {return ::coshf (x);}133static float sinh (float x) {return ::sinhf (x);}134static float tanh (float x) {return ::tanhf (x);}135static float exp (float x) {return ::expf (x);}136static float log (float x) {return ::logf (x);}137static float log10 (float x) {return ::log10f (x);}138static float modf (float x, float *y) {return ::modff (x, y);}139static float pow (float x, float y) {return ::powf (x, y);}140static float sqrt (float x) {return ::sqrtf (x);}141static float ceil (float x) {return ::ceilf (x);}142static float fabs (float x) {return ::fabsf (x);}143static float floor (float x) {return ::floorf (x);}144static float fmod (float x, float y) {return ::fmodf (x, y);}145#if !defined(_MSC_VER)146static float hypot (float x, float y) {return ::hypotf (x, y);}147#else148static float hypot (float x, float y) {return ::sqrtf(x*x + y*y);}149#endif150};151152153//--------------------------------------------------------------------------154// Don Hatch's version of sin(x)/x, which is accurate for very small x.155// Returns 1 for x == 0.156//--------------------------------------------------------------------------157158template <class T>159inline T160sinx_over_x (T x)161{162if (x * x < limits<T>::epsilon())163return T (1);164else165return Math<T>::sin (x) / x;166}167168169//--------------------------------------------------------------------------170// Compare two numbers and test if they are "approximately equal":171//172// equalWithAbsError (x1, x2, e)173//174// Returns true if x1 is the same as x2 with an absolute error of175// no more than e,176//177// abs (x1 - x2) <= e178//179// equalWithRelError (x1, x2, e)180//181// Returns true if x1 is the same as x2 with an relative error of182// no more than e,183//184// abs (x1 - x2) <= e * x1185//186//--------------------------------------------------------------------------187188template <class T>189inline bool190equalWithAbsError (T x1, T x2, T e)191{192return ((x1 > x2)? x1 - x2: x2 - x1) <= e;193}194195196template <class T>197inline bool198equalWithRelError (T x1, T x2, T e)199{200return ((x1 > x2)? x1 - x2: x2 - x1) <= e * ((x1 > 0)? x1: -x1);201}202203204205} // namespace Imath206207#endif208209210