TIL:When n + 1 == n in python?

twitter logo github logo ・1 min read

Today I was looking to python docs for doctest and found following

if n+1 == n:  # catch a value like 1e300
    raise OverflowError("n too large")

Immediately I opened the shell and try it:

>>> from math import exp
>>> n = exp(300)
>>> n
>>> n + 1 == n

TIL n+1 == n! and how to guard against a ridiculously large number

twitter logo DISCUSS (3)
markdown guide

That's a fairly awful way to handle floating point limits. Much lower ranges should be checked if the algorithm is in danger of breaking.


That's an oft-forgotten error when exposing to remote commands as it is too innocent.


If you need numbers that might get that high, maybe pure integer calculations would work.

There's also some arbitrary precision float modules you could use.

Classic DEV Post from Jan 20

My dev life has just started 😎👩‍💻

Hi there all this is my first post on dev.to (I love this platform 😍👍). This is ...

Shaikhul Islam profile image
Software Craftsman

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️