Floating Point Accuracy Issue

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi all.. Im a little rusty regarding floating point accuracy, and could do
with some hints as to what the issues are with the following problem.

I have a maximum value that is used to cap user input that when multiplied
by a maximum tolerance of 3.5, rounded up and then cast back to unsigned
long keeps the result within the limits of an unsigned long.

To test result accuracy, I multiply the max input value by the max tolerance
value in 2 ways, the first of which actually gives the correct answer of
4294967292.0 and the other loses accuracy giving 4294967296.0 which when
cast to a DWORD cycles the result back to 0 as its 1 number higher than can
be stored in it.

When considering answer1 does not use either variables, in its calculation,
how is the second answer losing accuracy when using the values from a
variable rather than when using the actual values themselves. I tried
setting different compiler options for floating point numbers but the
results are always the same.

Basically I need to achieve the same results using variables as when not
using them. Its almost as if the double variables are acting like floats or
using the values directly allows more accurate results by using internal
variables larger than doubles. Im not really sure which is the case or how
to get round the issue. Maybe this is only an issue on my pc.. not really
sure.

Any ideas ?

const double dMaxTol = 3.5;
const DWORD dwMaxVal = 1227133512;

double answer1 = 3.5*1227133512.0; // Correct
double answer2 = dMaxTol*(double)dwMaxVal; // Loses accuracy ?

// To save you time seeing the result heres a message box.

TCHAR szTemp[1024];
sprintf_s(szTemp, 1024, "dMaxTol = %f - dwMaxVal = %u\n" \
"3.5*1227133512.0 = %u\n" \
"dMaxTol*(double)dwMaxVal = %u\n",
dMaxTol,
dwMaxVal,
(DWORD)answer1,
(DWORD)answer2);
MessageBox(NULL, szTemp, "", MB_OK);

Any help greatly appreciated.


.



Relevant Pages

  • Re: Floating Point Accuracy Issue
    ... I have a maximum value that is used to cap user input that when multiplied ... by a maximum tolerance of 3.5, rounded up and then cast back to unsigned ... cast to a DWORD cycles the result back to 0 as its 1 number higher than can ... const double dMaxTol = 3.5; ...
    (microsoft.public.vc.language)
  • Re: Floating Point Accuracy Issue
    ... const double dMaxTol = 3.5; ... double answer2 = dMaxTol*dwMaxVal; ... by a maximum tolerance of 3.5, rounded up and then cast back to unsigned ... cast to a DWORD cycles the result back to 0 as its 1 number higher than can ...
    (microsoft.public.vc.language)
  • Re: Error in CF Decimal-type calculations?
    ... I was able to recreate the issue and the only workaround I found was to ... problem using floating point types or to cast to double before using the ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: allocating vectors in a function
    ... Don't cast the result of calloc(or malloc(), ... Any unnecessary cast is bad style, ... Don't use calloc() for floating point values in the first place. ...
    (comp.lang.c)
  • Re: POLLING
    ... the bread they have cast on the waters comes floating back to them, ...
    (alt.true-crime)