File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes float or double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "float or double" Watch "float or double" New topic

float or double

Ravi Shankarappa
Ranch Hand

Joined: Jan 09, 2010
Posts: 55
I have to write a Java code that is computational intensive and has array of arrays ([N][N] where N = 4000). None of the values are over what a float can represent. I was told in Java it is better to use double rather than float for speed. Can someone clarify if this is true?


Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I believe that doubles are only "faster" on processors that are optimized for mathematical calculations. But depending on what sort of data you need to represent, there are pros and cons for both double and float values.

What kind of data do you need to represent?


"If the facts don't fit the theory, get new facts" --Albert Einstein
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Ravi Shankarappa wrote:None of the values are over what a float can represent.

Can you clarify what "over" means in this sentence? Because normally the choice between double and float is not going to be made because double can represent numbers closer to infinity than float (or vice versa, if either is the case). It might be made because double can represent numbers closer to zero than float can, or because double can represent arbitrary numbers more precisely than float can.
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3733

Speed definitely should not be your main concern. What is more important is the amount of errors that will be introduced to your computations by using wrong data type. Floats can keep only about seven significant digits (if I remember it correctly), while double keeps about fifteen. If you store the intermediate results of your calculations in floats, you'll strip the significant digits that double can keep but float cannot. This rounding might not be a problem when handling the final result, but if you do it at every step in a lenghty calculation, the rounding errors may accumulate and the final result might be off the correct (or rather expected) one by tens of percents or more. Floats are generally nearly unusable for computationally intensive applications. If you really, really need, store inputs and/or results as floats, but take care not to store as float any intermediate variable.

This is a general rule of thumb. It's possible that in a specific case you might not need doubles, but to be able to decide you'd need to have a good background in numerical mathematics, and spend so much time analyzing the problem that it is ususally not worth the trouble.

By the way, your array will be quite large (at least 128 MB when using doubles), but it is not that much by today's standards.
Ravi Shankarappa
Ranch Hand

Joined: Jan 09, 2010
Posts: 55
Thanks to everyone. I am playing data, values of which do not exceed 250000. The computation involves spline fitting and perhaps gaussian/lorentzian fitting. Martin's reply woke me up to what should be real concern for me instead of speed alone.

Thanks again to all.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Ravi Shankarappa wrote:I am playing data, values of which do not exceed 250000.

It does not matter what the maximum value is of the numbers is, and the range of values that float can represent is most likely more than enough (the largest value that a float can hold is approximately 3.4 x 10^38). You should think about the precision that is needed. A float has a precision of about 7 decimal digits, while a double has a precision of about 15 decimal digits. Maybe 7 digits is too little for your application, so that you'd get rounding errors, resulting in inaccurate results.
I agree. Here's the link:
subject: float or double
It's not a secret anymore!