[comp.unix.microport] 386/ix bug on doubles inside if-statement ?

dirk@altger.UUCP (dirk) (04/25/88)

Does anyboady have an idea why the following program fails ?

It seems as the compiler can not handle functions which return
a double inside an 'if' statement. I've tested it under the
Interactive UNIX Sys. V rel. 3 v. 1.0.3 port to the iAPX386
and Microport's version 2.1. Both failed. Only the Silicon
Valley IN80386 'C' compiler works fine, thus the bug can not
be in the libraries.

A workaround is to use dummy variables inside the if statement.

cu,
dirk :-)

..unido!cosa!dirk
%%
#include <math.h>

main()
{
	double	a, b, bugI, bugII;

	a= -3.1415;
	b= 0.00001;
	printf ("|a|= %lf\n", fabs (a));
	printf ("|b|= %lf\n", fabs (b));
	if (fabs (a) < fabs (b))
		printf ("1. a is lower\n");
	if ((bugI= fabs (a)) < (bugII= fabs (b)))
		printf ("2. a is lower\n");
	printf ("3. %c is lower\n", fabs (a) < fabs (b)?'a':'b');
}	
%%

vrs@littlei.UUCP (vrs) (04/27/88)

Yes, I have seen this problem.  The compiler cannot cope with two functions
returning float/double in the relational expression.  It seems to handle other
kinds of floating point operands better, so when I need a work-around I rewrite

	if (f(x) < g(y)) ...

as

	{ double t = f(x);
	  if (t < g(y)) ... }

Being sure, of course, to make the result more readble than this.  There's a
belief locally that this is fixed in V.3.1 (and later?) cc for the 386.

	Vince

zeeff@b-tech.UUCP (Jon Zeeff) (04/28/88)

In article <672@altger.UUCP> dirk@altger.UUCP (dirk) writes:
>Does anyboady have an idea why the following program fails ?
>
>	if (fabs (a) < fabs (b)) printf("a is lower\n"); 

Evidently most of the Sys V '386 compilers produce incorrect code for this.

Often you get:

	fldl	-40(%ebp)
	fcompp

When you should get:

	fcompl	-40(%ebp)

Perhaps the bug is consistent enough that the first form can always be 
replaced by the second form by some add on program.

Does anyone have gcc working well enough that I can throw out this buggy
compiler?

-- 
Jon Zeeff           		Branch Technology,
uunet!umix!b-tech!zeeff  	zeeff%b-tech.uucp@umix.cc.umich.edu