Re: float vs. double?
- From: "Robert Wong" <robertwong@xxxxxxxxxxx>
- Date: Wed, 20 Dec 2006 14:04:20 -0500
Hi David,
Thanks for the explanation, my application uses float and I think the
problem is someone else using a calculator or the Windows calculator
application shouldn't expect that much of a difference.
The calculator/windows calculator will give the 1004714.708 answer.
/* output
Float value Answer = 1004714.688
Double value Answer = 1004714.708
*/
I wasn't sure if it was a code generation problem. Also kind of weird is
depending on the variable type, is different precision being stored? The
same multplication is used.
answer = value * lsb; // debugger - 1004714.7 float
danswer = value * lsb; // debugger - 1004714.7080078125 double
Thanks again,
Robert
"David Webber" <dave@xxxxxxxxxxxxxxxxxxx> wrote in message
news:uWO7lSFJHHA.3676@xxxxxxxxxxxxxxxxxxxxxxx
"Robert Wong" <robertwong@xxxxxxxxxxx> wrote in message
news:uDxKgBFJHHA.1248@xxxxxxxxxxxxxxxxxxxxxxx
I was wondering if someone can explain this? There seems to be a big
difference in the computation which the resultant is float vs. double.
I'm
not sure if I understand it myself.
Is this a compiler problem? or just a precision loss?
It's a precision feature (rather than loss). Scientifically in all
floating point calculations like
6.3 * 5.4
the meaning of 6.3 is "some number between 6.250000000recurring and
6.3499999999recurring". You can render it more precise by writing 6.3000
if you wish.
Now on to computers: if you type 6.3 the computer will add trailing
zeroes to the precision defined by float or double. But the
interpretation as being within a range of specified precision applies
after that. The non-existent extra digits will not be zero by default as
the number is stored as hexadecimal and not decimal. [And numbers like
0.100000000recurring do not in general end in an infinite number of zeroes
when expressed in hexadecimal form.]
All your answers are identical to 8 significant figures - so what's the
problem?
I believe - BTW - that these days chips are optimised for double precision
arithmetic. Using float will be slower than double, so there's really
no need for it.
Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm
long int value = 1028827861;
float lsb = 0.0009765625;
float answer;
double danswer;
answer = value * lsb; // debugger - 1004714.7 float
danswer = value * lsb; // debugger - 1004714.7080078125 double
printf("\nFloat value Answer = %6.3f",answer);
printf("\nDouble value Answer = %6.3f",danswer);
/* output
Float value Answer = 1004714.688
Double value Answer = 1004714.708
*/
}
.
- Follow-Ups:
- Re: float vs. double?
- From: David Webber
- Re: float vs. double?
- From: David Wilkinson
- Re: float vs. double?
- References:
- float vs. double?
- From: Robert Wong
- Re: float vs. double?
- From: David Webber
- float vs. double?
- Prev by Date: Re: float vs. double?
- Next by Date: Re: CString and UTF-8
- Previous by thread: Re: float vs. double?
- Next by thread: Re: float vs. double?
- Index(es):
Relevant Pages
|
Loading