[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Compiler bugs [ Was: (yet another) Infernal compiler error ]



Quoting Goswin Brederlow (goswin.brederlow@student.uni-tuebingen.de):
> I have a programm that has several strange bugs, depending on options
> and compiler versions used. I'm still trying to sepperate them and
> make small test cases, but I think something is seriously wrong. The
> code looks as follows (with a lot other stuff around).
> 
> double a,b;
> 
> a = angle();
> if (abs(a - b) > 1E-10) {
> 	cout << abs(a-b) << endl;
> }
> 
> The output is something like
> 
> 1.358376E-15
> 
> which isn't greater than 1E-10 by no means. A gets calculated just
> before the if, so I would guess that some pipelining is wrong.
abs is a function, which only works for integers and returns an int.
(man 3 abs). You want to use fabs:

#include <iostream.h>
#include <math.h>

main()
{
 double a,b;

  a = 1e-3;
  if (fabs(a-b) > 1e-10)
     cout << fabs(a-b) << endl;
}

(Don't forget -lm at link time!)
Bye,
Martin.

-- 
esa$ gcc -Wall -o ariane5 ariane5.c
ariane5.c: 666: warning: long float implicitly truncated to unsigned type
esa$ ariane5


Reply to: