Re: When to use float (in teaching)



On May 7, 4:32 pm, Christian <fakem...@xxxxxx> wrote:
Stefan Ram schrieb:

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"

For most applications (as in 99% of all I see) float is more than enough
for floating point computations. And as one usually uses int and it
doesn't matter if you use long instead ... in some applications you are
just better off with using int or short or byte where applicable ...

There are 2 situations where double really starts to be useful ...
1. very high numbers ... and you don't want to use BigInt and co...
2. high numbers that need high precicision ... if the number is in the
Billions and you still need precicion behind the comma ...

2. sometimes happens i.e. if very large numbers are multiplied with very
small ones.. like a * a^-1
1. is rather rare...


I'd concur with Christian's points with a few reservations. One way
that I often look at this is that when I am storing measurements
(e.g., fluxes, distances, ...) I rarely have more than a few (2-3)
digits accuracy and so a float is more than adequate. [The common
exception is time where we often require double -- or occasionally
better -- accuracy.] So for storing data floats generally are fine.

However it's very easy to do computations where the answer may involve
millions of
intermediate steps. Here roundoff errors can propagate several
digits. If you start with a float the accuracy of the result may be
eroded. Also since most of Java's math functions return doubles,
doing calculations in float requires a fair number of tedious casts.
So when you are actually going to do significant manipulations,
doubles may be a bit safer and cleaner.

So my advice to zeroth order: measure in floats, compute in doubles.

All of this depends upon the context in which you program. YMMV.

Regards,
Tom McGlynn
.



Relevant Pages

  • Re: Floating Point Rounding Mode
    ... To emulate this, you might have better luck using doubles everywhere -- ... truncate on reconversion to float. ... > The trouble happens when I try to sum. ... using decimal numbers in the SQL Server code would be ...
    (microsoft.public.vb.general.discussion)
  • Re: When to use float (in teaching)
    ... Its like telling "Don't use int because it will only give you trouble, ... For most applications float is more than enough for floating point computations. ... And as one usually uses int and it doesn't matter if you use long instead ... ... a real world calculation to have a ratio of the order of tens of thousands. ...
    (comp.lang.java.programmer)
  • Re: When to use float (in teaching)
    ... I also teach that beginners should not use the data type ... Its like telling "Don't use int because it will only give you trouble ... For most applications float is more than enough ...
    (comp.lang.java.programmer)
  • Newby: More VC6 errors...
    ... I am trying to typecast an int and a float into a basic_string. ... The help files are less than helpful. ... How do I accomplish this? ... The trouble is I haven't touched C/C++ in 10 years. ...
    (microsoft.public.vc.language)
  • Re: using MFC VC++ - which is more efficient - float or double?
    ... I'm about to say may be null and void. ... I don't think its true that the hardware does everything as doubles. ... there's nothing that is 'saved' by using float. ... If you are working with large sets of data, then memory. ...
    (microsoft.public.vc.mfc)