This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What is the difference between a double and a float? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the difference between a double and a float?" Watch "What is the difference between a double and a float?" New topic
Author

What is the difference between a double and a float?

Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
I keep encountering an error in my programs whenever I have float variables. In debugging the error usually says something along the lines of found a double required a float. I thought if I had a floating point result and I used floating point variables I would get a float as out put i.e float = float * float.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Can you post a simple test case?
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
Here is the code:

Actual error is Found: double Required: float

(Marilyn reformated long line in code)
[ August 09, 2002: Message edited by: Marilyn de Queiroz ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi Matt,
After running the program I discovered 2 things:
1. the literal 1.5 is a double. Use 1.5f instead.
2. I need a raise.
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
What makes the compiler believe 1.5 is a double and not a float?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Because of the JLS:

A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.
Khurram Shahood(SCJP2)
Greenhorn

Joined: Jun 08, 2002
Posts: 18
hi
with respect to capacity.


khurram shahood(Java Farmer)
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Matt Kidd:
What makes the compiler believe 1.5 is a double and not a float?


In java the default decimal is a double, not a float. You have to specifically state when you want a float (22.5F)


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Hi Matt,
To be convinced of the difference between float and double ,and the danger in type casting, you can run this simple test program:

The output ought to be
f = 0.95
d = 0.95
d==f? false
(double)f= 0.949999988079071
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192

C:\Java\EigeneJavaProgramme>javac GrossPay1.java
GrossPay1.java:4: possible loss of precision
found : double
required: float
float hoursWorked = 3.056;
^
GrossPay1.java:5: possible loss of precision
found : double
required: float
float workRate = 4.0125;
^
GrossPay1.java:9: possible loss of precision
found : double
required: float
yourPay = ((hoursWorked - 40) * (workRate * 1.5)) + (40 * workRate);
^
3 errors
Why is double found? The input of the yourPay calculation is float so the output to yourpay should
Be float, too and not double.
Thank you very much for your answers.
Thomas
Peter Kristensson
Ranch Hand

Joined: Jul 02, 2001
Posts: 118

Note the "1.5", that's treated as a double, so the whole result returns a double. Use 1.5f instead.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi Thomas. The primitive double is like the Borg among other primitive types -- any primtive type that interacts with a double will return a double. For example,
double * float
double * long
double * int
double * short
double * byte'
will return a double result.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10

Put 'F' after each literal and it will work. If you have an expression which includes a double and a float, the float will be automatically promoted to a double.
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
Thanks everyone...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: What is the difference between a double and a float?
 
Similar Threads
Float Wrapper Class -- accept double
K&B book chapter 1 Q.No.15
long and float
Floating point literals
Strange fractions!