Small error in range check?
Manfred Schwarb
manfred99@gmx.ch
Thu Dec 12 15:07:00 GMT 2013
Am 12.12.2013 15:56, schrieb Rimvydas Jasinskas:
> This shouldn't rely on -HUGE(var).
>
> $cat foo.f90
> program foo
> implicit none
> integer(kind=1) :: bar=-127_1-1_1
> write(*,*) bar
> endprogram foo
> $ gfortran -O3 -Wall -Wextra foo.f90 && ./a.out
> -128
>
> Definitely problem with range check for given variable.
> -HUGE(var)-1 depends on platform choice over integer bit field implementation.
> Something like HUGE(var, [sign](var)) would help a lot.
>
>
No, everything is fine.
Fortran as per standards has symmetrical integer ranges. Period.
To use the full integer ranges, you have to use -fno-range-check
Cheers,
Manfred
>
> On Thu, Dec 12, 2013 at 4:35 PM, Anton Shterenlikht <mexas@bris.ac.uk> wrote:
>>> Date: Thu, 12 Dec 2013 15:20:40 +0100
>>> Subject: Small error in range check?
>>> From: Arjen Markus <arjen.markus895@gmail.com>
>>> To: Fortran List <fortran@gcc.gnu.org>
>>>
>>> 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.
>>
>> neither does 4.9:
>>
>> gfortran49 z.f90
>> z.f90:7.19:
>>
>> var = -128_byte
>> 1
>> Error: Integer too big for its kind at (1). This check can be disabled with the option -fno-range-check
>>
>> Neither should it, I think.
>> I think -HUGE() is always guaranteed to work,
>> however, -HUGE()-1 might or might not work,
>> depending on the platform. Am I wrong?
>>
>> Anton
>>
>
More information about the Fortran
mailing list