File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 'if' with  float &double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Watch " New topic
Author

'if' with float &double

nachiket deshpande
Ranch Hand

Joined: Oct 06, 2000
Posts: 114
hi all!
i have doubt in the following two simple if statements.please help
if(f*3.0F==1.0F)where f=1.0F/3.0F
returns true though the value of f is 0.33333...how come f*3.0F gives 1.0F
also
if(f*3.0==1.0)returns false?i can understand that f is promoted to double but why it i false
same with double
if(d*3.0==1.0)where d=1.0/3.0,
returns true even d is again 0.33333333333 how d*3.0 is 1.0?

sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
Originally posted by nachiket deshpande:
hi all!
i have doubt in the following two simple if statements.please help
if(f*3.0F==1.0F)where f=1.0F/3.0F
returns true though the value of f is 0.33333...how come f*3.0F gives 1.0F
also
if(f*3.0==1.0)returns false?i can understand that f is promoted to double but why it i false
same with double
if(d*3.0==1.0)where d=1.0/3.0,
returns true even d is again 0.33333333333 how d*3.0 is 1.0?


I saw this piece of code on the sun's web site .I thought it might be of help to you.
class Test {
public static void main(String[] args) {
short s = 12; // narrow 12 to short
float f = s; // widen short to float
System.out.println("f=" + f);
char c = '\u0123';
long l = c; // widen char to long
System.out.println("l=0x" + Long.toString(l,16));
f = 1.23f;
double d = f; // widen float to double
System.out.println("d=" + d);
}
}
It produces the following output:
f=12.0
l=0x123
d=1.2300000190734863
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 'if' with float &double