Floating Point Accuracy Issue
- From: "Callum Winter" <Spam not required>
- Date: Sun, 26 Aug 2007 21:27:15 +0100
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.
.
- Follow-Ups:
- Re: Floating Point Accuracy Issue
- From: Vincent Fatica
- Re: Floating Point Accuracy Issue
- From: Vincent Fatica
- Re: Floating Point Accuracy Issue
- Prev by Date: Re: Initializing a class field of a custom struct type?
- Next by Date: Re: On external projects
- Previous by thread: On external projects
- Next by thread: Re: Floating Point Accuracy Issue
- Index(es):
Relevant Pages
|