4

I'm getting huge differences when I pass a float from C# to C++. I'm passing a dynamic float wich changes over time. With a debugger I get this:

c++ lonVel    -0.036019072    float
c#  lonVel    -0.029392920    float

I did set my MSVC++2010 floating point model to /fp:fast which should be the standard in .NET if I'm not mistaken, but this didn't help.

Now I can't give out the code but I can show a fraction of it.

From C# side it looks like this:

namespace Example
{
    public class Wheel
    {       
        public bool loging = true;
        #region Members     
        public IntPtr nativeWheelObject; 
        #endregion Members

        public Wheel()
        {           
            this.nativeWheelObject = Sim.Dll_Wheel_Add();
            return;
        }

        #region Wrapper methods 
        public void SetVelocity(float lonRoadVelocity,float latRoadVelocity {
              Sim.Dll_Wheel_SetVelocity(this.nativeWheelObject, lonRoadVelocity, latRoadVelocity);
        }
        #endregion Wrapper methods
    }

    internal class Sim
    {
        #region PInvokes
        [DllImport(pluginName, CallingConvention=CallingConvention.Cdecl)]
                public static extern void Dll_Wheel_SetVelocity(IntPtr wheel,
                     float lonRoadVelocity, float latRoadVelocity);
        #endregion PInvokes 
    }       
}

And in C++ side @ exportFunctions.cpp:

EXPORT_API void Dll_Wheel_SetVelocity(CarWheel* wheel, float lonRoadVelocity,
     float latRoadVelocity) {
        wheel->SetVelocity(lonRoadVelocity,latRoadVelocity);
}

So any sugestions on what I should do in order to get 1:1 results or at least 99% correct results.

chue x
  • 18,573
  • 7
  • 56
  • 70
JohnYouDontLike
  • 71
  • 1
  • 1
  • 8
  • Have you tried marking your floating point value `volatile` ? Or have you tried doing this with static values? – Ahmed Masud May 31 '14 at 21:30
  • like this? public volatile float lonVel; public volatile float latVel; public void SetVelocity(float lonRoadVelocity,float latRoadVelocity){ lonVel = lonRoadVelocity; latVel = latRoadVelocity; Sim.Dll_Wheel_SetVelocity(this.nativeWheelObject,lonVel,latVel); } This way it didnt help – JohnYouDontLike May 31 '14 at 22:01
  • 2
    It would be nice if the difference was actually huge. It is not, there's no simple explanation for this, other than that you're just getting confused about the values that the C# code actually passed. – Hans Passant May 31 '14 at 22:35
  • 1
    There is no reasonable explanation in this code for a difference of this magnitude. There are too many pieces missing to work out what it might be. Please submit a Minimal Workable Example that shows fully what you are doing. Otherwise I think this question should be closed. – david.pfx Jun 01 '14 at 06:15
  • Hans, The numbers arent huge indeed, but the difference itself is quite huge actually. A fraction of 0.006626152 gets lost in the passing process. Wich is 20.25...% of difference, if it would have been 1% then it wouldn't be that much of a diference, but 20% is quite huge actually. And David, I'm sorry I cant give out the whole code, but the point stays the same, if you pass a dynamically changing float, the values get messed, you can try this with a simple timestep based small timer, and pass this float through the same process (from C# to C++ with a dll) and you should face the same issue. – JohnYouDontLike Jun 01 '14 at 08:54

2 Answers2

0

I believe this may describe your issue, http://msdn.microsoft.com/en-us/library/c151dt3s.aspx

I recommend choosing another data type if possible.

Floats can change from one program to another or even within the same application. Here is some reading on the subject: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

So you can either implement an "AlmostEqual" approach, or simply choose another data type.

1232133d2ffa
  • 191
  • 1
  • 13
  • hmm, interesting. But C# side is not that important actually. I just need to pass some C# floats to C++. I've seen some custom data types in C++ before, but not familiar with these, so any chanse of doing this(custom data type, i.e. mfloat instead of float) on C++ would help? – JohnYouDontLike Jun 01 '14 at 08:58
0

Still havent found a way to completely fix my issue, but for others who might have got the same issue, setting /fp:fast and /arch:SSE2 got me much closer!

My debuging results where my problem occured:

latRoadVelocity -0.15862428 float
lonRoadVelocity -0.036250707    float
wheel->latRoadVelocity  -0.15102120 float
wheel->lonRoadVelocity  -0.036250707    float

as you can see my lonRoadVelocity currently is 1:1, but there still is some small difference in latRoadVelocity. Will see if I can get this one also 1:1 some how.

JohnYouDontLike
  • 71
  • 1
  • 1
  • 8