DSPRelated.com
Forums

float -to - integer conversion

Started by rashmi venugopal March 21, 2011
hi sir,,

whats the key logic to convert floating point number to integer.

is that multiplication of floating point number and 32767
if so how does this 32767 has arrived,,

waiting for u r reply,,
thanking you,
u r sciencierly
rashmi
rashmi venugopal <rashu32003@n_o_s_p_a_m.gmail.com> wrote:
 
> whats the key logic to convert floating point number to integer.
The general key is to shift the significand appropriately for the value of the exponent. Some systems have instructions to do that, others require you to do it yourself. One way it can be done is by adding an appropriate constant such that the bits you need are shifted to the right place. For IEEE 32 bit values, if you add 8388608 (2**23) to a positive value that is less than that number, the integer value will be in the least significant 23 bits. (Subtract if negative, or absolute value first.)
> is that multiplication of floating point number and 32767 > if so how does this 32767 has arrived,,
That should probably by 32768, but maybe not. That allows for scaled values, where the binary point isn't to the right of the least significant bit. That is, fixed point but not integer. -- glen

rashmi venugopal wrote:

> hi sir,, > > whats the key logic to convert floating point number to integer. > > is that multiplication of floating point number and 32767 > if so how does this 32767 has arrived,, > > waiting for u r reply,, > thanking you, > u r sciencierly > rashmi
On 03/21/2011 03:31 AM, rashmi venugopal wrote:
> hi sir,, > > whats the key logic to convert floating point number to integer. > > is that multiplication of floating point number and 32767 > if so how does this 32767 has arrived,,
floor(). Seriously -- the key is to understand what the floating point number represents, then to figure out how to represent that number by an integer, then to do whatever math is necessary to make that happen. If you have a floating point number that ranges strictly from -1 to 1, and you want to capture the entire range of that floating point number in a 16-bit integer without overflow, then you'd multiply it by 32767 (in C, you'd multiply it by 32767.0, which is a float, then you'd convert the resulting floating point number to integer). But that may not be your scenario. If, for instance, you're converting the 'human livable' temperature range of -40 to +55 degrees C to fit into a 16-bit integer, and the precision requirements work out, then you may want to store the number in centi-degrees. Then your number would range from -4000 to +5500. This wouldn't use the whole range that you have available, but it would be easily 'human interpretable' if you looked at the number in a debugger. I can think of other examples, but they all boil down to figuring out what the number should mean, then figuring out how to fit that into an integer in a way that is both convenient for you and meets the performance and cost requirements of the system you're designing. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
>On 03/21/2011 03:31 AM, rashmi venugopal wrote: >> hi sir,, >> >> whats the key logic to convert floating point number to integer. >> >> is that multiplication of floating point number and 32767 >> if so how does this 32767 has arrived,, > >floor(). > >Seriously -- the key is to understand what the floating point number >represents, then to figure out how to represent that number by an >integer, then to do whatever math is necessary to make that happen. > >If you have a floating point number that ranges strictly from -1 to 1, >and you want to capture the entire range of that floating point number >in a 16-bit integer without overflow, then you'd multiply it by 32767 >(in C, you'd multiply it by 32767.0, which is a float, then you'd >convert the resulting floating point number to integer). > >But that may not be your scenario. If, for instance, you're converting >the 'human livable' temperature range of -40 to +55 degrees C to fit >into a 16-bit integer, and the precision requirements work out, then you >may want to store the number in centi-degrees. Then your number would >range from -4000 to +5500. This wouldn't use the whole range that you >have available, but it would be easily 'human interpretable' if you >looked at the number in a debugger. > >I can think of other examples, but they all boil down to figuring out >what the number should mean, then figuring out how to fit that into an >integer in a way that is both convenient for you and meets the >performance and cost requirements of the system you're designing.
thanks for the reply,, now if i have floating value ( 0.0592 , 0.0000, -0.0592 ) how to convert it to integer,,
> >-- > >Tim Wescott >Wescott Design Services >http://www.wescottdesign.com > >Do you need to implement control loops in software? >"Applied Control Theory for Embedded Systems" was written for you. >See details at http://www.wescottdesign.com/actfes/actfes.html >
On 03/22/2011 02:06 AM, rashmi venugopal wrote:
>> On 03/21/2011 03:31 AM, rashmi venugopal wrote: >>> hi sir,, >>> >>> whats the key logic to convert floating point number to integer. >>> >>> is that multiplication of floating point number and 32767 >>> if so how does this 32767 has arrived,, >> >> floor(). >> >> Seriously -- the key is to understand what the floating point number >> represents, then to figure out how to represent that number by an >> integer, then to do whatever math is necessary to make that happen. >> >> If you have a floating point number that ranges strictly from -1 to 1, >> and you want to capture the entire range of that floating point number >> in a 16-bit integer without overflow, then you'd multiply it by 32767 >> (in C, you'd multiply it by 32767.0, which is a float, then you'd >> convert the resulting floating point number to integer). >> >> But that may not be your scenario. If, for instance, you're converting >> the 'human livable' temperature range of -40 to +55 degrees C to fit >> into a 16-bit integer, and the precision requirements work out, then you >> may want to store the number in centi-degrees. Then your number would >> range from -4000 to +5500. This wouldn't use the whole range that you >> have available, but it would be easily 'human interpretable' if you >> looked at the number in a debugger. >> >> I can think of other examples, but they all boil down to figuring out >> what the number should mean, then figuring out how to fit that into an >> integer in a way that is both convenient for you and meets the >> performance and cost requirements of the system you're designing. > > > thanks for the reply,, now if i have floating value ( 0.0592 , 0.0000, > -0.0592 ) how to convert it to integer,,
int y; float x; y = x; -- Randy Yates Digital Signal Labs 919-577-9882 http://www.digitalsignallabs.com yates@digitalsignallabs.com
On 03/21/2011 11:06 PM, rashmi venugopal wrote:
>> On 03/21/2011 03:31 AM, rashmi venugopal wrote: >>> hi sir,, >>> >>> whats the key logic to convert floating point number to integer. >>> >>> is that multiplication of floating point number and 32767 >>> if so how does this 32767 has arrived,, >> >> floor(). >> >> Seriously -- the key is to understand what the floating point number >> represents, then to figure out how to represent that number by an >> integer, then to do whatever math is necessary to make that happen. >> >> If you have a floating point number that ranges strictly from -1 to 1, >> and you want to capture the entire range of that floating point number >> in a 16-bit integer without overflow, then you'd multiply it by 32767 >> (in C, you'd multiply it by 32767.0, which is a float, then you'd >> convert the resulting floating point number to integer). >> >> But that may not be your scenario. If, for instance, you're converting >> the 'human livable' temperature range of -40 to +55 degrees C to fit >> into a 16-bit integer, and the precision requirements work out, then you >> may want to store the number in centi-degrees. Then your number would >> range from -4000 to +5500. This wouldn't use the whole range that you >> have available, but it would be easily 'human interpretable' if you >> looked at the number in a debugger. >> >> I can think of other examples, but they all boil down to figuring out >> what the number should mean, then figuring out how to fit that into an >> integer in a way that is both convenient for you and meets the >> performance and cost requirements of the system you're designing. > > > thanks for the reply,, now if i have floating value ( 0.0592 , 0.0000, > -0.0592 ) how to convert it to integer,,
Oddly enough, in spite of giving you paragraphs of exposition, I did not seem to be able to make my point. You convert it to integer by doing what I said. As I said, you start with asking yourself "what does 0.0592 _mean_". Clearly you haven't done this, because you haven't said what your conclusion is. If it doesn't mean anything, then the easiest conversion function (in C) is: int convert_meaningless_float_to_int(float x) { return 0; } Does this clarify the need to _think_, and to _understand_ what your floating point number _means_? -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
>hi sir,, > >whats the key logic to convert floating point number to integer. > >is that multiplication of floating point number and 32767 >if so how does this 32767 has arrived,, > >waiting for u r reply,, >thanking you, >u r sciencierly >rashmi >
When writing a fixed point code , you should understand what will be the dynamic range of the variable and based on your processor architecture decide if you need a single-precision or double-precision representation. To answer your question , Where does 32767 comes from? In this case you are talking about a 16-bit processor so the range of signed numbers that could be represented using 16-bit is -32768 to 32767 . (-2^(n-1) to 2^(n-1)-1 look at 2-bit complement arithmetic for better understanding).This is in some jargon referred to as Q1.15.(Ex:0.2 is 6553 in Q1.15, implying 1sign bit and 15 fractional bits) Now not all floating-fixed conversion needs to be multiplied by 32767. You can only use 32767 if your sure if your input |x|<1 . What happens if you have to convert a number say x= 12.768? Multiplying by 32767 wouldn't fit in 16 bits. So now u need to figure out how many bits are need for the integer and allocate the rest for fractional or vice versa. In this example to fit 12 you would need 4 bits, leaving 1 bit for sign you will need a Q5.11 representation.Not in this representation you could only represent -16<= to < 16, with 11 bit precision to the fractional part. What happens when x becomes 17 , in fixed point arithmetic x will be saturated. So you would need to make a wise decision on the Q representation for the variable. It may look complicated for fixed-point beginners but it a lot of fun to write fixed point code.
Man firstly you need to do the normalization (i guess n hope  you
understand it) and then according to the integer conversion either its
int16 or int 8 ,,,,,,just multiply it accordingly ......by that i mean
2^15-1 and 2^7-1 respectively....(one bit for sign )

