This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/34261] New: Directed rounding doesn't work on MacOS X
- From: "j dot d dot pryce at ntlworld dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 28 Nov 2007 12:07:45 -0000
- Subject: [Bug c/34261] New: Directed rounding doesn't work on MacOS X
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
I research in interval arithmetic. I bought a MacBook Pro in Spring 07 (
MacBookPro2,2; Intel Core 2 Duo 2.33 GHz; Mac OS X 10.4.11 (8S2167), Darwin
8.11.1).
Within MATLAB (Version 7.4.0.287 (R2007a) Jan 2007) I wanted to use Rump's
interval library INTLAB. On startup, INTLAB detected the needed changing of
rounding-mode wasn't working, and I couldn't fix it.
So I tried a simple C program to check this with gcc as follows
/*--------roundtest.cpp--------*/
#include <iostream>
#include <fenv.h>
using namespace std;
int main(int argc, char* argv[]) {
int flag;
double x, y;
double z0, z1, z2, z3;
x = atof(argv[1]);
y = atof(argv[2]);
flag = fesetround(0); // round toward 0
fprintf(stdout, "fesetround(0) returned %i\n", flag);
fprintf(stdout, "Then fegetround() returned %i\n", fegetround());
z0 = x/y;
flag = fesetround(1); // round to nearest
fprintf(stdout, "fesetround(1) returned %i\n", flag);
fprintf(stdout, "Then fegetround() returned %i\n", fegetround());
z1 = x/y;
flag = fesetround(2); // round toward +oo
fprintf(stdout, "fesetround(2) returned %i\n", flag);
fprintf(stdout, "Then fegetround() returned %i\n", fegetround());
z2 = x/y;
flag = fesetround(3); // round toward -oo
fprintf(stdout, "fesetround(3) returned %i\n", flag);
fprintf(stdout, "Then fegetround() returned %i\n", fegetround());
z3 = x/y;
fesetround(1); // round to nearest
fprintf(stdout, "z0=%20.18f, z1=%20.18f, z2-z1=%g, z1-z3=%g\n",
z0, z1, z2-z1, z1-z3);
return 0;
}
/*--------------*/
I compiled with this (I am a C++ beginner, for some reason "gcc" compiles but
can't link):
johnpryce 49$ g++ -frounding-math -Wall roundtest.cpp -o roundtest
Then I ran with this, for instance
johnpryce 54$roundtest 1 3
fesetround(0) returned 0
Then fegetround() returned 0
fesetround(1) returned 1
Then fegetround() returned 0
fesetround(2) returned 1
Then fegetround() returned 0
fesetround(3) returned 1
Then fegetround() returned 0
z0=0.333333333333333315, z1=0.333333333333333315, z2-z1=0, z1-z3=0
With correct rounding, z2 and z3 MUST be different, and z1 equals one of them.
But the fesetround/fegetround output seems to show
(a) Rounding mode can't be changed
(b) It is always toward zero (i.e. oldfashioned IBM360 style chopping) if this
output is to be believed. This violates the IEEE754 standard, which of course
mandates rounding to nearest as the default.
Actually I think rounding to nearest is what DOES happen - big number-crunching
calculations seem too accurate to be consistent with chopping - so the
fesetround/fegetround functions must be wrong.
--
Summary: Directed rounding doesn't work on MacOS X
Product: gcc
Version: 4.0.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: j dot d dot pryce at ntlworld dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34261