# Integer overflow

**URL:** <https://fortran-lang.discourse.group/t/integer-overflow/83>\
**Category:** Help\
**Created:** [May 30, 2020, 6:47pm UTC](https://fortran-lang.discourse.group/t/integer-overflow/83 "2020-05-30T18:47:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ELNS](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/elns/32/3338_2.png) [@ELNS](https://fortran-lang.discourse.group/u/ELNS)\
**Post date:** [May 30, 2020, 6:47pm UTC](https://fortran-lang.discourse.group/t/integer-overflow/83/1 "2020-05-30T18:47:28Z")

</div>

Hi all ,  
I have written program below which occurs integer overflow in factorial operation for n \> 31

 ![Integer_overflow](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/12297d405f640eb1f381190c6aaea167ac383f7c.png)  
How can i handle this?

---

<div class="post-metadata">

**Author:** ![lkedward](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lkedward/32/72_2.png) [@lkedward](https://fortran-lang.discourse.group/u/lkedward)\
**Post date:** [May 30, 2020, 7:53pm UTC](https://fortran-lang.discourse.group/t/integer-overflow/83/2 "2020-05-30T19:53:20Z")

</div>

A quick google search says that the maximum 32bit integer value is `2,147,483,647`.  
So you can’t calculate any values larger than this without increasing the size of the integer you’re using.  
I expect that your program is not giving correct answers for factorials above `12!`

One option for a larger integer, is to use the `int64` kind from the `iso_fortran_env` module:

```auto
program main
  use iso_fortran_env, only: int64
  implicit none
  integer(int64) :: n, i, s_1, s_2, s_3, factorial
  ...
end program main

```

According to google, max value for 64bit int is `9,223,372,036,854,775,807`.  
If you don’t need the precision, you could also just use a float-point (`real`) variable for your factorial result.

edit: to use iso\_fortran\_env instead

---

<div class="post-metadata">

**Author:** ![ELNS](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/elns/32/3338_2.png) [@ELNS](https://fortran-lang.discourse.group/u/ELNS)\
**Post date:** [May 30, 2020, 8:12pm UTC](https://fortran-lang.discourse.group/t/integer-overflow/83/3 "2020-05-30T20:12:11Z")

</div>

Thank you very much 🙏

---

<div class="post-metadata">

**Author:** ![pdebuyl](https://avatars.discourse-cdn.com/v4/letter/p/7bcc69/32.png) [@pdebuyl](https://fortran-lang.discourse.group/u/pdebuyl)\
**Post date:** [May 31, 2020, 10:30am UTC](https://fortran-lang.discourse.group/t/integer-overflow/83/4 "2020-05-31T10:30:52Z")

</div>

The intrinsic `huge` returns the maximum value in a given variable type.

```
print *, huge(factorial)

```

prints the maximum value that can be stored in a default integer variable as `factorial`.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [May 31, 2020, 10:59am UTC](https://fortran-lang.discourse.group/t/integer-overflow/83/5 "2020-05-31T10:59:41Z")

</div>

@ELNS,  
if you come from Python, you can be disappointed because in Python you can use arbitrarily long integers:

```python
>>> 44568668*345346344747*57457455585*353523463473474585*66347474735583
20743094674690460723155872026108386637422588202843490781776300L

```

But in Fortran, you will need a specific library to do such things (there is surely one, but don’t ask me…)  
Fortran uses the hardware to compute fast. Python can go beyond but at the expense of the speed.
