Intermediate type of mixed-type expressions

Quote:

>I've got a question about evaluating the intermediate type of an

>expression when using mixed-mode (different types) in the f77 standard.

>By "intermediate type", I mean the type of the expression after it has

>been evaluated, but not yet assigned.

>Given a parse tree representing an expression, I need to be able to

>figure out this intermediate type. For the case where the operands of

>the tree are all one type, the answer is obvious (isn't it?).

>i1+i2*i3**i4 -> integer type.

>r1+r2*r3**r4 -> real type.

>I believe I remember reading somewhere once (I can't recreate the source

>now that I need it) that if a real (or real function) shows up anywhere

>in an expression full of integer types, then the intermediate type is real.

>ie: i1+i2*i3**r4 -> real type. Is this always the case?

Yes, for sufficiently precise definitions of "shows up anywhere".

The standard specifies some rules for implicit type conversion when

the operands of certain arithmetic operations are of different type.

Generally speaking, both operands are converted to the "higher" of the

two types just before the operation. Types are ordered as follows:

INTEGER < REAL < DOUBLE PRECISION; REAL < COMPLEX.

I won't discuss nonstandard types here.

Note that you can't mix COMPLEX with DOUBLE PRECISION.

What happens in your example is that

i3**r4 == INTEGER**REAL -> REAL real(i3)**r4

i2*(i3**r4) == INTEGER*REAL -> REAL real(i2)*(real(i3)**r4)

i1+(i2*(i3**r4)) == INTEGER+REAL -> REAL real(i1)+...

Note that exponentiation follows slightly different rules, in that

r**i does not involve the conversion of the integer i to a real,

although the result of the operation is of the same type as r.

i**r, on the other hand, is evaluated as real(i)**r.

Quote:

>What about when doubleprecision numbers are mixed in?

The corresponding subexpressions are converted to double precision.

But watch out: r1*r2+r1*d3 is not the same as r1*(r2+d3).

The former is evaluated as real(r1)*real(r2)+dble(r1)*dble(d3),

the latter as dble(r1)*(dble(r2)*dble(d3)).

(Assuming REAL r1, r2; DOUBLE PRECISION d3; in the first place.)

Quote:

>And is it legal to have logical types in a mixed-mode expression? If so

>- how should they be handled? Is is possible/legal to mix character

>types into an expression?

Not according to the standard. Some compilers support implicit

conversions between LOGICAL and INTEGER as an extension. I haven't

seen anyone offer implicit conversions between CHARACTER types and

anything else.

Quote:

>For context, assume that these are expressions which may appear in a

>parameter list, and the target type cannot be known (due to seperately

>compiled modules).

You'd better make sure that you know the target type when coding. If

in doubt, perform an explicit type conversion on the final result of

your expression before passing it as an argument:

CALL SUBR (real(expr1), dble(expr2), ...)

as needed. Fortran 90 is at least able to detect type mismatches, if

you take the trouble of specifying explicit interface blocks.

Otherwise, utilities like FTNCHEK exist to do the intercompilation

analysis and detect type mismatches.

Quote:

>Does anybody have a list of rules which I can go by here? Any pointers

>are greatly appreciated.

Most Fortran manuals explain the rules in detail. So do the Fortran standards.

Quote:

>--Rob Neely

>--University of Illinois, Urbana-Champaign

--