Blender V2.61 - r43446

math_base.c

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