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
java - MINValue in an Array and NEGATIVE_INFINITY - Stack Overflow
math - Java Output is Giving me "Infinity" - Stack Overflow
How do you set variables to "infinity"?
In the IEEE 754 format, floating point numbers have three parts to them. The sign bit, the exponent, and a mantissa. The first bit is the sign bit which distinguishes positive and negative infinity in this case. The two infinities have all the exponent bits turned on and the mantissa bits zero. The way you can calculate the number is the mantissa multiplied by 2^ exponent. SO essentially they're the highest values possible, with the leading bit distinguishing between positive and negative.
I would do something similar for finding the min
double minVal = Double.POSITIVE_INFINITY;
for (int i = 0; i < array.length; i++) {
if (array[i] < minVal) {
minVal= array[i];
}
}
EGATIVE_INFINITY = -1.0D / 0.0;
From the documentation:
A constant holding the negative infinity of type double. It is equal to the value returned by Double.longBitsToDouble(0xfff0000000000000L).
The computer itself can not represent infinite numbers, because it has finite memory. For example the double type represents its data in 8 bits of memory which is finite, which means that it can store finite number of numbers :). Think of this constant value as holding the smallest possible Double number which can be represented in computer.
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
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?
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.