Re: Integers and standard



In article <7jsjk.138647$102.38296@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"James Giles" <jamesgiles@xxxxxxxxxxxxxxxx> writes:
Steven G. Kargl wrote:
...
Note, it does not use signed-int-literal-constant. More importantly
Z'80000000' will be converted to either an INTEGER(8) or INTEGER(16)
(depends on the hardware and I'm using gfortran's kind type parameter
values) value prior to the assignment.

j.f90:2.24:

data i/Z'80000000'/
1
Error: Arithmetic overflow converting INTEGER(16) to INTEGER(4) at (1)

I don't have gfortran installed, so I can't test it, but what does
it do if you overflow an integer expression at run-time? If
it doesn't abort then, why does it cause fatal error at compile-
time? The standard doesn't say what implementations should
do with overflow, but whatever it is should be consistent as
a quality of implementation issue.

Compile time errors are issued if gfortran detects overflow
during constant folding. Special casing BOZ in DATA statements
so that overflow is permitted would be a poor choice IMHO.
Think of it as an aid to (non?)-expert Fortran programmers who
think they know what they are doing.

Consider the following simple demonstration

program testing
integer i
i = huge(1) + 10 ! Whoops, I meant to use a minus sign, here.
print *, i
end

troutmask:kargl[223] ~/../sgk/work/4x/bin/gfortran -c j.f90
j.f90:3.6:

i = huge(1) + 10
1
Error: Arithmetic overflow at (1)

Personally, I would rather get the above error message than to
silently get -2147483639.


The error message in gfortran 4.4.0 (and maybe 4.3.x) has changed
to point to an option to disable range check.

j.f90:3.21:

data i /Z'80000000'/
1
Error: Arithmetic overflow converting INTEGER(16) to INTEGER(4) at (1).
This check can be disabled with the option -fno-range-check.


Do you really want a compiler to automatically instrument the
following code to catch overflow

function add(i,j)
integer add
integer, intent(in) :: i, j
add = i + j
end function add

where add() may be called several hundered trillion times?
If you want this capability, send a bug report requesting
an option similar to -ffpe-trap, which is used with floating
point exceptions.

--
Steve
http://troutmask.apl.washington.edu/~kargl/
.


Quantcast