0
import math
    if n < 0:
        raise ValueError("n must be >= 0")
    if math.floor(n) != n:
        raise ValueError("n must be exact integer")
    if n+1 == n:  # catch a value like 1e300
        raise OverflowError("n too large")
    result =1;
    factor =2;

    while factor <=n:
        result *= factor
        factor += 1

    return result

This is a factorial function. I don't understand "if n+1 == n:", then it indicates a 'too large integer' exception. Why is it that?

user697911
  • 10,043
  • 25
  • 95
  • 169
  • The `float` type has a precision problem. Python `int` does not. `1e16==1e16+1` is `True` due to the loss of precision, but `10000000000000000==10000000000000001` is True, even though they visually are the same. Python integers are only limited by available memory. Better to check that the user entered an integer and not a float. – Mark Tolonen Dec 14 '16 at 02:02

2 Answers2

0

Python has a limit to the size of a number that it can use. Conceptually, if Python's max number is 10,000 and you enter in the number 10,001, Python will truncate that number to 10,000, thus num == num+1.

Here's a better answer for more details: What is the maximum float in Python?.

Community
  • 1
  • 1
David542
  • 104,438
  • 178
  • 489
  • 842
0

First off, I don't know why this program exists, because python has no limit to the highest integer (https://docs.python.org/3/library/stdtypes.html#typesnumeric). However, as the previous person says, this guards against truncation, with values like "1e300" where the difference between 10^300 and 10^300 + 1 is so small relatively that it ignores small values.

computergorl
  • 133
  • 1
  • 8