Reputation: 4434
As I was trying to compute some very small simple precision and double precision floating numbers I encountered some issues.
Take a look at the following code sample:
public class FloatingLimits {
public static void doSimpleFloatingLimitDemo() {
float firstValue = 1.56F;
float newValue = 1.0F / ((float)Math.pow(2.0D, 150));
double doubleFirst = 2.56;
double doubleNew = 1.0F /Math.pow(2.0D, 150);
double doubleThird = 1.0F/Math.pow(2.0D, 589);
double doubleFourth = 1.0F/Math.pow(2.0, 1589);
System.out.println("float first value =" + firstValue);
System.out.println("float new value =" + newValue);
System.out.println("double first value =" + doubleFirst);
System.out.println("double new value =" + doubleNew);
System.out.println("double third value =" + doubleThird);
System.out.println("double fourth value =" + doubleFourth);
}
public static void main(String[] args) {
doSimpleFloatingLimitDemo();
}
}
It produces the following result:
There is therefore a representation issue or a display issue! Does this have anything to do with numbers precision? The very small numbers that I could not represent with a simple float precision type (32 bits), could be represented with double float precision numbers (64) bits, but the double float also is showing limits. So what would that limit be for very small numbers? Is there a workaround for this using float and double numbers or should I necessarily use BigDecimal to solve it. If I have to use BigDecimals, is there a certain limit to BigDecimal representation as well?
Upvotes: 0
Views: 1024
Reputation: 328745
If you look at Double.MAX_VALUE
A constant holding the largest positive finite value of type double, (2-2-52).21023.
And if you run:
System.out.println(Math.pow(2.0, 1589));
You will see the expected result:
Infinity
Upvotes: 2
Reputation: 26926
If you need to represents decimal numbers with arbitrary precision you have to use BigDecimal
.
Here is a link
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
The internal binary representation of float and double can introduce errors. Another possibility is to work with int
or long
multiply the values by a factor (10, 100, 1000 ...) and treat them as non decimal values.
Upvotes: 1