[PATCH] Fix overflowing integer to an integer exponent

Steve Kargl sgk@troutmask.apl.washington.edu
Fri Mar 1 22:37:00 GMT 2019


Consider, the program

   program foo
     i = 10**23
     x = 6.02 * 10**23
     print *, i, x
   end

% gfc8 -o z a.f90 && ./z
  -159383552   6.01999981E+23

Now, if 10**23 = -159383552, then 6.02 * 10**23 should be -959488983.04.  
Moreover raising a positive number to a positive exponent should result
in a positive number.  Yes, I'm aware of twos-complement wrap around.
And, finally, the code compiles without so much as a warning.

The attached patch brings some consistency to gfortran.

% gfcx -c a.f90 
a.f90:1:9:

    1 |    i = 10**23
      |         1
Error: Result of exponentiation at (1) exceeds the range of INTEGER(4)
a.f90:2:16:

    2 |    x = 6.02 * 10**23
      |                1
Error: Result of exponentiation at (1) exceeds the range of INTEGER(4)

It has been regression tested on x86_64-*-freebsd.  OK to commit?

2019-03-01  Steven G. Kargl  <kargl@gcc.gnu.org>

	* arith.c (arith_power): Rework overflow of an integer to an integer
	exponent.

2019-03-01  Steven G. Kargl  <kargl@gcc.gnu.org>

	* gfortran.dg/integer_exponentiation_4.f90: Update test.
	* gfortran.dg/integer_exponentiation_5.F90: Ditto.
	* gfortran.dg/no_range_check_1.f90: Ditto.

-- 
Steve
-------------- next part --------------
A non-text attachment was scrubbed...
Name: power.diff
Type: text/x-diff
Size: 4289 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20190301/9c34f439/attachment.bin>


More information about the Fortran mailing list