Arithmetic involving integer and real operands

前端 未结 3 1383
一向
一向 2021-01-21 01:54

In Fortran, will arithmetic involving an integer and real operand be always carried out by converting the integer operand to a real value of t

相关标签:
3条回答
  • 2021-01-21 02:21

    Two answers both say something important: that the result is of type real (with the kind parameter of the real in the expression).

    There is something missing though. Yes: the integer is first converted to the real (with kind parameter) of the result. From 7.1.5.2.1

    Except for a value raised to an integer power, if the operands have different types or kind type parameters, the effect is as if each operand that differs in type or kind type parameter from those of the result is converted to the type and kind type parameter of the result before the operation is performed. When a value of type real or complex is raised to an integer power, the integer operand need not be converted.

    So the following are equivalent (as the latter is how this conversion is defined)

    1+1._wp
    real(1,wp)+1._wp
    

    That paragraph also gives you an exception to the question. 5._wp**1 is arithmetic involving an integer operand which need not be converted.

    As a further exception, the above covers only the intrinsic operations (given by Alexander Vogt's answer). One could imagine defining arithmetic operations which are not subject to these rules.

    As an aside, two examples (which are very much implementation-specific, but could still indicate interesting features to look for).

    print*, 16777219+2., REAL(16777219+2)
    print*, (1.000001)**16777219, (1.000001)**REAL(16777219)
    

    In one implementation I looked at, for each line both expressions return different results, but all results are default real.

    For the first, knowing that the result is of type real is not sufficient, but we know that the compiler is not allowed to return the second result if it does not have the same effect as performing the conversion of the integer first. [It's a bad example really, and the interpretation of the arithmetic is also processor-dependent.]

    For the second, converting the integer to real may generate a different result from not.

    0 讨论(0)
  • 2021-01-21 02:26

    This is defined in Ch. 7.1.5.1 "Intrinsic operation classification" of the Fortran 2008 Standard:

    Excerpt from NOTE 7.15:

    Intrinsic operator      Type of     Type of       Type of
    op                        x1          x2        [x1] op x2
    ----------------------------------------------------------
                               I        I, R, Z       I, R, Z
    Binary +, –, *, /, **      R        I, R, Z       R, R, Z
                               Z        I, R, Z       Z, Z, Z
    

    [...]

    Note: The symbols I, R, Z, C, and L stand for the types integer, real, complex, character, and logical, respectively. Where more than one type for x 2 is given, the type of the result of the operation is given in the same relative position in the next column.

    So yes, it is converted to real in your case.

    0 讨论(0)
  • 2021-01-21 02:30

    Yes, with the intrinsic arithmetic operators the result type of an expression involving an integer and real operand is always real of the same kind as the real operand.

    0 讨论(0)
提交回复
热议问题