Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Holding Data in a scaled Unsigned short

Status
Not open for further replies.

porto99

Technical User
Nov 1, 2004
96
GB
I have to read and store data in a series of unsigned short variables in Shared Memory.
The data is scaled and stored, but entered and displayed in engineering units.

Now these variables are scaled and I need to convert them into their proper units. I display them as either float or integers.

For example for one variable:-
Max Val = 0.999969
Min Val = -1.000000

Least Sig Bit = 0.000030517578 = scaleFactor1.

So what I do is:-

// Get current data item for Shared Memory
unsigned short currentField = *(unsigned short *) fieldPtr;
float scaleFactor1 = (float)GetScalingFactor(scaleFactorStr);
float rescaledDataEng = currentField * scaleFactor1;


This works OK (rescaledDataEng ) for positive numbers, but for negative numbers it fails, e.g. I get for -0.44 the incorrect value displayed as (rescaledDataEng = 1.560028).

Also I dont think that I store the data (negative value corectly) when writing to the Shared Memory:-

*(unsigned short *) fieldPtr = (unsigned short)(fEngValue/m_fScalingFactor);

For example for another variable:-
Max Val = 179.999
Min Val = -180.000

Least Sig Bit = 0.005493 = m_fScalingFactor.

I enter -20 and 159.01 is displayed, but if I enter 20, then 20.000 is displayed OK.

Any suggestions please as the more I look at the problem the more confused I get.

In summary I take the entered value and scale it (divide by m_fScalingFactor) and store it.
Then I get the value and re-scale for display (multiple by m_fScalingFactor). All works fine apart from negative numbers.


 
could this just be as simple as you are assigning a signed number to an unsigned short?

If somethings hard to do, its not worth doing - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top