Re: When to use float (in teaching)
- From: Patricia Shanahan <pats@xxxxxxx>
- Date: Thu, 07 May 2009 14:01:33 -0700
Christian 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...
In class I try to explain what mantisse/exponent is and what it means for precicion ("imagine the float as an int with 256 bit, the exponent gives you an offset in that int and in the mantisse are the next x bits stored ...like this with very few bits you can approximate numbers..bla bla" )... bring a counter example that works with double but not with float ... and end with resume "in general float is good" but know that there exists double...
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...
You seem to have left out what I view as the most important case for
double. Any floating point program needs some level of numerical
analysis to ensure the results are meaningful.
For double, the analysis is often trivial. Worst case bounds, with no
statistical analysis, are enough to show that the calculation will
result in a result that is sufficiently accurate.
For float, simple worst case bounds work for only the shortest, simplest
calculations with narrow ranges of inputs. It is easy for two numbers in
a real world calculation to have a ratio of the order of tens of thousands.
An int calculation will give the same results as mathematical integer
arithmetic as long as every intermediate result fits in the int range, a
relatively simple condition. There is no such simple rule for ensuring
correct results from float.
Patricia
.
- References:
- Re: When to use float (in teaching)
- From: Christian
- Re: When to use float (in teaching)
- Prev by Date: Re: When to use float (in teaching)
- Next by Date: Re: When to use float (in teaching)
- Previous by thread: Re: When to use float (in teaching)
- Next by thread: Re: When to use float (in teaching)
- Index(es):
Relevant Pages
|