How to implement infinity in Java?
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.
The best way to use Infinity in java to avoid double type?
You can use Integer.MAX_VALUE
is this example. No need for infinity. After all, the minimum value can't be higher than Integer.MAX_VALUE
.
int min_so_far = Integer.MAX_VALUE;
What are the INFINITY constants in Java, really?
Java floating point is based on the IEEE 754 binary floating point standard Floating Point Standard, the first version of which was issued in about 1985, so it is much older than Java. Given widespread hardware implementation of IEEE 754 by the time Java was being defined, the Java creators had little choice.
Each IEEE 754 floating point number has three components, a sign bit, an exponent, and a mantissa. Simplifying considerably, the magnitude of a normal number is:
mantissa * (2 ** exponent)
where "**" represents power.
The leading bit is the sign bit. In doubles, the next 11 bits are the exponent.
The bit patterns with all the exponent bits on are reserved for infinities and NaNs. All normal numbers have at least one zero bit in the exponent. The two infinities are represented by having all exponent bits on, and all mantissa bits zero. The leading sign bit distinguishes positive and negative infinity.
The choice of all exponent bits one for the special cases is not arbitrary. It is easier to chop off one of the extremes than to deal with a gap in the middle of a range of numbers, especially for hardware implementations. Taking the all bits off exponent for special cases would have prevented encoding zero with the all bits off pattern, and would have given the largest absolute magnitude values, the infinities, the smallest exponent, which would have also made hardware more complicated. The all bits on exponent is definitely the best choice for the infinities.
The two infinities are both used to represent two things, actually infinite results and results that are too large in absolute magnitude to represent in the normal number system, numbers larger than Double.MAX_VALUE or smaller than -Double.MAX_VALUE. 1.0/0.0 is infinite. So is 2*Double.MAX_VALUE.
There are some algorithms that can be simplified, with fewer special cases, by allowing intermediate results to be infinite, in either sense. Doing so also allows e.g. even a line parallel to the y-axis to have a storable gradient that can be used in calculations.
How to implement infinity in Java?
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.
How to represent -infinity in programming
You could define a number as -infinite and, when adding or substracting something from a number, you do first check if the variable was equal to that pseudo-number. If so you just leave it as it was.
But you might find library functions giving you that functionality implemented, e.g Double in Java or std in c++.
Why is the square root of -Infinity +Infinity in Java?
A NaN is returned (under IEEE 754) in order to continue a computation when a truly undefined (intermediate) result has been obtained. An infinity is returned in order to continue a computation after an overflow has occurred.
Thus the behaviour
Math.sqrt(Double.NEGATIVE_INFINITY); // NaN
is specified because it is known (easily and quickly) that an undefined value has been generated; based solely on the sign of the argument.
However evaluation of the expression
Math.pow(Double.NEGATIVE_INFINITY, 0.5); // Infinity
encounters both an overflow AND an invalid operation. However the invalid operation recognition is critically dependent on how accurate the determination of the second argument is. If the second argument is the result of a prior rounding operation, then it may not be exactly 0.5. Thus the less serious determination, recognition of an overflow, is returned in order to avoid critical dependence of the result on the accuracy of the second argument.
Additional details on some of the reasoning behind the IEEE 754 standard, including the reasoning behind returning flag values instead of generating exceptions, is available in
What Every Computer Scientist Should Know About Floating-Point Arithmetic (1991, David Goldberg),
which is Appendix D of
Sun Microsystems Numerical Computation Guide.
Java Generics and Infinity (Comparable)
This doesn't make any sense...
Given that you don't know what K is at that point, (i.e. You're implementing it generically... duh!) you can't specify a min/max bound for it.
in a case where K could be a int, long, string OR object, you couldn't sensibly guess to use
Integer.MIN_VALUE, "" OR NULL.
I guess what you're looking for is a K.MIN_VALUE_OF_EVENTUAL_TYPE but that doesn't exist.
generating a random double between 0 and infinity in java
Try changing this line inf*R.nextDouble();
to Double.MAX_VALUE * R.nextDouble();
If you also need to get Double.POSITIVE_INFINITY
from time to time, then you have to implement an additional if block and randomly return Double.POSITIVE_INFINITY
in some of the cases.
Related Topics
How to Convert .Pfx File to Keystore with Private Key
Tomcat - Maxthreads VS Maxconnections
Jtable Getselectedrow Does Not Return the Selected Row Index
Hashmap Implementation in Java. How Does the Bucket Index Calculation Work
Waiting at Sun.Misc.Unsafe.Park(Native Method)
Print Full Call Stack on Printstacktrace()
How to Declare a 2D String Arraylist
Java Sorting Based on Enum Constants
Why Were Most Java.Util.Date Methods Deprecated
How to Enable Anti Aliasing in Arbitrary Java Apps
Why Is This Java Code in Curly Braces ({}) Outside of a Method
Why Are Java 8 Lambdas Invoked Using Invokedynamic
"Not Allowed to Load Local Resource: File:///C:....Jpg" Java Ee Tomcat
How to Get the Http Status Code Out of a Servletresponse in a Servletfilter
How to Configure Hibernate Config File for SQL Server