Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float vs double Math Java

Is there a precision difference between the following (assuming the value of a and b can be represented without loss of precision in a float).

With floats:

float a;
float b;
double result = 1 + a*b;

With doubles:

double a;
double b;
double result = 1 + a*b;
like image 399
Quirion Avatar asked Aug 22 '16 08:08

Quirion


People also ask

Is float better than double in Java?

double is more precise than float. So, if a more precise and accurate result is required use double. Another reason to use double is that if the number is not fitting in the range offered by the float then use double. We should use float if we have memory constraint because it occupies half-space than double.

Are floats and doubles the same Java?

Size: Float is of size 32 bits while double is of size 64 bits. Hence, double can handle much bigger fractional numbers than float. They differ in the allocation of bits for the representation of the number. Both float and double use 1 bit for representing the sign of the number.

Is it better to use double or float?

Double is more precise than float and can store 64 bits, double of the number of bits float can store. Double is more precise and for storing large numbers, we prefer double over float.

What is the difference between floats and doubles?

A float has 7 decimal digits of precision and occupies 32 bits . A double is a 64-bit IEEE 754 double-precision floating-point number. 1 bit for the sign, 11 bits for the exponent, and 52 bits for the value. A double has 15 decimal digits of precision and occupies a total of 64 bits .


2 Answers

Simple example:

float a = 16777217;  // Largest int exactly representable in a float.
float b = 16777217;
System.out.println((double)(1 + a*b));

double c = 16777217;
double d = 16777217;
System.out.println(1 + c*d);

Output (Ideone):

2.81474976710656E14
2.8147501026509E14

So yes, there is a loss of precision using float.

like image 69
Andy Turner Avatar answered Oct 12 '22 00:10

Andy Turner


There is a loss of precision in

float a;
float b;
double result = 1 + a*b;
  • the float representation.
  • the product of a and b which will also be a float. Note: a * b is a float
  • the addition on 1 could result in a loss of precision.

To should that a * b can lose more precision

for (int i = 1; i < 100; i += 2) {
    float a = i;
    float b = 1.0f / i;
    if ((double) a * b != a * b && a * b != 1)
        System.out.println(i + " " + (double) a * b + " " + a * b);
}

prints

41 0.999999962747097 0.99999994
47 0.9999999683350325 0.99999994
55 0.9999999683350325 0.99999994
61 0.9999999441206455 0.99999994
83 0.999999962747097 0.99999994
97 0.999999969266355 0.99999994

note: it could also happen recover precision lost and get the right answer after b has lost precision

for (int i = 1; i < 20; i += 2) {
    float a = i;
    float b = 1.0f / i;
    if (b != 1.0 / i && a * b == 1)
        System.out.println(i + " " + (double) a * b + " " + a * b);
}

prints

3 1.0000000298023224 1.0
5 1.0000000149011612 1.0
7 1.0000000447034836 1.0
9 1.0000000074505806 1.0
11 1.0000000298023224 1.0
13 1.000000037252903 1.0
15 1.0000000521540642 1.0
17 1.0000000037252903 1.0
19 1.0000000074505806 1.0
like image 31
Peter Lawrey Avatar answered Oct 12 '22 00:10

Peter Lawrey