473,770 Members | 6,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# floating point inconsistencies

3 New Member
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 1621
Plater
7,872 Recognized Expert Expert
Are you using Singles or Doubles, if you mix and match them, there will be conversion issues I think.
Oct 14 '08 #2
roche72
3 New Member
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 Recognized Expert Expert
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_PixelSiz e) - (this.m_ImageHe ight / 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
3684
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. However, they generate slightly different results for floating point numbers. Are they really supposed to generate exactly the same results? I guess so because both platforms are supposed to be IEEE floating point standard (754?) compliant. ...
5
3751
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 using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging session we found out, that our program was working correctly, but the floating point processing...
12
2821
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 the floating divs, nothing is cleared. What am I doing wrong ? Here is the url:http://www.reenie.org/test/test5.php Here is the code: <html> <head>
687
23735
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 believe that. in pieces of the application where speed really matters you can still use "normal" functions or even static methods which is basically the same. in C there arent the simplest things present like constants, each struct and enum...
7
3394
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 discuss about ways to emulate floating point calculations with just long int or long long int. For eg., if i've a formula X=(1-b)*Y + b*Z in floating point domain, i can calculate X with just long ints (but, some data may be lost in final division;...
15
3933
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 the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
4
2837
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 would like your expert advise before I publish any serious nonsense. Any comments are welcome, style, organization, hard errors, etc.
32
4118
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 ); } int main() { std::deque<double> pt ;
39
3582
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 out there understands what happens. Essentially, when I subtract the (double) function value GRID_POINT(2) from a variable which has been assigned the same value before this gives a non-zero result and I really do not understand why.
0
9595
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.