On Fri, Jan 26, 2024 at 03:44:26PM +0200, Elias Toivanen wrote:
> 
> I stumbled upon a Fortran quirk regarding the ranges for integers.
> The standard stipulates that the ranges be symmetric even though
> as we know two's complement signed integers have asymmetrical
> ranges, e.g. int8 kind can vary between -128 and 127.

This is not quite true.  The model number for integer is symmetric,
but the machine representable does not need to be symmetric.

> 
> This is sure to cause confusion as for example the following
> program does not compile:
> 
> ```
> program main
>   use iso_fortran_env
>   implicit none
>   print *, -128_int8

This is a unary minus operator with an operand of 128.
gfortran does not have negative integers.

> end program
> ```
> 
> GFortran 13.2.0 provides the error message
> 
> Error: Integer too big for its kind at (1). This check can
> be disabled with the option '-fno-range-check'

which gives you this error because 128 is greater 127.

It is a little known fact, but you can force a symmetric
range with the -pedantic option.

-- 
Steve

Reply via email to