473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1592
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

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

Similar topics

31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
12
by: meltedown | last post by:
I would like the floating divs to float and then the header to come after them , on the left. That's what I thought clearing the floats was for, but in this example, the header is to the right of...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.