4

Consider the following example:

integer::i
i=0
print*,0/i
print*,1/i
end

Newer compilers (gfortran 13.1.0 and ifx 2024.1.2) generate an executable that prints two zeros, while an older compiler that I found (gfortran 7.4.0) generates an executable that prints only one zero and interrupts execution with "SIGFPE: Floating-point exception - erroneous arithmetic operation", although it is not a floating-point operation.

According to the Fortran Interpretation Document (10.1.5.2.2 Integer division): "The result of [integer division] is the integer closest to the mathematical quotient and between zero and the mathematical quotient inclusively." I didn't find any information about handling integer division by zero.

What should be the correct result of executing this code according to the standard? Or is this behavior undefined?

(A similar question was asked in this post about an older version of Fortran. I'm interested in Fortran 2003 or later. This behavior might have changed considering the difference between the compilers mentioned above. Also, there are no replies in this post that answer the question from the author: "How does F77 handle division by zero for INT division?" Additionally, my question is related to the Fortran standard.)

4
  • There is no sensible integer value that can be substituted for integer divide by zero so I think any reasonable compiler ought to generate a hard runtime trap for "integer divide by zero". Terminating execution. It certainly should not be zero for the second case. NB with an optimising compiler the result may be different since the compiler knows both values at compile time. To be sure of seeing actual runtime behaviour you need to hide the denominator value of zero in a string then convert it or read it in. Commented Jun 24 at 14:45
  • @MartinBrown the behavior that I described can be observed for both -O0 (no optimization) and -O3 (high optimization). I think both the GNU and Intel compiler have some reasons to ignore this arithmetic error and just return zero. But I'm interested, whether the Fortran standard defines, what the result of such operation should be. Commented Jun 24 at 14:52
  • @MartinBrown correction: I just found out that Intel compiler also generates a run-time error "integer divide by zero" for -O0 in the second case. But the newer GNU compiler doesn't. Maybe it's a bug in this version. Commented Jun 24 at 14:59
  • Intel Fortran XE 2013 : forrtl : severe (194): Program Execution - integer divide by zero. No results print. Commented Jun 25 at 7:52

1 Answer 1

8

Your code is invalid Fortran, so a Fortran processor can do anything it wants. This includes doing what you expect or deleting your filesystems.

Fortran 2023, page 163

10.1.5.2.4 Evaluation of numeric intrinsic operations

The execution of any numeric operation whose result is not defined by the arithmetic used by the processor is prohibited.

The prohibition is not a numbered constraint. A Fortran processor need not catch or issue an error or warning. The prohibition is on the programmer.

Sign up to request clarification or add additional context in comments.

9 Comments

so, you are saying that read(5,*)i;print*,1/i;end becomes invalid Fortran code depending on the user input? Also, where exactly does the Fortran standard specify that the prohibition you are talking about is on the programmer and not on the compiler?
Yes. The code is invalid for an input value of 0. It is your responsibility to validate the input. Fortran 2023, page 1. Except where stated otherwise, requirements and prohibitions specified by this document apply to programs rather than processors.
@VT, to make things more confusing, while the program read(5,*)i;print*,1/i;end fails to conform with invalid input, the program unit function f(); read(5,*)i;print*,1/i;end does conform.
And see, @VT, this other question and its answers for far more detail on conformance.
@PierU, Consider the statement J = IABS(I) for integer I and J on a twos-complement system. A program using that statement is conforming except for the value I = - HUGE(I) - 1. If I can be assigned that value during execution, then it is the responsibility of the programmer to check I before executing that statement. PS: with gfortran the -pedantic option forces the integers to be symmetric about 0.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.