Small error in range check?

Steve Kargl sgk@troutmask.apl.washington.edu
Thu Dec 12 14:32:00 GMT 2013


On Thu, Dec 12, 2013 at 03:20:40PM +0100, Arjen Markus wrote:
> Hello,
> 
> I ran into a slight problem regarding one-byte integers. The program
> below illustrates it:
> 
> ! chkbyte.f90 --
> !     Check the range for an integer of one byte
> !     This ought to be from -128 to +127
> !
> program chkbyte
>     implicit none
> 
>     integer, parameter :: byte = 1
>     integer(kind=byte) :: var
> 
>     var = -128_byte
> 
>     write(*,*) var
> end program chkbyte
> 
> The gfortran compiler (I tried with 4.5, 4.7 and 4.8) does not like
> the value of -128 for such integers.
> As far as I know the range for such integers is -128 to 127 and if I
> turn off the range checking, the variable is indeed set to -128.
> 
> Shall I create a bug report for this one? I did not find anything
> similar in the bug list.
> 

No.  You need to use 'var = -huge() - 1'.

var = - 128_byte is using an unary minus operator on the 
integer-literal-constant 128_byte.

-- 
Steve



More information about the Fortran mailing list