This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Fold sqrt comparisons against constants
On 16-Mar-2003, Zack Weinberg <zack at codesourcery dot com> wrote:
> This looks good to me, but you should get a second opinion from
> someone who's more up on floating point issues.
>
> As a followup, could you consider working out which of these
> optimizations are only safe under -ffast-math and which can be
> applied generally? It looks to me like your example above is
> always safe, or at least could be made safe by adding NaN checks.
I think it has different rounding behaviour.
You may get situations arising where e.g.
sqrt(4.000000000000001) = 2.0 (because of rounding),
but 4.000000000000001 > 2.0 * 2.0.
For example, the following program prints more As than Bs.
#include <math.h>
#include <stdio.h>
int main() {
double sqr = 4.0;
double rt = 2.0;
double i;
for (i = 1.0; i != 0.0; i /= 2) {
double d = sqr + i;
double sqrt_d = sqrt(d);
printf("sqrt(%.30f) = %.30f\n", d, sqrt_d);
if (sqrt_d <= rt) { printf("A\n"); }
if (d <= rt * rt) { printf("B\n"); }
}
return 0;
}
--
Fergus Henderson <fjh at cs dot mu dot oz dot au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.