[comp.lang.fortran] Confused about optimizing 1-COS

fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts) (09/23/88)

In article <4032@lanl.gov> jlg@lanl.gov (Jim Giles) writes:
>From article <1033@amelia.nas.nasa.gov>, by fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts):
>> This is true, but only accurate when the expression evaluates to a
>> constant.  You couldn't (because of finite precision) replace
>> 1-COS(X)*COS(X) with SIN(x)*SIN(x) and guarentee the same results.
>
>True, but Fortran would allow this anyway since optimized expressions
>need only to be mathematically identical, not computationally identical
>(see section 6.6 of the standard).
> 

Hmm.  Sounds like this is not in the same spirit as the fortran
requirement that (A-B)-C be treated differently than A-(B-C) because
they would produce different numerical results.  I guess I haven't
been bitten by it yet, but it looks like a smallish inconsistency in
the logic behind the standard.

Thanks for pointing it out.

Marty
+-+-+-+     I don't know who I am, why should you?     +-+-+-+
   |        fouts@lemming.nas.nasa.gov                    |
   |        ...!ames!orville!fouts                        |
   |        Never attribute to malice what can be         |
+-+-+-+     explained by incompetence.                 +-+-+-+

jlg@lanl.gov (Jim Giles) (09/24/88)

From article <1041@amelia.nas.nasa.gov>, by fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts):
> Hmm.  Sounds like this is not in the same spirit as the fortran
> requirement that (A-B)-C be treated differently than A-(B-C) because
> they would produce different numerical results.  [...]

Subtract isn't associative!  (A-B)-C is _mathematically_ different from
A-(B-C).  In any case, Fortran is not allowed to optimize across
parenthesis.  Fortran _is_ allowed to optimize expressions to any
_mathematically_ identical result (so long as it doesn't optimize
across parenthesis).  So, 1-COS(x)*COS(x) may be optimized to SIN(x)**2.
But, 1-(COS(x)*COS(x)) may only be optimized to 1-(COS(x)**2).

Meanwhile, A-B-C _must_ be implemented as (A-B)-C.  Read chapter 6 of
the standard: it's all in there!

J. Giles
Los Alamos

fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts) (09/24/88)

In article <4090@lanl.gov> jlg@lanl.gov (Jim Giles) writes:
>From article <1041@amelia.nas.nasa.gov>, by fouts@lemming.nas.nasa.gov.nas.nasa.gov (Marty Fouts):
>> Hmm.  Sounds like this is not in the same spirit as the fortran
>> requirement that (A-B)-C be treated differently than A-(B-C) because
>> they would produce different numerical results.  [...]
>
>Subtract isn't associative!  (A-B)-C is _mathematically_ different from
>A-(B-C).  In any case, Fortran is not allowed to optimize across
>parenthesis.  Fortran _is_ allowed to optimize expressions to any
>_mathematically_ identical result (so long as it doesn't optimize
>across parenthesis).  So, 1-COS(x)*COS(x) may be optimized to SIN(x)**2.
>But, 1-(COS(x)*COS(x)) may only be optimized to 1-(COS(x)**2).
>

Ignore the fact that at 1 AM i hit the - key instead of the + key and
think about what I was trying to say.

>Meanwhile, A-B-C _must_ be implemented as (A-B)-C.  Read chapter 6 of
>the standard: it's all in there!
>

I didn't say it wasn't there.  What I was trying to say was that
spirit of respecting parenthesis seems to be different than the spirit
of replacing expressions involving functions with algebraicly
equivalent expressions, and I got fouled up with a bad example.

Care to comment on my content rather than my form?

>J. Giles
>Los Alamos


+-+-+-+     I don't know who I am, why should you?     +-+-+-+
   |        fouts@lemming.nas.nasa.gov                    |
   |        ...!ames!orville!fouts                        |
   |        Never attribute to malice what can be         |
+-+-+-+     explained by incompetence.                 +-+-+-+