tomcontr Posted September 5, 2005 Report Posted September 5, 2005 Does any one know what could be going wrong in these piece of code? double hdg; char headi[64]; if (FSUIPC_Read(0x580, 4, &hdg, &dwResult) && FSUIPC_Process(&dwResult)) { hdg = (((hdg + 32768) / 65536) * 360) / 65536; if (hdg >= 360) { hdg = hdg - 360; } sprintf(headi, "%f", hdg); } It´s really weird, because the hdg variable, after being proceced by FSUIPC it retunes a value like this "-9.2559629702313e+061", so I think that the problemens are not the calculations. Hope that somene can help me Ragards Thomas
Pete Dowson Posted September 5, 2005 Report Posted September 5, 2005 Does any one know what could be going wrong in these piece of code? double hdg; char headi[64]; if (FSUIPC_Read(0x580, 4, &hdg, &dwResult) && ... Yes. You are reading a 32-bit (4 byte) integer into a 64-bit (8-byte) floating point variable and then treating the result, which will be half uninitialised rubbish and half unintelligible as a floating point number, as if it were a legitimate 64-bit floating point number. You need to think about what number formats things are in, and treat them appropriately. Read it into an int then copy it into a double if you like -- the compiler will include the correct machine instructions to do the conversion. But you cannot expect the correct result just treating one type of number as if it were another, and especially not when 32-bits worth of the number isn't even initialised. Regards, Pete
tomcontr Posted September 5, 2005 Author Report Posted September 5, 2005 so it should be something like this? int value; double hdg; char headi[64]; if (FSUIPC_Read(0x580, 4, &value, &dwResult) && FSUIPC_Process(&dwResult)) { hdg = (((value + 32768) / 65536) * 360) / 65536; if (hdg >= 360) { hdg = hdg - 360; } sprintf(headi, "%d", hdg); } Thomas.
Pete Dowson Posted September 5, 2005 Report Posted September 5, 2005 so it should be something like this? Did you try it? You would get better acuracy and make the code a lot simpler if you converted to floating point THEN did the calculation, thus: hdg = value; hdg = (hdg * 360.0) / (65536.0 * 65536.0); or this, which is really the same, as the compiler will convert 'value' before it does any computation: hdg = (value * 360.0) / (65536.0 * 65536.0); BTW Because "value" is signed, this will give you a signed heading, i.e +179 to -180, rather than the usual 0 to 359. You may want to make "value" an "unsigned int" (a Windows DWORD in fact) instead. Regards, Pete
tomcontr Posted September 6, 2005 Author Report Posted September 6, 2005 now is working perfectly. :D DWORD value; DWORD hdg; char headi[64]; if (FSUIPC_Read(0x580, 4, &value, &dwResult) && FSUIPC_Process(&dwResult)) { hdg = value; hdg = (hdg * 360.0) / (65536.0 * 65536.0); if (hdg >= 360) { hdg = hdg - 360; } sprintf(headi, "%d", hdg); } Thanks a lot for your help. Best Regards Thomas
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now