posted 7 years ago

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?

Thanks

Ravi

Thanks

Ravi

posted 7 years ago

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?

Hunter

What kind of data do you need to represent?

Hunter

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

posted 7 years ago

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.

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.

posted 7 years ago

- 1

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.

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

Posts: 57

posted 7 years ago

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.

Thanks again to all.

posted 7 years ago

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

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.

Consider Paul's rocket mass heater. |