This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the difference between 1.0 and 1.1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the difference between 1.0 and 1.1" Watch "what is the difference between 1.0 and 1.1" New topic
Author

what is the difference between 1.0 and 1.1

James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
given this code:

double num1=1.0;
double num2=1.1;
System.out.println(num1==1.0f);
System.out.println(num2==1.1f);

It prints out "true" and "false"
why? Looks like a fraction makes a difference. How?
Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
A double has higher precision than a float. The number 1 can be represented perfectly in binary (in just 1 bit). So there is no problem with comparing it. The number 0.1 must not have a perfect representation in binary. (I've never been inspired to take a close look at binary fractions so I can't provide any details.) Here the higher precision of the double gives a more accurate representation than the float. Essentially there is a rounding error that is the difference.

I tried this and when expanding 1.1 to 12 decimal places I got:
double = 1.100000000000, float = 1.100000023842

I don't know how many digits are really accurate on a double or a float. But anyway you can see there is a difference after 8 decimal places.
Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
Hi James,

I got interested in this problem and wrote a program to see what's going on. Here's the code.



And here's the output:
0.0625 4 0.0001 4
0.09375 5 0.00011 5
0.09765625 8 0.00011001 8
0.099609375 9 0.000110011 9
0.099853515625 12 0.000110011001 12
0.0999755859375 13 0.0001100110011 13
0.0999908447265625 16 0.0001100110011001 16
0.09999847412109375 17 0.00011001100110011 17
0.09999942779541016 17 0.00011001100110011001 20
0.09999990463256836 17 0.000110011001100110011 21
0.09999996423721313 17 0.000110011001100110011001 24
0.09999999403953552 17 0.0001100110011001100110011 25
0.09999999776482582 17 0.0001100110011001100110011001 28
0.09999999962747097 17 0.00011001100110011001100110011 29
0.09999999986030161 17 0.00011001100110011001100110011001 32
0.09999999997671694 17 0.000110011001100110011001100110011 33
0.09999999999126885 17 0.000110011001100110011001100110011001 36
0.09999999999854481 17 0.0001100110011001100110011001100110011 37
0.0999999999994543 16 0.0001100110011001100110011001100110011001 40
0.09999999999990905 17 0.00011001100110011001100110011001100110011 41
0.0999999999999659 16 0.00011001100110011001100110011001100110011001 44
0.09999999999999432 17 0.000110011001100110011001100110011001100110011 45
0.09999999999999787 17 0.000110011001100110011001100110011001100110011001 48
0.09999999999999964 17 0.0001100110011001100110011001100110011001100110011 49
0.09999999999999987 17 0.0001100110011001100110011001100110011001100110011001 52
0.09999999999999998 17 0.00011001100110011001100110011001100110011001100110011 53
0.1 1 0.0001100110011001100110011001100110011001100110011001101 55


0.0625 4 0.0001 4
0.09375 5 0.00011 5
0.09765625 8 0.00011001 8
0.099609375 9 0.000110011 9
0.099853516 9 0.000110011001 12
0.099975586 9 0.0001100110011 13
0.099990845 9 0.0001100110011001 16
0.099998474 9 0.00011001100110011 17
0.09999943 8 0.00011001100110011001 20
0.099999905 9 0.000110011001100110011 21
0.099999964 9 0.000110011001100110011001 24
0.099999994 9 0.0001100110011001100110011 25
0.1 1 0.000110011001100110011001101 27

I hope this looks OK on the screen. The lines are pretty wide.
:roll:
Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
Oh darn the output got mangled
I'll try putting it in a code window. If that doesn't work you'll just have to run it yourself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the difference between 1.0 and 1.1
 
Similar Threads
Need help with Overloading Methods
Problem with Sort Method
Swap function in java
Fractions
toString() method