SUMMARY: cc floating point problems

From: Ernie Bisson, MIT Bates Linear Accelerator <BISSON_at_AESIR.MIT.EDU>
Date: Fri, 29 Sep 1995 12:21:14 -0400 (EDT)

My orginal question,

================================================================================
>I'm having a bizarre problem with floating-point operations in C on an
>AlphaServer 1000 4/200 running Digital-Unix V3.2A.
>
>Here is a simple C program to demonstrate:
>
>#include <stdio.h>
>
>main()
>
>{
>float fff = 4.333;
>float ggg = 1/65535;
>
>printf("ggg = %f\n",ggg);
>printf("1/65535 = %f\n",1/65535);
>printf("fff = %f\n",fff);
>printf("1/65535 = %f\n",1/65535);
>printf("ggg = %f\n",ggg);
>
>}
>
>
>The results produced after building it using the compiler /usr/bin/cc:
>
>ggg = 0.000000
>1/65535 = 0.000000
>fff = 4.333000
>1/65535 = 4.333000
>ggg = 0.000000
>
>I get the same results from V3.0 and V3.2 systems.
>I get Correct results on Ultrix V4.3A and SunOS 4.1.3_U1 systems.
>Translating from the mips binary to AXP, via "mx", also works correctly.
>Does anyone know of any problems that could be causing this?
================================================================================


All replies I received pointed out that both "1" and "65535" were being
interpreted as integers and that I was trying to assign the calculation
of "1/65535" to a floating variable. To have it do a floating divide, at
least one of the values needs to be a floating-point number.

Do either, "1./65535" or cast one of the values to float "(float)1/65535".

It seems that on most machines, at least the others I tried, the result of
"1/65535" is 0.0.

An explanation from "reeves_at_zk3.dec.com" explains why this is not so on the
Alpha.

> The trickier question is why does
>
> printf("1/65535 = %f\n",1/65535);
>
> or the equivalent expression,
>
> printf("1/65535 = %f\n", 0);
>
> simply print the last floating point number printed? The answer is that you
> are passing an integer constant to a floating point format code. There is
> nothing to tell the compiler to convert the constant in this case; therefore
> the results are undefined.
>
> On the Alpha, floating point arguments are passed in different registers
> from integer arguments. This isn't true on the other architectures.
>
> If you want a floating point constant, put a period in the number or add an
> "F" or "E0" suffix.
>
> Bottom line: a broken program may display different broken behavior on
> different implementations, sometimes resembling working behavior.


I think I was so baffled from the bizarre results, that I simply overlooked
the fact that I was dividing 2 integers.

Thanks to all.

--------------------------------------------------------------------------------
Ernie Bisson
M.I.T. Bates Linear Accelerator
P.O. Box 846
Middleton, Massachusetts 01949-2846
(508) 774-2370 or (617) 253-9218

E-Mail : bisson_at_bates.mit.edu
--------------------------------------------------------------------------------
Received on Fri Sep 29 1995 - 17:50:37 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:46 NZDT