A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Certification
»
Programmer Certification (SCJP/OCPJP)
Author
== operator comparisons
Guangcheng Zhou
Greenhorn
Joined: Oct 13, 2006
Posts: 11
posted
Nov 01, 2006 19:35:00
0
Hi all,
I was wondering why the following code is printing true and false.
double d1 = 2.5;
double d2 = 2.6;
System.out.println(d1 == 2.5f);
System.out.println(d2 == 2.6f);
Shouldn't they be printing the same boolean value at least?
Thanks,
Mack
sachin poddar
Greenhorn
Joined: Feb 10, 2006
Posts: 17
posted
Nov 01, 2006 22:09:00
0
try this..
double d1= 2.5f;
double d2= 2.6f;
System.out.println(d1);
System.out.println(d2);
.. now i think you've got answer!
Sachin. [SCJP 1.4, SCWCD 5]
pranav thakker
Greenhorn
Joined: Nov 21, 2005
Posts: 20
posted
Nov 01, 2006 22:19:00
0
I am still not getting this. Can any one explain why value of 2.6f is 2.5999999046325684 ???
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
Nov 02, 2006 00:03:00
0
Check out the #21 in the
Java Beginners FAQ
. It links to articles that explain in detail what's going on.
Android apps
–
ImageJ plugins
–
Java web charts
I agree. Here's the link:
http://zeroturnaround.com/jrebel
subject: == operator comparisons
Similar Threads
double & float related Qn
pls help on NaN
if(0.0 == -0.0) - Sample question
Question for Dan Wrapper Class
Issue adding double value
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter