Float point variable comparison issue?

F

Fritz

If I run this on my computer exactly as typed below, 'pos2' and 'hold'
are not equal (Windows XP, AMD Athlon 64 X2 Dual, .NET 3.5, Release
Build). Both floats are computed separately, but the same way and with
the same inputs. How can this be?


class Program
{
static int minPos = 0;
static float res = Convert.ToSingle("0.004");

public static float ConvertToSlideZCoord(int stageZCoord)
{
return (stageZCoord - minPos) * res;
}


static void Main(string[] args)
{
while (true)
{
int steps = 41303;

Console.WriteLine("Steps = " + steps);
float hold = ConvertToSlideZCoord(steps);

Console.WriteLine("Steps = " + steps);
float pos2 = ConvertToSlideZCoord(steps);
if (pos2 != hold)
{
Console.WriteLine("Not equal");
Console.ReadKey();
}
}


}
}
 
R

Rick Lones

I am unable to reproduce this result with .NET 2.0/XP/Intel Xeon. I would ask:

- If you keep an iteration counter in your loop, does the problem still occur?
- If so, does the problem occur on the same iteration each time? (Peter's
question, essentially)
- Does the problem go away if you run in debug mode from the IDE?
- Have you tried this program on different models of hardware and/or on a
different instance of the same model? (Assuming those are available)
- Have you run diagnostics on your hardware, especially floating-point ones?
- Would you say that your computer has been rock-solid recently? Any flaky or
mysterious behaviour noted?
- Have you added a peripheral board or opened the case for some other reason
recently?

Obviously the .NET runtime and the OS can't be ruled out, but really this sounds
more like a hardware problem at this point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top