# [J3] function references and optimization

Malcolm Cohen malcolm at nag-j.co.jp
Tue Oct 9 20:26:14 EDT 2018

```Robert Corbett writes:

<<<

Suppose
a program contains the statement
function

f(x) = y

and the assignment statement

x = y - f(1.0)

Does the mathematical equivalence
rule allow the expression to be
replaced with 0.0?

>>>

Yes.

No matter what kind of function F is, if the processor “knows” that the value of the function is y, it need not evaluate the function reference at all.

This reduces the statement to the well-known

x = y – y

and by the mathematical equivalence rule, y-y is zero so no need to evaluate Y either, giving us x = zero.

It is absolutely standard-conforming to do mathematically equivalent transformations.

Bob continues:

> A better example would be the
>statement function
>
>f(x) = x - 1.0
>
>and the assignment statement
>
>y = f(z) + 1.0

I don’t really see why the expression “x – 1 + 1” is particularly interesting.  Without parentheses (which it is – function reference syntax is not expression parentheses) it looks highly likely to be turned into “x”.  Even if you put parentheses around the subtraction, mathematical equivalence allows computation of the whole thing in a higher precision which will likely remove or at least change the rounding error from the subtraction.  If you want to force rounding to a specific precision, there are many ways which work, and some which are even guaranteed by the standard to work!

Cheers,

--

..............Malcolm Cohen, NAG Oxford/Tokyo.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20181010/a480ba1a/attachment.html>
```