When to use float (in teaching)

E

Eric Sosman

Lew said:
That bubble was burst upthread:

The bubble may have been prodded, but exhibits some
resilience and a resistance to bursting. For what it's
worth, my system[*] takes 5.88 seconds to do 200 naive
Gaussian eliminations on a 250x250 double matrix, 4.86
seconds to do the same on a float matrix. That's a 21%
speed penalty for double, which seems more than "modest."

[*] 3 GHz Pentium 4, Windows XP SP3, Java 1.6.0_13.
 
E

Eric Sosman

Thomas said:
According to Eric Sosman said:
The bubble may have been prodded, but exhibits some
resilience and a resistance to bursting. For what it's
worth, my system[*] takes 5.88 seconds to do 200 naive
Gaussian eliminations on a 250x250 double matrix, 4.86
seconds to do the same on a float matrix. That's a 21%
speed penalty for double, which seems more than "modest."

[*] 3 GHz Pentium 4, Windows XP SP3, Java 1.6.0_13.

That's why I wrote "in-CPU processing". Your matrix uses about
250 KB with floats, 500 KB with doubles. Your CPU has only 32 KB
of L1 cache for data, so what you are seeing is a cache effect.
For _storage_ (as opposed to _computations_), you should use
floats if their precision is sufficient for your data.

If we could get a useful "CPU-only" performance measure,
the distinction might be of practical value. But as long as
the CPU must acquire operands from and deliver results to RAM,
the CPU-plus-RAM system cannot run faster than the RAM will.
To put it another way, it doesn't matter that the CPU knows
the answer "now" if it can't reveal it until "later."
Nonetheless, when you perform the multiplications and subtractions that
Gaussian reduction entails, you should use doubles (i.e. you read floats
from the matrix, cast them to doubles, perform the computation, then
cast back the result as float and store it back in the matrix).

It would take an analysis beyond my feeble powers to say
for sure when it is or is not better to perform intermediate
calculations in higher precision. Keep in mind that rounding
to double and then rounding to float may give a different
result than rounding to float immediately. (Analogy: Rounding
4.047 to one decimal place gives 4.0, but rounding it first to
two places -- 4.05 -- leads to a final result of 4.1.)
 
M

Mark Thornton

Eric said:
Thomas said:
According to Eric Sosman said:
The bubble may have been prodded, but exhibits some
resilience and a resistance to bursting. For what it's
worth, my system[*] takes 5.88 seconds to do 200 naive
Gaussian eliminations on a 250x250 double matrix, 4.86
seconds to do the same on a float matrix. That's a 21%
speed penalty for double, which seems more than "modest."

[*] 3 GHz Pentium 4, Windows XP SP3, Java 1.6.0_13.

That's why I wrote "in-CPU processing". Your matrix uses about
250 KB with floats, 500 KB with doubles. Your CPU has only 32 KB
of L1 cache for data, so what you are seeing is a cache effect.
For _storage_ (as opposed to _computations_), you should use
floats if their precision is sufficient for your data.

My recommendation would be to use double unless your data exceeds cache
size and performance is critical. Even then you should only change to
float if you are competent at numerical analysis and can verify that the
resulting computation will be sufficiently accurate. And even then you
should first consider alternative algorithms that reduce the impact of
the cache.

Mark Thornton
 
A

Arne Vajhøj

Christian said:
Stefan said:
I teach some properties of the data type »double«:

public class Main
{ public static void main( final java.lang.String[] args ) {
java.lang.System.out.println( 0.1 + 0.1 + 0.1 == 0.3 );
java.lang.System.out.println( 1.1 * 1.1 == 1.21 ); }}

false
false

. I also teach that beginners should not use the data type
»float«, because it will only cause trouble.

imho this is a bad thing to tell...
Its like telling "Don't use int because it will only give you trouble ,
always use long"

Given that double over float both increases range and precision, while
long over int only increase range not precision and, and that long can
not be used for several basic things like array index, then that
comparison i snot valid.
For most applications (as in 99% of all I see) float is more than enough
for floating point computations.

That is strange. Double seems to be the standard today in both Java,
C#, C/C++ and Fortran for numerical calculations.
Also a situtation where float is preferred for me to double:
A volatile float is written atomic while a volatile double is not!
Though concurrency is at least here not part of the programming classes...

We believe that. As Lew explained, then volatile double are written
atomic. It is non-volatile double that are not written atomic.

Arne
 
A

Arne Vajhøj

Tom said:
Performance, of course! :)

There's no great advantage. For me, this works as a form of
documentation - whenever i see a float, i know it's something sums-ish
rather than mathematical.

double are not more mathemaical than float. Seen from a mathematical
perspective they work identical - just with a different number of
bits.

Arne
 
T

Tom Anderson

double are not more mathemaical than float. Seen from a mathematical
perspective they work identical - just with a different number of bits.

Arne, do me a favour and read what i wrote before responding to it. I use
double as a *convention* to indicate mathematicity, so when i see a double
in code *i* wrote, it *does* indicate that the code is more mathematical.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top