Constant expressions and mathematical functions
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.fortran Profile…
 Up
Constant expressions and mathematical functions         


Author: Arjen Markus
Date: Oct 12, 2006 00:48

Hello,

I just ran into an odd portability problem:

program printpi
real, parameter :: pi = 4.0*atan(1.0)
write(*,*) pi
end program

This simple program fails with the CVF and Intel compiler, but g95
allows
the use of atan() here.

This raises two questions:
- Are functions like atan() not allowed?
- Which functions _are_ allowed?

It seems counter-intuitive, as the expression above has a perfectly
simple
interpretation and can be evaluated at compile time. Though I realise I
may
be naive in these matters ;).

Regards,

Arjen
24 Comments
Re: Constant expressions and mathematical functions         


Author: Brooks Moses
Date: Oct 12, 2006 01:04

Arjen Markus wrote:
> I just ran into an odd portability problem:
>
> program printpi
> real, parameter :: pi = 4.0*atan(1.0)
> write(*,*) pi
> end program
>
> This simple program fails with the CVF and Intel compiler, but g95
> allows
> the use of atan() here.
>
> This raises two questions:
> - Are functions like atan() not allowed?
> - Which functions _are_ allowed?

Nope, functions like atan() are not allowed. At least, not in Fortran
95 -- the only elemental intrinsic functions allowed are the ones that
are of type integer or character (and where each argument is of type
integer or character). No floating-point.
Show full article (1.56Kb)
3 Comments
Re: Constant expressions and mathematical functions         


Author: Arjen Markus
Date: Oct 12, 2006 01:22

Brooks Moses schreef:
>
> Fortran 2003 removes this restriction; this is presumably why g95 allows it.
>
>> It seems counter-intuitive, as the expression above has a perfectly simple
>> interpretation and can be evaluated...
Show full article (1.22Kb)
2 Comments
Re: Constant expressions and mathematical functions         


Author: glen herrmannsfeldt
Date: Oct 12, 2006 02:17

Brooks Moses wrote:

(snip)
> What if you're using a compiler that's running on a i686 machine, but
> cross-compiling an executable that will run on some IBM workstation that
> supports 128-bit reals? Do you use the native i686 version of ATAN(),
> which won't get nearly the precision that the workstation's version of
> ATAN() will get? Or are you required to emulate the target's
> floating-point hardware?

Many compilers do software floating point constant expression
evaluation. This was interesting when the pentium FDIV bug came
out, and a test program using constants showed no bug.

I don't know how many do atan() separate from the run time atan()
routine.

-- glen
2 Comments
Re: Constant expressions and mathematical functions         


Author: FX
Date: Oct 12, 2006 02:24

> Many compilers do software floating point constant expression
> evaluation. This was interesting when the pentium FDIV bug came out,
> and a test program using constants showed no bug.
>
> I don't know how many do atan() separate from the run time atan()
> routine.

I think Brooks didn't say it was impossible, but only stated a reason why
there is more difficult in implementing it than meet most eyes.

I think any compiler that is capable of cross-compiling has to do in a
host-independent way. For example, gfortran uses MPFR for many such
compile-time evaluations of real constants.

--
FX
1 Comment
Re: Constant expressions and mathematical functions         


Author: Jan Vorbrüggen
Date: Oct 12, 2006 02:38

>> Or are you required to emulate the target's floating-point hardware?

Nothing else makes sense, anyway. What about initialization expressions that
don't rely on intrinsics, such as exponentiation? It also isn't restricted
to FP - the target could support integer or character kinds not natively
supported on the host.
> - How does the F2003 standard solve that issue of cross-compilation?

People realised that it was onerous not to be able to use these intrinsics
in these places, and that it was (no longer, perhaps) onerous to support them
in the compiler properly.

I'll betcha that the restriction was already contentious when F90/F95 were
being defined. I'm surprised that CVF didn't support this as an extension.
Steve?

Jan
no comments
Re: Constant expressions and mathematical functions         


Author: robert.corbett
Date: Oct 12, 2006 04:11

Arjen Markus wrote:
> Hello,
>
> I just ran into an odd portability problem:
>
> program printpi
> real, parameter :: pi = 4.0*atan(1.0)
> write(*,*) pi
> end program
>
> This simple program fails with the CVF and Intel compiler, but g95
> allows
> the use of atan() here.
>
> This raises two questions:
> - Are functions like atan() not allowed?
> - Which functions _are_ allowed?
>
> It seems counter-intuitive, as the expression above has a perfectly
> simple ...
Show full article (1.48Kb)
1 Comment
Re: Constant expressions and mathematical functions         


Author: nospam
Date: Oct 12, 2006 08:33

> Also, there used to be a reluctance to evaluate floating-point
> expressions at compile-time because different models of a
> given machine might deliver different results.

Or, more obviously, there is the case of cross-compilation. The target
machine might not even have the same floating-point representation as
the compilation machine. In that case, you'd have had to emulate not
only the math library, but even fundamental floating-point operations.
That would have increased the required size of the compiler even more.
Yes, cross-compilation was a significant issue.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
no comments
Re: Constant expressions and mathematical functions         


Author: nospam
Date: Oct 12, 2006 08:47

Arjen Markus wrote:
> I live a sheltered life, I guess, I have never done any
> cross-compilation,
> as far as I remember. Yes, that does make sense. Although:
> - The cross-compiler might decide this computation should actually
> be done at start-up of the program

That basically doesn't "work". Too many things can depend on
initialization expresssions. If you start saying that initialization
expressions can be put off until start-up of the program, before long
you've basically moved the whole compiler into run-time. You have
basically destroyed the concept of compilation as a separate step. You
have something more like.... I think it is often called just-in-time
compilation or some such thing. While that can be done in principle, and
is done in some imlementations of some languages (Java?), that's *NOT* a
way to keep the target execution environment small. It simply was not an
option in the environments and days in question.
> - How does the F2003 standard solve that issue of cross-compilation?
Show full article (2.01Kb)
no comments
Re: Constant expressions and mathematical functions         


Author: glen herrmannsfeldt
Date: Oct 12, 2006 10:42

Arjen Markus wrote:
> program printpi
> real, parameter :: pi = 4.0*atan(1.0)
> write(*,*) pi
> end program

I used to see, though not for a while now:

DATA PI/0./
IF(PI.EQ.0) PI=4.*ATAN(1.0)

It would then be initialized once (assuming a static
variable). The result would be the same even if it
was initialized each time.

-- glen
no comments

RELATED THREADS
SubjectArticles qty Group
Hypergeometric functions and beta functionssci.math ·
Updating PCI/PCI Express configuration values for PCI/PCI Express bridgemicrosoft.public.development.device.drivers ·
1 2 3