Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!rochester!pt!b.gp.cs.cmu.edu!ralf
From: ralf@b.gp.cs.cmu.edu (Ralf Brown)
Newsgroups: comp.sys.ibm.pc
Subject: Re: Another TURBO C Bug??
Message-ID: <58@b.gp.cs.cmu.edu>
Date: Sat, 11-Jul-87 12:36:38 EDT
Article-I.D.: b.58
Posted: Sat Jul 11 12:36:38 1987
Date-Received: Sun, 12-Jul-87 16:07:17 EDT
References: <1236@ihlpm.ATT.COM>
Organization: Carnegie-Mellon University, CS/RI
Lines: 52
Keywords: TURBO C floating point problems

In article <1236@ihlpm.ATT.COM> kmh@ihlpm.ATT.COM (Kirk Hoyer) writes:
>
>I have also encountered problems with floating point numbers,
>but mine seems to be different. I have applied the three
>compiler patches that Borland has posted on Compuserve, but the problem
>still persists.
>
>Given the following program:
>[deleted for brevity]
>
>When this program is compiled and executed (I used the tcc compiler),
>the values of f1 and f2 printed in the routine ffunc() are not the same
>as the values printed in the main program.
>
>The problem seems to go away if the declaration in the main program for
>ffunc() is changed as follows:
>
>	float f1, f2, f3, ffunc( float, float );
>	
>Since Borland claims that both declaration styles are supported, it
>seems to me that both should work.

I experimented with variations of the sample program [which I deleted above].
The bug appears to be that Turbo C does not widen the float parameters in the
old-style declaration to doubles.  If the old-style declaration is changed
to declare the parameters as doubles, the program works fine.

When the compiler gets a function without a prototype, it has to assume that
we are using old-style code, in which all parameters are widened just as in
an arithmetic expression, i.e. char -> int, float -> double, etc.  Thus, when
compiling the function call, the float parameters are widened to doubles.

Since these widenings occur in the function calls, the corresponding widening
must be performed on the function definition: char parameters are actually
ints, floats are actually doubles, etc.  I checked old-style char parameters,
and they worked correctly, but the old-style floats don't seem to be widened
to doubles.  Thus the two float parameters in the sample program are actually
accessing the two halves of *one* of the parameters passed in--no wonder you
get garbage values!

The workarounds I see are:
	1. use prototypes and new-style definitions only
	2. manually widen floats to doubles in old-style parameter list 
	   definitions

-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
ARPA:  RALF@B.GP.CS.CMU.EDU               USnail: Ralf Brown
AT&T:  (412) 268-3053 (school)                    Computer Science Department
                                                  Carnegie-Mellon University
DISCLAIMER?  Who ever said I claimed anything?    Pittsburgh, PA 15213
"I do not fear computers.  I fear the lack of them..." -- Isaac Asimov