Intermediate type of mixed-type expressions 
Author Message
 Intermediate type of mixed-type expressions

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?

What about when doubleprecision numbers are mixed in?

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?

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).

Does anybody have a list of rules which I can go by here?  Any pointers
are greatly appreciated.

--Rob Neely
--University of Illinois, Urbana-Champaign



Mon, 07 Oct 1996 04:53:17 GMT  
 Intermediate type of mixed-type expressions


|>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.

The standard says that type conversion is done on an operation-by-operation
basis.  For each operation, if the operands are of different types, the
type and interpretation of the result is given by various tables in the
standard.  For example, here's part of the table for  X1 + X2:

          X2            I2                                      R2
        X1

        I1              I = I1 + I2                     R = REAL(I1) + R2
        R1              R = R1 + REAL(I2)               R = R1 + R2
        D1              D = D1 + DBLE(I2)               D = D1 + DBLE(R2)
        C1              C = C1 + CMPLX(REAL(I2),0.)     C = C1 + CMPLX(R2,0.)

You follow the rules for each operation in the proper order, and end up
with the expression type being the type of the last operation.

|>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.

Not true.

|>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 in standard fortran-77.  Many implementations support use of logicals
in an arithmetic expression by treating them as integer quantities of the
same size.  Mixing arithmetic and character expressions is not allowed
in standard Fortran-77 and is not generally implemented.

|>
|>Does anybody have a list of rules which I can go by here?  Any pointers
|>are greatly appreciated.

The standard is a good place to look - it is quite clear on this.  You may
be able to get by with a vendor's language reference manual; for example
the DEC Fortran Language Reference Manual has a table that gives relative
"rankings" for datatypes; in a mixed-mode operation, the lower-ranked type
is converted to the higher-ranked type.
--


SDT Languages Group               WWW:  http://www.digital.com/info/slionel.html
Digital Equipment Corporation    
110 Spit Brook Road, ZKO2-3/N30
Nashua, NH 03062-2698             "Free advice is worth every cent"



Mon, 07 Oct 1996 22:34:43 GMT  
 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

--



Mon, 07 Oct 1996 23:03:39 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Intermediate expressions and modular types

2. ? Mix type BINARY data file

3. MIN with mixed type arguments

4. Mixing INTEGER & CHARACTER Data Types

5. Mixing INTEGRS& CHARACTER Types

6. DVF: Passing mixed arg types to subroutines?

7. data type question (mix array and mapping?)

8. mixed type assignment in fortran77 corrupts memory elsewhere!

9. Mixed language programming and derived types

10. Mixed type binary streams?

11. Expression conformance and anchored types

12. type expressions

 

 
Powered by phpBB® Forum Software