float to integer rounding
Dean Schulze
uunet!asgard.lpl.arizona.edu!schulze
Tue Dec 21 09:42:17 PST 1993
When Zortech implemented IEEE-754 in their Engineering and Science
edition v3.0 they seem to have complied with the ANSI C standard for
implicit conversions and implemented explicit functions to comply with
IEEE-754. Here is an example program with output:
/*
File: roundtest.cpp
Written by: Dean Schulze
Last Modified: 12-21-93
Tests default rounding mode in Zortech C++.
*/
#include <fltpnt.h>
#include <stdio.h>
main()
{
int i,j;
long rounded;
double dbli;
for (j=-5; j<=5; j++)
{
dbli=j/3.0;
i=dbli;
rounded = rndtol(dbli);
printf("\ndbli = %lf rounded = %li i = %i",dbli,rounded,i);
}
return 0;
}
dbli = -1.666667 rounded = -2 i = -1
dbli = -1.333333 rounded = -1 i = -1
dbli = -1.000000 rounded = -1 i = -1
dbli = -0.666667 rounded = -1 i = 0
dbli = -0.333333 rounded = 0 i = 0
dbli = 0.000000 rounded = 0 i = 0
dbli = 0.333333 rounded = 0 i = 0
dbli = 0.666667 rounded = 1 i = 0
dbli = 1.000000 rounded = 1 i = 1
dbli = 1.333333 rounded = 1 i = 1
dbli = 1.666667 rounded = 2 i = 1
The rndtol() function uses the current rounding mode, which defaults
to round to nearest. Zortech also provided a function to round to nearest
long which doesn't rely on (and presumably doesn't change) the current
rounding mode.
Does an implementation comply with IEEE-754 if the rounding mode is
only used for explicit function calls while the C standard is used for
implicit conversions?
Dean Schulze
More information about the Numeric-interest
mailing list