Jump to content
The simFlight Network Forums

Read Heading value 0x580 have problem


Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.