aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting of double to float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting of double to float" Watch "casting of double to float" New topic
Author

casting of double to float

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
class DefaultAssignment extends Print{
public static void main(String args[ ]){
float f=(float)1.3;//casting to float
double d=1.3;
print(f);//1.2999999523162842???
print(" "+f+" "+ d);//1.3 1.3???
}
}
//why the output of f is different in two successive print method???
//I guess this error is because of the floating point representation float in binary............


Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Can you give us a hint as to what the print method does?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
u can replace the print method with System.out.println();Print was declared inside the package whose job was the same as that of println();
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Harvinder
Which compiler/version are you using.
Here's the modified code

The output for me is

I'm using
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Print.print must be doing something screwy. Can you show us the code for that method?
Upon further evaluation, it looks like the print method that you are using doesn't take a float but is instead casting the float to a double. You get the same effect by doing this:
System.out.println((double)f);
It looks like the person who wrote the print() methods overloaded it with a String version and a double version but didn't realize that you need a float version or you have a casting problem when you promote the float to a double.
[ August 21, 2003: Message edited by: Thomas Paul ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Thomas
Thomas : It looks like the person who wrote the print() methods overloaded it with a String version and a double version but didn't realize that you need a float version or you have a casting problem when you promote the float to a double.
Why does it happen so?
Unni Kainila
Ranch Hand

Joined: Jul 26, 2003
Posts: 45
I think Thomas means to say that "print()" method is implemented like the one in the code below:

There are two overloaded methods for "print()" in the above example. One takes string as parameter and other one takes a double. While there is no overloaded method that takes float, a call "print(1.03f)" will be mapped to "print(double)" method, and the float is automatically casted to a double.
If you uncomment "print(float f)" method, the result will be displayed as expected.
But why it looses the precision when casted, I hope Thomas can explain to us?
thanks
Unni
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It's because floating point types can't hold most decimal values because most decimal values can't be represented in binary. And part of the problem is that the println(double) method is deceiving you. If we use the BigDecimal class to display exactly what is stored in a floating point then I think you will see the problem:
Take a look at this code:

This prints:
1.1
1.10000002384185791015625
It is the second number that is actually stored in the floating point becuase .1 can not be represented in binary.
Running the same program changing d to a double and the 1.1 float literal to a double literal gives this:
1.1
1.100000000000000088817841970012523233890533447265625
What if we make d a double but leave the literal as a float literal? then we get this:
1.100000023841858
1.10000002384185791015625
Why? Remember what the float really contained earlier: 1.10000002384185791015625. And that fits perfectly in a double. But it is not the same as the 1.1 double literal.
The println() actually has a complex method for figuring out what exactly to print. But you should always keep in mind that it rarely prints what the float or double actually contains.
So what does all this mean? Avoid using floats and doubles. Use the BigDecimal class when you care about doing math on decimals.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting of double to float
 
Similar Threads
Wrapper class question from Danchisolm
passing a float to a double
What is the difference between a double and a float?
converting float to double
convert double to float ?