by doing normalization (dividing all data by highest value )and then
multiplication you would be able to cover all rangeof integers

Personally I think that Tim made a very good clarification of all this but
i hope this works for you
>>hi sir,, >> >>whats the key logic to convert floating point number to integer. >> >>is that multiplication of floating point number and 32767 >>if so how does this 32767 has arrived,, >> >>waiting for u r reply,, >>thanking you, >>u r sciencierly >>rashmi >> > >When writing a fixed point code , you should understand what will be the >dynamic range of the variable and based on your processor architecture >decide if you need a single-precision or double-precision representation. > >To answer your question , Where does 32767 comes from? In this case you
are
>talking about a 16-bit processor so the range of signed numbers that
could
>be represented using 16-bit is -32768 to 32767 . (-2^(n-1) to 2^(n-1)-1 >look at 2-bit complement arithmetic for better understanding).This is in >some jargon referred to as Q1.15.(Ex:0.2 is 6553 in Q1.15, implying 1sign >bit and 15 fractional bits) > >Now not all floating-fixed conversion needs to be multiplied by 32767.
You
>can only use 32767 if your sure if your input |x|<1 . What happens if you >have to convert a number say x= 12.768? Multiplying by 32767 wouldn't fit >in 16 bits. So now u need to figure out how many bits are need for the >integer and allocate the rest for fractional or vice versa. In this
example
>to fit 12 you would need 4 bits, leaving 1 bit for sign you will need a >Q5.11 representation.Not in this representation you could only represent >-16<= to < 16, with 11 bit precision to the fractional part. What happens >when x becomes 17 , in fixed point arithmetic x will be saturated. So you >would need to make a wise decision on the Q representation for the >variable. > >It may look complicated for fixed-point beginners but it a lot of fun to >write fixed point code. >
dear sir,, i understood,, that if the float value is less than 1 then multipling with 32767, will result in the intrger value,, but what if the float value is like -1.7708, 5.884 in u r example of 12.768,, i did not understood the part " figure out how many bits are need for the integer and allocate the rest for fractional or vice versa." plz be bit in detail step by step,, thanking you waiting for u r reply rashmi
> >