Re: float vs. double?



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
*/

}







.



Relevant Pages

  • Re: float vs. double?
    ... On a calculator (like Windows calculator), ... Maybe I was expecting the precision differences to be further out... ... big difference in the computation which the resultant is float vs. ...
    (microsoft.public.vc.mfc)
  • Re: float vs. double?
    ... 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. ... So of course a different precision is being stored. ...
    (microsoft.public.vc.mfc)
  • Re: float vs. double?
    ... On a calculator (like Windows calculator), ... Maybe I was expecting the precision differences to be further out... ... difference in the computation which the resultant is float vs. double. ...
    (microsoft.public.vc.mfc)
  • printing bit representation of floats
    ... i'm learning about the floating point format that's used on my computer ... calculator called BinCalc which shows the bits of whatever number. ... the code that's used to print the float bits is below. ... thought that the format that the software calculator is using would be ...
    (comp.lang.c)
  • Re: Losing Precision from FLOAT in DBD::Informix
    ... The versions of CSDK and IDS are also not ... STSM, Informix Database Engineering, IBM Data Management ... >> are declared FLOAT. ... >> - where and why is the precision lost? ...
    (perl.dbi.users)

Loading