next up previous contents
Next: Array and sign conversion Up: Implementation Previous: Maintaining appropriate precision

Converting powers

FortranForm incorrectly maintains fractional powers as rationals - these will be truncated by a compiler.

In[4]:= FortranForm[x^(-2/7)]

Out[4]//FortranForm= x**(-2/7)
Using FortranAssign most rational powers are converted to floating point numbers.

In[5]:= FortranAssign[x^(-2/7),AssignIndent->""]

Out[5]//OutputForm= x**(-2.d0/7.d0)
This representation is generally more compact than carrying out the division, which will be handled by a compiler when creating the executable file. Where possible, Mathematica functions are converted to compiled language functions (see section 4.2). An example is the square root function in FORTRAN.

In[6]:= FortranAssign[y x^(-1/2),AssignIndent->""]

Out[6]//OutputForm= y/sqrt(x)
The only exception to the conversion of rational exponents to floats concerns expressions of the form $x^{\pm 1/2}$. Expressions generally translate into more lines of code, but this is outweighed by improvements in performance.

Unlike C, FORTRAN compilers recognise integer powers and use special heuristics to evaluate them more efficiently than floats. Therefore FortranAssign maintains integer powers as integers.

In[7]:= FortranAssign[x^7,AssignIndent->""]

Out[7]//OutputForm= x**7
A strategy for efficient factorisation of powers is implemented in a separate package for peforming optimizations (see section 5 and [29]).



Jorge Romao
5/14/1998