Blender  V2.59
math_base.c
Go to the documentation of this file.
00001 /*
00002  * $Id: math_base.c 36815 2011-05-22 04:25:31Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: some of this file.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  * */
00027 
00034 #include "BLI_math.h"
00035 
00036 /* WARNING: MSVC compiling hack for double_round() */
00037 #if (defined(WIN32) || defined(WIN64)) && !(defined(FREE_WINDOWS))
00038 
00039 /* from python 3.1 pymath.c */
00040 double copysign(double x, double y)
00041 {
00042         /* use atan2 to distinguish -0. from 0. */
00043         if (y > 0. || (y == 0. && atan2(y, -1.) > 0.)) {
00044                 return fabs(x);
00045         } else {
00046                 return -fabs(x);
00047         }
00048 }
00049 
00050 /* from python 3.1 pymath.c */
00051 double round(double x)
00052 {
00053         double absx, y;
00054         absx = fabs(x);
00055         y = floor(absx);
00056         if (absx - y >= 0.5)
00057                 y += 1.0;
00058         return copysign(y, x);
00059 }
00060 #else /* OpenSuse 11.1 seems to need this. */
00061 double round(double x);
00062 #endif
00063 
00064 
00065 /* from python 3.1 floatobject.c
00066  * ndigits must be between 0 and 21 */
00067 double double_round(double x, int ndigits) {
00068         double pow1, pow2, y, z;
00069         if (ndigits >= 0) {
00070                 pow1 = pow(10.0, (double)ndigits);
00071                 pow2 = 1.0;
00072                 y = (x*pow1)*pow2;
00073                 /* if y overflows, then rounded value is exactly x */
00074                 if (!finite(y))
00075                         return x;
00076         }
00077         else {
00078                 pow1 = pow(10.0, (double)-ndigits);
00079                 pow2 = 1.0; /* unused; silences a gcc compiler warning */
00080                 y = x / pow1;
00081         }
00082 
00083         z = round(y);
00084         if (fabs(y-z) == 0.5)
00085                 /* halfway between two integers; use round-half-even */
00086                 z = 2.0*round(y/2.0);
00087 
00088         if (ndigits >= 0)
00089                 z = (z / pow2) / pow1;
00090         else
00091                 z *= pow1;
00092 
00093         /* if computation resulted in overflow, raise OverflowError */
00094         return z;
00095 }
00096