14

I don't understand why are float values different from double values. From the example bellow it appears that float provides different result than double for the same operation:

public class Test {
 public static void main(String[] args) {
 double a = 99999.8d;
 double b = 99999.65d;
 System.out.println(a + b);
 float a2 = 99999.8f;
 float b2 = 99999.65f;
 System.out.println(a2 + b2);
 }
}

Output:

199999.45
199999.44

Can you explain what makes this difference between float and double?

asked Jul 20, 2012 at 14:25
3

5 Answers 5

25

A float is a 32 bit IEEE 754 floating point.

A double is a 64 bit IEEE 754 floating point.

so it is just a matter of precision because neither of the fraction portions .8 and .65 have a terminating binary representation, so there is some rounding error. the double has more precision so it has slightly less rounding error.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

answered Jul 20, 2012 at 14:28
Sign up to request clarification or add additional context in comments.

Comments

14

Can you explain what makes this difference between float and double?

Sure. Imagine you had two decimal types, one with five significant digits, and one with ten.

What value would you use to represent pi for each of those types? In both cases you'd be trying to get as close to a number which you couldn't represent exactly - but you wouldn't end up with the same value, would you?

It's the same for float and double - both are binary floating point types, but double has more precision than float.

answered Jul 20, 2012 at 14:36

1 Comment

+1 A better example; one with 6 significant digits and one with 16 ;)
12
public class Main {
 public static void main(String[] args) {
 float a=12.6664287277627762f;
 double b=12.6664287277627762;
 System.out.println(a);
 System.out.println(b);
 }
}

Output:

12.666429
12.666428727762776

float can handle about 7 decimal places. A double can handle about 16 decimal places.

Saeed Zarinfam
10.3k7 gold badges63 silver badges74 bronze badges
answered Feb 2, 2013 at 9:42

Comments

5

Doubles have twice the precision of floats. Thus they have smaller rounding errors.

A float has (usually) 32 bits, and a double 64 (again usually). Thus floats have rounding errors on more numbers than doubles.

answered Jul 20, 2012 at 14:27

1 Comment

+1 in Java float is always 32-bit and double is always 64-bit.
1

Floats have less precision than doubles.

It's roughly half as much - 23 bits vs 52 for double(Thanks a lot Mr. Skeet!)!

32-bit for floats, 64-bit for doubles. ...Remember that the word "float" has fewer letters than "double", that's a "memory" trick :)

answered Jul 20, 2012 at 14:30

1 Comment

Not "half" - a float only has 23 bits of precision, and double has 52. It's not like the mantissa size is proportional to the overall size.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.