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.

Answer from Peter Lawrey on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java numbers › infinity in java
Infinity in Java | Baeldung
January 8, 2024 - For int type in Java, the concept of infinity is not covered. We can only store integer numbers that fit in the memory location that we chose. For real numbers, we also have the concept of infinity, either positive or negative.
Discussions

Why don't we have infinity constant for Int? - Support - Kotlin Discussions
Why don't we have infinity constant for Int · Why doesn’t Java/Kotlin support Int.NEGATIVE_INFINITY and Int.POSITIVE_INFINITY? Currently, we need to convert to Double to use infinity numbers in case Int.MIN_VALUE and Int.MAX_VALUE are not accepted · An integer is coded by four bytes. More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
0
September 30, 2022
java - MINValue in an Array and NEGATIVE_INFINITY - Stack Overflow
i'm learning Java and playing around with a Java programming book. I'm up to the point where they teaching me about Arrays. So, i created an Array that takes a non specific amount of Command Line More on stackoverflow.com
🌐 stackoverflow.com
November 8, 2017
math - Java Output is Giving me "Infinity" - Stack Overflow
The output for a Java program I wrote is giving me "Infinity" when I should be getting a number. I'm using floats, and I don't think my numbers are going out of the range of what floats can handle... More on stackoverflow.com
🌐 stackoverflow.com
How do you set variables to "infinity"?
IEEE754 floating point formats have special positive and negative infinity values... Is that what you're talking about? More on reddit.com
🌐 r/learnprogramming
14
10
April 4, 2019
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Double.html
Double (Java Platform SE 7 )
If the argument is 0xfff0000000000000L, the result is negative infinity. If the argument is any value in the range 0x7ff0000000000001L through 0x7fffffffffffffffL or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the result is a NaN. No IEEE 754 floating-point operation provided ...
🌐
Delft Stack
delftstack.com › home › howto › java › java infinity
Infinity in Java | Delft Stack
February 14, 2024 - Double Positive Infinity: Infinity Double Negative Infinity: -Infinity Float Positive Infinity: Infinity Float Negative Infinity: -Infinity · The division by zero method offers a unique and practical way to implement infinity in Java.
🌐
CodeSpeedy
codespeedy.com › home › implementing positive and negative infinity in java
Implementing Positive and Negative Infinity in Java - CodeSpeedy
November 2, 2021 - In the above statements, we have defined a variable ‘negative_inf’ with the type ‘double’ and is initialized with ‘Double.NEGATIVE_INFINITY’ which is an inbuilt java infinite variable.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
Why don't we have infinity constant for Int? - Support - Kotlin Discussions
September 30, 2022 - Why doesn’t Java/Kotlin support Int.NEGATIVE_INFINITY and Int.POSITIVE_INFINITY? Currently, we need to convert to Double to use infinity numbers in case Int.MIN_VALUE and Int.MAX_VALUE are not accepted.
🌐
Oracle
docs.oracle.com › javame › config › cdc › ref-impl › pbp1.1.2 › jsr217 › java › lang › Float.html
java.lang Class Float
If the argument is 0xff800000, the result is negative infinity. If the argument is any value in the range 0x7f800001 through 0x7fffffff or in the range 0xff800001 through 0xffffffff, the result is a NaN. No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values ...
🌐
CodeGym
codegym.cc › java blog › random › infinity in java
Infinity in Java
April 24, 2025 - Remember: division by zero in Java only produces infinity with floating-point types. ... double nan = Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY; // This filter won't remove NaN! if (values.contains(nan)) { values.remove(nan); } // Correct approach values.removeIf(Double::isNaN); ... int largeValue = Integer.MAX_VALUE; int result = largeValue + 1; // Overflows to negative!
Find elsewhere
🌐
GitHub
github.com › appliedtopology › primitive-lib › blob › master › src › edu › stanford › math › primitivelib › utility › Infinity.java
primitive-lib/src/edu/stanford/math/primitivelib/utility/Infinity.java at master · appliedtopology/primitive-lib
* @return the representation of negative infinity · */ · public static double getNegativeInfinity() { · return -getPositiveInfinity(); · } · } · · public static class Int { · /* Most algorithms do not expect infinite values so this constant holds · * a very large value that for all practical purposes can be considered · * as equivalent to infinity. */ · protected static final int POSITIVE_INFINITY = java.lang.Integer.MAX_VALUE; ·
Author   appliedtopology
🌐
Blogger
javahungry.blogspot.com › 2022 › 03 › java-infinity.html
How to assign Infinity in Java | Java Hungry
We can implement the negative infinity in Java using the Float class in Java as shown below in the example:
🌐
CodeGym
codegym.cc › courses › java multithreading › nan, infinity
Course Java Multithreading - Lecture: NaN, Infinity
double inf = Double.POSITIVE_INFINITY; System.out.println(inf); // Infinity System.out.println(inf + 1); // Infinity+1 == Infinity System.out.println(inf + 10); // Infinity+10 == Infinity System.out.println(inf * -1); // Equal to negative infinity Double.NEGATIVE_INFINITY
🌐
MyCleverAI
mycleverai.com › it-questions › is-infinity-in-java-treated-as-an-integer
Is infinity in Java treated as an integer?
" + (positiveInfinity instanceof ... Output: false int test = (int) positiveInfinity; //results in compilation error } } In summary, infinity is not treated as an integer in Java....
🌐
Techwalla
techwalla.com › tech support › how to
How to Use Infinity in Java | Techwalla
February 9, 2017 - Though it is impossible for a computer to literally represent the value of infinity in memory, the Java "double" and "float" data-type reserves two slots in its address range that are understood by the computer to refer to positive and negative infinity. Open your Java Integrated Development ...
🌐
Mccue
javabook.mccue.dev › floating_point_numbers › positive_and_negative_infinity
Positive and Negative Infinity - Modern Java
You can get positive infinity by dividing any positive number by zero. ... You can get negative infinity by dividing any negative number by zero.
🌐
Yorku
eecs.yorku.ca › teaching › docs › java-api › java › lang › Math.html
Math (Java Platform SE 8 )
If the argument is positive zero or negative zero, then the result is negative infinity. If the argument is equal to 10n for integer n, then the result is n.
🌐
Reddit
reddit.com › r/programminglanguages › 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?
r/ProgrammingLanguages on Reddit: 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?
July 26, 2023 -

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.