This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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.

Roger
--


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]