question on FE_TOWARDZERO

F

Francois Grieu

Does ISO C insure this program's output is 0 ?

#include <fenv.h>
#include <math.h>
int main(void)
{
#ifdef FE_TOWARDZERO
unsigned long a;
double d;
int j;
fesetround(FE_TOWARDZERO);
a = 0xFFFFFFFF;
d = 0;
for(j=10;--j;)
d = (d+a)*((double)1/65536/65536);
do
if ((unsigned long)floor(d*a)==a)
return 1;
while (--a);
#endif
return 0;
}


As an aside, my <fenv.h> defines FE_TOWARDZERO as an enum,
isn't that a defect (assuming FE_TOWARDZERO is supported)?

François Grieu
 
K

Kevin Bracey

In message <[email protected]>
Francois Grieu said:
Does ISO C insure this program's output is 0 ?

#include <fenv.h>
#include <math.h>
int main(void)
{

You need to put

#pragma STDC FENV_ACCESS ON

either here or before main, otherwise you're not allowed to change the
rounding mode.
#ifdef FE_TOWARDZERO
unsigned long a;
double d;
int j;
fesetround(FE_TOWARDZERO);
a = 0xFFFFFFFF;
d = 0;
for(j=10;--j;)
d = (d+a)*((double)1/65536/65536);
do
if ((unsigned long)floor(d*a)==a)
return 1;
while (--a);
#endif
return 0;
}

Looks like it's got to return 0 to me, yes.
As an aside, my <fenv.h> defines FE_TOWARDZERO as an enum,
isn't that a defect (assuming FE_TOWARDZERO is supported)?

FE_TOWARDZERO has to exist as a macro if the rounding mode is supported, so
that it can be tested for with #ifdef. The macro could expand to an enum
value though:

enum { FE_TONEAREST, FE_TOWARDZERO };
#define FE_TOWARDZERO FE_TOWARDZERO
 
F

Francois Grieu

Kevin Bracey said:
You need to put

#pragma STDC FENV_ACCESS ON

either here or before main, otherwise you're not allowed to change the
rounding mode.

Many thanks; I did not realize this was a requirement, because
it is not one in my usual environements.

Looks like it's got to return 0 to me, yes.

My criptic question bounces with a terse answer.

Of course you guessed what I REALLY wan to know:
is it guaranteed that when FE_TOWARDZERO is in effect,
for any double d in range ]0,1[,
for any unsigned long a in range ]0,0xFFFFFFFF[
floor(d*a)<a holds.

FE_TOWARDZERO has to exist as a macro if the rounding mode is supported
The macro could expand to an enum value
The author of my <fenv.h> missed that requirement. I should report..


François Grieu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,568
Members
47,204
Latest member
abhinav72673

Latest Threads

Top