Best answer:
This is typically only an issue with floating point number formats. Most computers use a standard (IEEE 754) to represent floating point numbers. That standard defines bit patterns that stand for "+ infinity" and "- infinity", and also defines what results should be produced when infinities...
show more
Best answer: This is typically only an issue with floating point number formats. Most computers use a standard (IEEE 754) to represent floating point numbers. That standard defines bit patterns that stand for "+ infinity" and "- infinity", and also defines what results should be produced when infinities are used in arithmetic and other operations.
However, these special "infinity" values really are simply encodings for values too large to represent.
That standard also has a different set of bit patterns to represent "Not a Number" or NaN values. An earlier answer has these two ideas (infinity v. NaN) mixed up. These have "quiet" and "signalling" versions. Trying to use a "signalling" NaN as a data value will generate an exception--if those exceptions are enabled. (They usually aren't) Trying to use an infinite value as a data value generally won't.
There also five defined types of exceptions that can be thrown, but implementations may suppress these and most will simply produce a result (infinity or NaN). "Divide by zero" is often trapped as an exception. Python does that, for example, but both Java and all the C++ implementations I've used will produce an infinity value quietly.
For integer data types, there is no infinity. Computers do integer arithmetic as "modular arithmetic" with the modulus being some large power of 2, nearly always 2^(number of bits in the data type).
5 answers
·
2 weeks ago