This is the mail archive of the
mailing list for the GCC project.
Re: Relying on precise integer calculation with double
- From: Roger Sayle <roger at eyesopen dot com>
- To: Daniel Bratell <bratell at lysator dot liu dot se>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 6 Apr 2006 07:41:27 -0600 (MDT)
- Subject: Re: Relying on precise integer calculation with double
On Thu, 6 Apr 2006, Daniel Bratell wrote:
> that it would be safe to use that flag unless I relied on extreme
> precision or behaviour related to NaN or +-Inf, and that normal simple
> arithmetics should still give the same result.
Unfortunately, with -ffast-math simple arithemtics no longer have
precisely the same result. Converting the division into multiplication
by reciprocal sometimes results in numbers that can't precisely be
represented such as 1/7. Of course, in this case you are relying on
"extreme precision" as you describe it above. As pointed out by
Andrew the result is 0.99999999999999994448884876874217298 which
should be close enough to one for most -ffast-math applications.
The real problem is the source line:
> if(diff_days/7.0 != (int)(diff_days/7.0))
Here you're using C's default FP->int conversion with is truncation
towards zero. The fact that 0.99999999 is less than 1 means the result
is zero. Instead, you should probably use "rint" or "round" or "lround"
to return the nearest integer.