This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: How to determine that 2.0 == (float) 2 in a front-end.
- From: Tim Josling <tej at melbpc dot org dot au>
- To: gcc at gcc dot gnu dot org
- Date: Fri, 28 Jun 2002 06:31:13 +1000
- Subject: Re: How to determine that 2.0 == (float) 2 in a front-end.
- Organization: Melbourne PC User Group
- References: <1025186349.2842.ezmlm@gcc.gnu.org>
> Robert Dewar wrote:
>
> > I wrote:
>
> >> Obviously, because X**Y, with Y real, is implemented by computing
> >> EXP(Y*LOG(X)) one doesn't want to change 2.0 to 2 lightly (i.e.,
> >> without the assurance of -funsafe-math_optimizations).
> >
> > Gosh I hope not! computing x**y that way is horribly inaccurate even with
> > perfect log and exp functions.
>
> No, that was a brain-fart - thanks for alerting me to it. g77 generates
> a call to the `pow' function in the C library, thereby shifting the
> blame.
>
> I initially thought I had seen this in libf2c, but
> gcc/libf2c/libF77/Version.c doesn't mention a switch from exp(y*log(x))
> to pow(x,y) in its "ChangeLog", so it must have been there from 1980
> onwards.
>
> Sorry, I don't know where I got the idea from ...
>
> --
> Toon Moene - mailto:toon@moene.indiv.nluug.nl - phoneto: +31 346 214290
I have come across situations where for example 2.0 ** N where N > 240 was
calculated by successive multiplications. This sort of calculation is common
in net present value calculations, where you use binary search of Newton's
method to zero in on the correct interest rate given a set of payments. This
is terribly slow.
That is, calculating the exact value may be more accurate but slower. If you
are doing a calculation as above and suddenly the power you are using happens
to be an exact integer then you can end up with a discontinuity in the output
of the expression which can be very confusing.
So I would agree don't change n.0 to n lightly.
Tim Josling