Watcom C bug?

Hello,
I am a QNX 4.25 user, December 1999 CD. I have a paid support
contract–I don’t know how to indicate that using the QDN as opposed
to QUICs.

On this nifty little C program:

#include <stdio.h>
#include <stdlib.h>

double interval, aa;
int cnt;

main()
{

for(cnt = 0; cnt < 10000; cnt++)
aa = sin(3.1416 * 10.0 / 180.0);

interval = 0.833276;
printf(“Value: %f\n”, interval);
printf(“Value: %f\n”, interval);
exit(EXIT_SUCCESS);
}


the output will be:


Value: 1.000000
Value: 0.833276


This looks like a bug to me. Has it been covered before?


I stumbled on this because I forgot to #include <math.h>, and if I do
so, the bug goes away. It seems that the floating point system is
somehow screwed up doing the sin operation when the system thinks the
sin is integer instead of double.

I would imagine the sin is returning a double in the registers and C
is doing a float convertion (thinking an int was returned) and then
storing the result in aa. I don’t understand how this would effect
the printf.

Thanks,
Greg Laird

Hello,
I am a QNX 4.25 user, December 1999 CD. I have a paid support
contract–I don’t know how to indicate that using the QDN as opposed
to QUICs.

On this nifty little C program:

#include <stdio.h
#include <stdlib.h

double interval, aa;
int cnt;

main()
{

for(cnt = 0; cnt < 10000; cnt++)
aa = sin(3.1416 * 10.0 / 180.0);

interval = 0.833276;
printf(“Value: %f\n”, interval);
printf(“Value: %f\n”, interval);
exit(EXIT_SUCCESS);
}

It’s not a bug per say, saying that WATCOM has a bug is a bold statement '-)
It’s a side effect of sin being handle as returning an int, when in fact
it’s
returning an int.

I use the debugger to understand exactly what happen and the problem is
that the FPU stack “overflows”. FPU don’t have standard register per
say, the registers are handle like a stack. Most instruction like fsin
perform
their operation on last item put on the stack.

Because the compiler thinks sin() returns an int, it doesn’t generate code
to pull data from the FPU stack.

The FPU stack register is 8 register deep. The problem will only show if
you loop at least 7 times…

That’s nasty I agree, but hey that’s why they have the -w9 option :wink:

That one reason why I compile all my C code with the C++ compiler.
No prototype being defined is treated ERROR so you can’t get
bitten by this.

the output will be:


Value: 1.000000
Value: 0.833276


This looks like a bug to me. Has it been covered before?


I stumbled on this because I forgot to #include <math.h>, and if I do
so, the bug goes away. It seems that the floating point system is
somehow screwed up doing the sin operation when the system thinks the
sin is integer instead of double.

I would imagine the sin is returning a double in the registers and C
is doing a float convertion (thinking an int was returned) and then
storing the result in aa. I don’t understand how this would effect
the printf.

Thanks,
Greg Laird

Sorry,
I forgot to say that I am running this program on a Pentium machine,
using the compile & link command:

cc test.c

and running:

a.out


If you need any other info, let me know.

Best to you,
Greg Laird



On Sat, 26 Aug 2000 23:21:35 GMT, glaird@teleport.com (Greg Laird)
wrote:

Hello,
I am a QNX 4.25 user, December 1999 CD. I have a paid support
contract–I don’t know how to indicate that using the QDN as opposed
to QUICs.

On this nifty little C program:

#include <stdio.h
#include <stdlib.h

double interval, aa;
int cnt;

main()
{

for(cnt = 0; cnt < 10000; cnt++)
aa = sin(3.1416 * 10.0 / 180.0);

interval = 0.833276;
printf(“Value: %f\n”, interval);
printf(“Value: %f\n”, interval);
exit(EXIT_SUCCESS);
}


the output will be:


Value: 1.000000
Value: 0.833276


This looks like a bug to me. Has it been covered before?


I stumbled on this because I forgot to #include <math.h>, and if I do
so, the bug goes away. It seems that the floating point system is
somehow screwed up doing the sin operation when the system thinks the
sin is integer instead of double.

I would imagine the sin is returning a double in the registers and C
is doing a float convertion (thinking an int was returned) and then
storing the result in aa. I don’t understand how this would effect
the printf.

Thanks,
Greg Laird