Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java float is more precise than double?

Code:

class Main {
    public static void main (String[] args) {
        System.out.print("float: ");
        System.out.println(1.35f-0.00026f);
        System.out.print("double: ");
        System.out.println(1.35-0.00026);
    }
}

Output:

float: 1.34974
double: 1.3497400000000002

??? float got the right answer, but double is adding extra stuff from no where, Why??

Isn't double supposed to be more precise than float?

like image 400
Mustafa Avatar asked Oct 18 '13 07:10

Mustafa


People also ask

Are floats more precise than doubles?

double has 2x more precision than float. float is a 32 bit IEEE 754 single precision Floating Point Number – 1 bit for the sign, 8 bits for the exponent, and 23* for the value. float has 7 decimal digits of precision.

What has more precision than a double?

In computing, quadruple precision (or quad precision) is a binary floating point–based computer number format that occupies 16 bytes (128 bits) with precision at least twice the 53-bit double precision.

How precise is double in Java?

A double is a 64 bit IEEE 754 floating-point. Double can provide precision up to 15 to 16 decimal points.

Is double bigger than float 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.


3 Answers

A float is 4 bytes wide, whereas a double is 8 bytes wide.

Check What Every Computer Scientist Should Know About Floating-Point Arithmetic

Surely the double has more precision so it has slightly less rounding error.

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.

On a side note:-

I would suggest if you want the exact decimal values then use java.math.BigDecimal

like image 136
Rahul Tripathi Avatar answered Oct 04 '22 20:10

Rahul Tripathi


If you know something about the rules for converting double values to strings, which are specified by the documentation for Double.toString [Java-API], you know that the program prints the shortest decimal fraction sufficient to distinguish the double value from its nearest neighbor, with at least one digit before and after the decimal point.

Check Joshua Bloch's Java Puzzlers: Traps, Pitfalls, and Corner Cases - Puzzle 2: Time for a Change. He explains this question in that chapter.

like image 26
qiGuar Avatar answered Oct 04 '22 21:10

qiGuar


The reason of this is the mechanism of numbers with floating point calculation. Also when java tries to print a float value, it truncates trailing zeroes. And, you know, double type uses 8 bytes and float uses 4. So the double precision is bigger.

So, when java calculates your float value, it gets something like 1.3497400 and truncates it before output. And when java calculates your double value, it gets more digits and so you get such result.

Try to execute this simple example for better understanding:

public class Test {
    public static void main( String[] args ) {
        float sum = 0;
        for (int i = 1; i <= 10; i++) {
            sum += 0.1;
        }
        System.out.println( sum );
    }
}

public class Test {
    public static void main( String[] args ) {
        double sum = 0;
        for (int i = 1; i <= 10; i++) {
            sum += 0.1;
        }
        System.out.println( sum );
    }
}
like image 23
Mikhail Kopylov Avatar answered Oct 04 '22 20:10

Mikhail Kopylov