unsigned short to floating point type and back

M

ma740988

I'm perusing code that reads 30 words from a memory mapped location.
Each word is 16 wide. The requirements within the specification on
Word 11 is as follows

WORD 11
SIGNAL: 2's complement
UNITS: Degrees
MAX VALUE : 33.332316
MIN VALUE: -33.333333
RESOLUTION: .0010172526
ACCURACY: NA
MSB: 16.666666
LSB: .0010172526
FULL SCALE: 33.332316

NAME: Pitch Pos
BIT RANGE: 0 (MSB) ......15 (LSB)


I'm reviewing source code that has set and get functions for handling
Word11 akin to what's show below. The question: Is there a good
alternative to what's presented below or is that as good as it gets?

Now referencing the Set_Pitch_Pos function, one thought was to treat
the double as an array of char. This way one could take the pitch_pos
value, strip out 16 bits then store that into Ushort_Vector... Trouble
is I dont think this is a valid alternative.


# include <vector>
# include <iostream>


enum {
WORD1 = 0 ,
//WORD2 ... WORD10
WORD11 = 10,
WORD30 = 29
};
typedef unsigned short WordType ;
typedef std::vector < WordType > USHORT_VEC ;
USHORT_VEC Ushort_Vector ( 30 ) ;

double const PITCH_POS_LSB = 0.001017253;
double const INV_PITCH_POS_LSB = 1. / 0.001017253;

void Set_Pitch_Pos ( double pitch_pos )
{
// store in location 10 (i.e WORD11)
Ushort_Vector [ WORD11 ] = (WordType) ( pitch_pos *
INV_PITCH_POS_LSB ) ;
}

double Get_Pitch_Pos()
{
// retrieve value from location 10 (i.e WORD11)
return (double) ( (short) Ushort_Vector [ WORD11 ] *
PITCH_POS_LSB );
}

int main()
{
// test... lets assume the value read from the FPGA memory location
is 65535.
// if the layout is 0..15, is 'unsigned short' appropriate here?
// shouldn't this be short since one could infer receipt of a
negative value
Ushort_Vector [ WORD11 ] = 65535 ;
std::cout << Get_Pitch_Pos () << std::endl;
Set_Pitch_Pos ( 33.333333 ); //max
std::cout << Get_Pitch_Pos () << std::endl;
Set_Pitch_Pos ( -33.333333 ); // min
std::cout << Get_Pitch_Pos () << std::endl;

std::cin.get();

}
 
R

Richard Herring

Observe that 0.001017253 is 1017253 / 1000000000, so you are
essentially calculating n * 1017253 / 1000000000. Without having the
benefit of some minor number crunching, I don't know offhand if there's
greatest common divisor that can be used to reduce the magnitude of the
nominator and the denominator.

No. 3 is neither 5 nor even.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top