I have a simple floating point calculation and depending on what thread it gets executed on, I get a different result. Here are the pieces of code:
Expand|Select|Wrap|Line Numbers
- class OmniCamera
- {
- private float m_PixelSize;
- private float m_ImageHeight;
- ...
- /// prints out the hex value of the floating point number.
- private static string HexString(float val)
- {
- byte[] bytes = BitConverter.GetBytes(val);
- string s = string.Empty;
- for (int i = bytes.Length - 1; i >= 0; i--)
- {
- s += bytes[i].ToString("X2");
- }
- return s;
- }
- public bool WorldFromImage(Vector2 screenPosition, out Vector3 worldPoint)
- {
- worldPoint = new Vector3();
- ...
- worldPoint.Y = (screenPosition.Y * this.m_PixelSize) - (this.m_ImageHeight / 2.0f);
- Debug.WriteLine("worldPoint.Y: " + HexString(worldPoint.Y) +
- " screenPosition.Y: " + HexString(screenPosition.Y) +
- " m_PixelSize: " + HexString(this.m_PixelSize) +
- " m_ImageHeight: " + HexString(this.m_ImageHeight));
- Debug.WriteLine("worldPoint.Y: " + worldPoint.Y.ToString("r") +
- " screenPosition.Y: " + screenPosition.Y.ToString("r") +
- " m_PixelSize: " + this.m_PixelSize.ToString("r") +
- " m_ImageHeight: " + this.m_ImageHeight.ToString("r"));
- ...
- return true;
- }
- }
worldPoint.Y: BFAA07A7 screenPosition.Y: 43EB1D26 m_PixelSize: 3D0240B8 m_ImageHeight: 420240B8
worldPoint.Y: -1.32835853 screenPosition.Y: 470.227722 m_PixelSize: 0.0318 m_ImageHeight: 32.5632
Here is the output of the code when worldFromImage(...) gets executed from the main app thread:
worldPoint.Y: BFAA07A8 screenPosition.Y: 43EB1D26 m_PixelSize: 3D0240B8 m_ImageHeight: 420240B8
worldPoint.Y: -1.32835865 screenPosition.Y: 470.227722 m_PixelSize: 0.0318 m_ImageHeight: 32.5632
As you can see the right hand side of the equation has exactly the same values. But we get a different result from the different threads. I thought c# floating point followed the IEEE floating point standard, and I thought that guarenteed that floating point operations always returned the same value.
Any insight would be greatly appreciated. I would like to understand this behaviour.
-thanks
steve