cyberflygo Posted December 13, 2008 Report Posted December 13, 2008 hi,peter when I read the Heading 0x580,Ican't get the correct value.can you help me .FS2004 show heading is 340 . DWORD head; if (!FSUIPC_Read(0x580, 4, &head, &dwResult)||!FSUIPC_Process()) {} int heading= head*360/(65536*65536); Edit12->Text =heading; i track into .found the value of head is 3730953 .so 3730953*360/(65536*65536)<0,program error.
Pete Dowson Posted December 13, 2008 Report Posted December 13, 2008 int heading= head*360/(65536*65536); ... i track into .found the value of head is 3730953 .so 3730953*360/(65536*65536)<0,program error. I'm not surprised, since all FS angular measures in fixed point fields are designed to achieve maximum resolution in the space available. To do this in a 32-bit value means the maximum value in 32 bits is used to provide 359.999999... degrees. You are multiplying a very large value by 360, so getting overflow. Even if you did not get overflow, dividing a 32 bit integer by 65536 * 65536 is similar to shifting it downwards by 16 + 16 = 32 bits -- guaranteeing to leave you with nothing at all for your efforts! All this is surely simple arithmetic, can you see? Copy the value you read into a floating point variable, like a "double" first, THEN do your conversions! This applies to nearly all the fixed point values -- unless you want simple approximations (which you could get above by doing the arithmetic in a different order, e.g. int heading = ((head/65536)*360)/65536; That rounds down to the integer below. Of course it would be a better approximation if you rounded to the nearest integer instead, thus: int heading = (((head/65536)*360) + 32768)/65536; Regards, Pete
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