double supports Infinity
double inf = Double.POSITIVE_INFINITY;
System.out.println(inf + 5);
System.out.println(inf - inf); // same as Double.NaN
System.out.println(inf * -1); // same as Double.NEGATIVE_INFINITY
prints
Infinity
NaN
-Infinity
note: Infinity - Infinity is Not A Number.
double supports Infinity
double inf = Double.POSITIVE_INFINITY;
System.out.println(inf + 5);
System.out.println(inf - inf); // same as Double.NaN
System.out.println(inf * -1); // same as Double.NEGATIVE_INFINITY
prints
Infinity
NaN
-Infinity
note: Infinity - Infinity is Not A Number.
I'm supposing you're using integer math for a reason. If so, you can get a result that's functionally nearly the same as POSITIVE_INFINITY by using the MAX_VALUE field of the Integer class:
Integer myInf = Integer.MAX_VALUE;
(And for NEGATIVE_INFINITY you could use MIN_VALUE.) There will of course be some functional differences, e.g., when comparing myInf to a value that happens to be MAX_VALUE: clearly this number isn't less than myInf. Also, as noted in the comments below, incrementing positive infinity will wrap you back around to negative numbers (and decrementing negative infinity will wrap you back to positive).
There's also a library that actually has fields POSITIVE_INFINITY and NEGATIVE_INFINITY, but they are really just new names for MAX_VALUE and MIN_VALUE.
Why don't we have infinity constant for Int? - Support - Kotlin Discussions
How stupid is the idea of having infinity included in integer type? More over, how to design a integer/floating point system that makes more sense mathematically?
How do you set variables to "infinity"?
math - Java Output is Giving me "Infinity" - Stack Overflow
So in my imagined language, efficiency is not an issue. I decide to use arbitrary precision integers(i.e. big ints). I realize that sometimes you need infinity as a boundary, so I'm curious, how bad is the idea of having positive/negative infinity in integer type?
I know the fact that you have more UBs, like 0*inf doesn't make sense, but it's fundamentally the same as div by 0 problems. And it should be solved the same as div by 0s.
And for floating numbers, we're all plagued by precision problems, so I think it should make sense for any floating number to be encoded by x = (a, b), where it means that: a - b < x < a + b, and as you do floating point arithemetic, b grows and you lose the precision.
In general, is there any efforts on designing a number system for both integer/floating nums that make more sense mathematically, when you don't care about performance?
EDIT: just realized that haskell have both NAN and INF included in the language.
I see in a lot of pseudocode the initialization of variables to "infinity". Do most programming languages have a special constant for an arbitrarily large number? If not, how are these algorithms implemented?
The JLS answers this pretty explicitly.
A floating-point operation that overflows produces a signed infinity.
You are probably dividing by zero somewhere but you could check.
double b = 1.1*Double.MAX_VALUE;
System.out.println(b);
That shows up as infinity.
Please refer to thedayofcondor's answer here: What are the INFINITY constants in Java, really?
When your operations leave the range of float, you DO get infinity as a result, by default. As you said, however, infinity usually begins with a zero division somewhere, or with a cyclic (or too large of a) operation. If you post a snippet of your code i can give you a hint on where it's 'unprotected'.
Operation Result
n ÷ ± Infinity 0
±Infinity × ±Infinity ±Infinity
±nonzero ÷ 0 ±Infinity
Infinity + Infinity Infinity
±0 ÷ ±0 NaN
Infinity - Infinity NaN
±Infinity ÷ ±Infinity NaN
±Infinity × 0 NaN