[SciPy-user] special.round(10.5) [was: Installing SciPy on Mac OS X Tiger]

Robert Kern rkern at ucsd.edu
Tue Jun 14 16:25:27 CDT 2005


Fernando Perez wrote:
> Robert Kern wrote:
> 
>> Yes, it's been checked in. However, it appears that on your machine, 
>> scipy.special.round(10.5) == 11. Bugger. Can you give me the 
>> repr(scipy.special.round) like this:
>>
>> In [1]: scipy.special.round
>> Out[1]: <ufunc 'round'>
> 
> 
> Yup:
> 
> In [8]: scipy.special.round
> Out[8]: <ufunc 'round'>
> 
> In [9]: scipy.special.round(10.5)
> Out[9]: 11.0
> 
> calderon[~]> uname -a
> Linux calderon.colorado.edu 2.6.10-1.741_FC3 #1 Thu Jan 13 16:38:22 EST 
> 2005 i686 i686 i386 GNU/Linux
> 
> calderon[~]> python -V
> Python 2.3.4
> 
> What should we do here?

Figure out why cephes' round doesn't behave as documented on your machine.

In [1]: scipy.special.round?
Type:           ufunc
String Form:    <ufunc 'round'>
Namespace:      Interactive
Docstring:
     y=Returns the nearest integer to x as a double precision
     floating point result.  If x ends in 0.5 exactly, the
     nearest even integer is chosen.

Can you compile the attached C program and give the results (you might 
need -lm to link)? On my Mac, I get:

[~] kern$ ./testround
10.4 -> 10
10.5 -> 10
10.6 -> 11
11.4 -> 11
11.5 -> 12
11.6 -> 12

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter
-------------- next part --------------
/*							round.c
 *
 *	Round double to nearest or even integer valued double
 *
 *
 *
 * SYNOPSIS:
 *
 * double x, y, round();
 *
 * y = round(x);
 *
 *
 *
 * DESCRIPTION:
 *
 * Returns the nearest integer to x as a double precision
 * floating point result.  If x ends in 0.5 exactly, the
 * nearest even integer is chosen.
 * 
 *
 *
 * ACCURACY:
 *
 * If x is greater than 1/(2*MACHEP), its closest machine
 * representation is already an integer, so rounding does
 * not change it.
 */

/*
Cephes Math Library Release 2.1:  January, 1989
Copyright 1984, 1987, 1989 by Stephen L. Moshier
Direct inquiries to 30 Frost Street, Cambridge, MA 02140
*/

#include <stdio.h>

#define ANSIPROT 1
#ifndef ANSIPROT
double floor();
#else
extern double floor(double);
double round(double);
#endif

double round(x)
double x;
{
double y, r;

/* Largest integer <= x */
y = floor(x);

/* Fractional part */
r = x - y;

/* Round up to nearest. */
if( r > 0.5 )
	goto rndup;

/* Round to even */
if( r == 0.5 )
	{
	r = y - 2.0 * floor( 0.5 * y );
	if( r == 1.0 )
		{
rndup:
		y += 1.0;
		}
	}

/* Else round down. */
return(y);
}

int main(int argc, char **argv)
{
    printf("10.4 -> %d\n", (int)round(10.4));
    printf("10.5 -> %d\n", (int)round(10.5));
    printf("10.6 -> %d\n", (int)round(10.6));
    printf("11.4 -> %d\n", (int)round(11.4));
    printf("11.5 -> %d\n", (int)round(11.5));
    printf("11.6 -> %d\n", (int)round(11.6));
    return 0;
}


More information about the SciPy-user mailing list