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

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?
As a mathematician: Infinity is not an integer. The nice thing when you don't have infinity as a term is that you can define numbers inductively, and you lose that when including infinity. On the other hand, you could implement general ordinal numbers, that could be fun. But I would have an extra type, I think. To floats: You could have arbitrary precision floats/big decimals, or rationals. Or even symbolic execution, but that is probably a bit too much, you're building a CAS at that point. More on reddit.com
🌐 r/ProgrammingLanguages
65
26
July 26, 2023
Why don't we have infinity constant for Int? - Support - Kotlin Discussions
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. More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
0
September 30, 2022
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
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
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Double.html
Double (Java Platform SE 7 )
If the argument is negative infinity, the result is 0xfff0000000000000L. If the argument is NaN, the result is 0x7ff8000000000000L. In all cases, the result is a long integer that, when given to the longBitsToDouble(long) method, will produce a floating-point value the same as the argument ...
🌐
CodeGym
codegym.cc › java blog › random › infinity in java
Infinity in Java
April 24, 2025 - Integers, like int and long, don't get the infinity treatment. Nope, when those guys hit their limit, they just overflow and loop back around, which can seriously mess things up. Picture this: you're coding a game, tracking a player's score, ...
🌐
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 negative infinity, the result is 0xff800000. If the argument is NaN, the result is the integer representing the actual NaN value.
🌐
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.

🌐
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 · This concise demonstration underscores how division by zero can be leveraged in Java to succinctly represent infinity, offering a straightforward approach for handling unbounded values in specific mathematical contexts.
🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
CodeGym
codegym.cc › courses › java multithreading › nan, infinity
Course Java Multithreading - Lecture: NaN, Infinity
"In Java, the double type has special values for positive infinity and negative infinity. A positive number divided by 0.0 yields positive infinity, and a negative number — negative infinity.
🌐
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:
🌐
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 ...
🌐
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.
🌐
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....
🌐
Iditect
iditect.com › program-example › integer--how-to-implement-infinity-in-java.html
integer - How to implement infinity in Java?
Description: Integer arithmetic in Java doesn't inherently support infinity. You can use special integer values to represent infinity, such as Integer.MAX_VALUE for positive infinity and Integer.MIN_VALUE for negative infinity.
🌐
Blogger
javarevisited.blogspot.com › 2013 › 01 › how-to-check-if-number-is-positive-or-negative-java-example.html
How to check if a Number is Positive or Negative in Java? Solution Example
This is also a tricky Java question, another tricky point is considering special cases like positive infinity, negative infinity, or NaN in the case of checking floating-point numbers. Things get more complicated when, as follow-up questions, the interviewer puts additional conditions such as you can not use relational operators, etc. Nevertheless, Java provides several ways to check if a number whether integer or floating-point is positive or negative.