www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/02/10/06:45:40

Subject: Re: your mail
To: oandico AT eee DOT upd DOT edu DOT ph (Orlando Andico)
Date: Sat, 10 Feb 1996 12:41:25 +0100 (MET)
Cc: djgpp AT delorie DOT com
From: Christoph Kukulies <kuku AT gilberto DOT physik DOT rwth-aachen DOT de>
Reply-To: Christoph Kukulies <kuku AT gilberto DOT physik DOT rwth-aachen DOT de>

> 
> Ignore the last missive --   =)
> I just discovered the cause.
> 
> Given the following:
> 
> #define X  5
> #define Y  6
> 
> float z;
> 
> z = X / Y * 5.6;

The above is a programming error. Nowhere is written that
C does automatic casting. The correct casting always
has done by the programmer him/herself.

Float constants in expressions are always treated as double in C
and results are double as well.

Defining the result of a float calculation as float may be dangerous
as well since the result may overfloat the range of a float.

> 
> Turbo C++ evaluates X and Y as integers (!) which is what caused my 
> acos() domain errors. However, GCC 2.7.2 (DJGPP 2.0) _correctly_ 
> evaluates the above; both X and Y seem to be treated as floats. "Fixing" 
> the defines like so:
> 
> #define  X 5.0
> 
> or
> 
> #define  X (float)5
> 
> now, which is the ANSI-defined behavior, that of Turbo C, or that of 
> DJGPP? (the evaluate-as-integer problem appeared using both Turbo C 2.0 
> and Turbo C++ 3.0)
> 
> Thanks,
> 
> /----------------------------------------------------------------------------\
> |  Orlando A. Andico                 "I have no concept of time, other than  |
> |  oandico AT eee DOT upd DOT edu DOT ph               it is flying." -- Alanis Morissette  |
> \----------------------------------------------------------------------------/
> 
> 

--Chris Christoph P. U. Kukulies kuku AT gil DOT physik DOT rwth-aachen DOT de

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019