469,642 Members | 1,279 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,642 developers. It's quick & easy.

c# floating point inconsistencies

Help.

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
  1. class OmniCamera
  2. {
  3.   private float m_PixelSize;
  4.   private float m_ImageHeight;
  5.  
  6.   ...
  7.  
  8.   /// prints out the hex value of the floating point number.
  9.   private static string HexString(float val)
  10.   {
  11.     byte[] bytes = BitConverter.GetBytes(val);
  12.     string s = string.Empty;
  13.     for (int i = bytes.Length - 1; i >= 0; i--)
  14.     {
  15.        s += bytes[i].ToString("X2");
  16.     }
  17.     return s;
  18.   }  
  19.   public bool WorldFromImage(Vector2 screenPosition, out Vector3 worldPoint)
  20.   {
  21.     worldPoint = new Vector3();
  22.     ...
  23.  
  24.     worldPoint.Y = (screenPosition.Y * this.m_PixelSize) -  (this.m_ImageHeight / 2.0f);
  25.  
  26.     Debug.WriteLine("worldPoint.Y: " + HexString(worldPoint.Y) + 
  27.                              " screenPosition.Y: " + HexString(screenPosition.Y) + 
  28.                              " m_PixelSize: " + HexString(this.m_PixelSize) + 
  29.                              " m_ImageHeight: " + HexString(this.m_ImageHeight));
  30.     Debug.WriteLine("worldPoint.Y: " + worldPoint.Y.ToString("r") + 
  31.                              " screenPosition.Y: " + screenPosition.Y.ToString("r") + 
  32.                              " m_PixelSize: " + this.m_PixelSize.ToString("r") + 
  33.                              " m_ImageHeight: " + this.m_ImageHeight.ToString("r"));
  34.  
  35.     ...
  36.  
  37.     return true;
  38.   }
  39. }
  40.  
Here is the output of the code when worldFromImage(...) gets executed from the worker thread:

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
Oct 13 '08 #1
3 1465
Plater
7,872 Expert 4TB
Are you using Singles or Doubles, if you mix and match them, there will be conversion issues I think.
Oct 14 '08 #2
Are you using Singles or Doubles, if you mix and match them, there will be conversion issues I think.
Everything is singles (floats). But even if i was mixing and matching I would expect the same result each time through.
Oct 14 '08 #3
Plater
7,872 Expert 4TB
Yeah it *should* be the same, as long as its called with identical data.

Seems like this line is the issue yes?
worldPoint.Y = (screenPosition.Y * this.m_PixelSize) - (this.m_ImageHeight / 2.0f);

I did the following and always go the same number:
Expand|Select|Wrap|Line Numbers
  1. float m_PixelSize=13.24572F;
  2. float m_ImageHeight=7.5757F;
  3. float screenPosition = 3.4F;
  4. for (int i = 0; i < 10; i++)
  5. {
  6.    float Y = (screenPosition * m_PixelSize) - (m_ImageHeight / 2.0f);
  7.    System.Diagnostics.Debug.WriteLine(Y.ToString() + "=(" + screenPosition + "*" + m_PixelSize + ")-(" + m_ImageHeight + "/2.0F)");
  8. }
  9.  
Oct 14 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Anton Noll | last post: by
12 posts views Thread by meltedown | last post: by
687 posts views Thread by cody | last post: by
7 posts views Thread by Vinoth | last post: by
15 posts views Thread by michael.mcgarry | last post: by
4 posts views Thread by jacob navia | last post: by
32 posts views Thread by ma740988 | last post: by
39 posts views Thread by rembremading | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.