# Bug? (large integers...)

**URL:** <https://fortran-lang.discourse.group/t/bug-large-integers/7086>\
**Category:** Uncategorized\
**Created:** [December 31, 2023, 9:14pm UTC](https://fortran-lang.discourse.group/t/bug-large-integers/7086 "2023-12-31T21:14:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TIMOTHY989](https://avatars.discourse-cdn.com/v4/letter/t/a9adbd/32.png) [@TIMOTHY989](https://fortran-lang.discourse.group/u/TIMOTHY989)\
**Post date:** [December 31, 2023, 9:14pm UTC](https://fortran-lang.discourse.group/t/bug-large-integers/7086/1 "2023-12-31T21:14:37Z")

</div>

integer :: x

```
x = (2*33333333333333333)

print *, x

```

end program

Calculated a (large) negative value (!) yet use 1 less “3” or one more “3” returns large positive integer…(?)

---

<div class="post-metadata">

**Author:** ![DavidB](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DavidB](https://fortran-lang.discourse.group/u/DavidB)\
**Post date:** [December 31, 2023, 9:34pm UTC](https://fortran-lang.discourse.group/t/bug-large-integers/7086/2 "2023-12-31T21:34:13Z")

</div>

Not a bug. See [Integer overflow](https://en.wikipedia.org/wiki/Integer_overflow).

Fortran integers are signed integers. On most systems the default integer is 32-bit. The maximum value of a 32-bit signed integer is 2147483647.

You can see this with a simpler example.

```auto
$ cat overflow.f90
  integer :: x
  x = 33333333333333333
  write(*,*) x
end

$ gfortran -o overflow.exe -Wall overflow.f90
overflow.f90:2:23:

    2 | x = 33333333333333333
      | 1
Error: Integer too big for its kind at (1). This check can be disabled with the option ‘-fno-range-check’

$ gfortran -o overflow.exe -Wall -fno-range-check overflow.f90

$ ./overflow.exe
  1954764117

```

You need to use an integer of a kind with sufficient range to store your values. Something like

```auto
$ cat nooverflow.f90
  integer, parameter :: k = selected_int_kind(20)
  integer(kind=k) :: x
  x = 33333333333333333_k
  write(*,*) x
end

$ gfortran -o nooverflow.exe -Wall nooverflow.f90

$ ./nooverflow.exe
                        33333333333333333

```

---

<div class="post-metadata">

**Author:** ![rbitr](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rbitr](https://fortran-lang.discourse.group/u/rbitr)\
**Post date:** [December 31, 2023, 9:38pm UTC](https://fortran-lang.discourse.group/t/bug-large-integers/7086/3 "2023-12-31T21:38:17Z")

</div>

Edit: I got beat to the answer. I’ll leave mine because I’m still curious about the error I have.  
Edit 2: I see from the example above I need to do `z = (2*33333333333333333_wp)` which removes the error.

In principle I think you should use [SELECTED\_INT\_KIND (The GNU Fortran Compiler)](https://gcc.gnu.org/onlinedocs/gfortran/SELECTED_005fINT_005fKIND.html) to ensure the integer is big enough to hold the value.

```auto
program ex1

integer :: x
integer(8) :: y
integer,parameter :: wp = selected_int_kind(17)
integer(kind=wp) :: z

x = (2*33333333333333333)
y = (2*33333333333333333)
z = (2*33333333333333333)

print *, wp
print *, x, y, z

end program

```

```auto
% gfortran ex1.f90 -fno-range-check -o ex
% ./ex
           8
  -385439062 66666666666666666 66666666666666666

```

An 8-byte int is required… curiously, if I compile without the `-fno-range-check` I get an error for all three assignments.

```auto
% gfortran ex1.f90 -o ex
ex1.f90:8:25:

    8 | x = (2*33333333333333333)
      | 1
Error: Integer too big for its kind at (1). This check can be disabled with the option ‘-fno-range-check’
ex1.f90:9:25:

    9 | y = (2*33333333333333333)
      | 1
Error: Integer too big for its kind at (1). This check can be disabled with the option ‘-fno-range-check’
ex1.f90:10:25:

   10 | z = (2*33333333333333333)
      | 1
Error: Integer too big for its kind at (1). This check can be disabled with the option ‘-fno-range-check’

```

See also [Doctor Fortran in "It Takes All KINDs" - Doctor Fortran](https://stevelionel.com/drfortran/2017/03/27/doctor-fortran-in-it-takes-all-kinds/)

---

<div class="post-metadata">

**Author:** ![davidpfister](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/davidpfister/32/3949_2.png) [@davidpfister](https://fortran-lang.discourse.group/u/davidpfister)\
**Post date:** [December 31, 2023, 9:57pm UTC](https://fortran-lang.discourse.group/t/bug-large-integers/7086/4 "2023-12-31T21:57:21Z")

</div>

Now if you really need to deal with huge integers there are few libraries for it

- [fmlib](http://dmsmith.lmu.build/)
- [Pcalcfun](https://web.archive.org/web/20230606063617/http://jean-pierre.moreau.pagesperso-orange.fr/Fortran/pcalcfun_f90.txt)

---

<div class="post-metadata">

**Author:** ![DavidB](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DavidB](https://fortran-lang.discourse.group/u/DavidB)\
**Post date:** [December 31, 2023, 10:02pm UTC](https://fortran-lang.discourse.group/t/bug-large-integers/7086/5 "2023-12-31T22:02:03Z")

</div>

Undecorated integer constants have default kind, so 33333333333333333 overflows.

You need to use 33333333333333333\_wp. Same with floating point constants. Common mistake.

---

<div class="post-metadata">

**Author:** ![TIMOTHY989](https://avatars.discourse-cdn.com/v4/letter/t/a9adbd/32.png) [@TIMOTHY989](https://fortran-lang.discourse.group/u/TIMOTHY989)\
**Post date:** [December 31, 2023, 10:03pm UTC](https://fortran-lang.discourse.group/t/bug-large-integers/7086/6 "2023-12-31T22:03:36Z")

</div>

Thanks for the clarification!  
(Ah, ok) yep- it’s been a wee while …FORTRAN 77 , back in 1984 ( ! ) on an ICL mainframe  
Glad 2 see this proj & love the Mandelbrot , BTW all the best Tim

---

<div class="post-metadata">

**Author:** ![TIMOTHY989](https://avatars.discourse-cdn.com/v4/letter/t/a9adbd/32.png) [@TIMOTHY989](https://fortran-lang.discourse.group/u/TIMOTHY989)\
**Post date:** [December 31, 2023, 10:04pm UTC](https://fortran-lang.discourse.group/t/bug-large-integers/7086/7 "2023-12-31T22:04:33Z")

</div>

Thanx! 😀👍

---

<div class="post-metadata">

**Author:** ![JohnCampbell](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@JohnCampbell](https://fortran-lang.discourse.group/u/JohnCampbell)\
**Post date:** [January 1, 2024, 6:45am UTC](https://fortran-lang.discourse.group/t/bug-large-integers/7086/8 "2024-01-01T06:45:28Z")

</div>

It is interesting that with gfortran -fno-range-check, 4-byte integer constants are promoted to 8-byte.

The following adaptation removes the requirement for “-fno-range-check”, while still allowing integer overflow for calculating x

```auto
program ex1

integer,parameter :: wp = selected_int_kind(17)
integer :: x
integer(8) :: y
integer(kind=wp) :: z
integer(wp) :: t = 33333333333333333_wp

y = (2*33333333333333333_wp)
z = (2*t)
! x = (2*33333333333333333_wp)
x = y

print *, wp
print *, x, y, z, t

end program

```

I have always objected to Fortran compilers not being allowed to correctly interpret the integer constant 33333333333333333, which would be read correctly from an input file.
