DSPRelated.com
Forums

integer to float

Started by Markus September 3, 2005
Hello,

I am new in C programming. I currently start to work with an TI dsp
(C6713). My question is whats happen when I cast an 32Bit integer value
into an 32bit float value.

Could anybody help me?

thanks markus

Markus wrote:
> Hello, > > I am new in C programming. I currently start to work with an TI dsp > (C6713). My question is whats happen when I cast an 32Bit integer value > into an 32bit float value.
The binary representation of the integer changes. Depending on the numerical value of the integer, some round-off error could be introduced, To see the binary representations, try something like long int i; float j; i = 27; j = (float) i; printf("Integer bit pattern: %x\n",&i); printf("Float bit pattern: %x\n\n",&j); printf("Numerical value of float: %4.12g"); Rune
"Rune Allnor" <allnor@tele.ntnu.no> wrote in message 
news:1125748473.248899.17120@g14g2000cwa.googlegroups.com...

> To see the binary representations, try something like > > long int i; > float j; > > i = 27; > j = (float) i; > > printf("Integer bit pattern: %x\n",&i); > printf("Float bit pattern: %x\n\n",&j);
I'm afraid that the above two statements will, at best, print out the hex representation of the *addresses* of i and j, not their values.
Markus wrote:

> Hello, > > I am new in C programming. I currently start to work with an TI dsp > (C6713). My question is whats happen when I cast an 32Bit integer value > into an 32bit float value. > > Could anybody help me? > > thanks markus >
During run time the computer will convert the integer to a float with the same mathematical value. The binary representation will change significantly. I like "C: A Reference Manual" by Harbison & Steele -- it goes into great depth on this sort of thing, which you need sometimes. You may also want to dig up the specification for IEEE floating point format. Not only is it representative of most floating point formats, it's also the format used in nearly all processors & programs these days. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Rune Allnor wrote:

> Markus wrote: > >>Hello, >> >>I am new in C programming. I currently start to work with an TI dsp >>(C6713). My question is whats happen when I cast an 32Bit integer value >>into an 32bit float value. > > > The binary representation of the integer changes. Depending on > the numerical value of the integer, some round-off error could > be introduced, > > To see the binary representations, try something like > > long int i; > float j; > > i = 27; > j = (float) i; > > printf("Integer bit pattern: %x\n",&i); > printf("Float bit pattern: %x\n\n",&j); > printf("Numerical value of float: %4.12g"); > > Rune >
I think you mean printf("Integer bit pattern: %x\n", i); printf("Float bit pattern: %x\n", *((long int *)&j)); printf("Numerical value of float: %4.12g\n", j); note that: * I haven't tested this code either * it assumes that sizeof(long int) == sizeof(float) -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Tim Wescott wrote:
> Markus wrote: > >> Hello, >> >> I am new in C programming. I currently start to work with an TI dsp >> (C6713). My question is whats happen when I cast an 32Bit integer value >> into an 32bit float value. >> >> Could anybody help me? >> >> thanks markus >> > During run time the computer will convert the integer to a float with > the same mathematical value. The binary representation will change > significantly. > > I like "C: A Reference Manual" by Harbison & Steele -- it goes into > great depth on this sort of thing, which you need sometimes. > > You may also want to dig up the specification for IEEE floating point > format. Not only is it representative of most floating point formats, > it's also the format used in nearly all processors & programs these days.
TI often uses a proprietary floating-point format. Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
"Rune Allnor" <allnor@tele.ntnu.no> writes:
> Markus wrote: > > > > I am new in C programming. I currently start to work with an TI dsp > > (C6713). My question is whats happen when I cast an 32Bit integer value > > into an 32bit float value. > > The binary representation of the integer changes. Depending on > the numerical value of the integer, some round-off error could > be introduced, > > To see the binary representations, try something like > > long int i; > float j; > > i = 27; > j = (float) i; > > printf("Integer bit pattern: %x\n",&i); > printf("Float bit pattern: %x\n\n",&j); > printf("Numerical value of float: %4.12g");
and removing the coding errors, try something like: typedef union { long L; float F;} FL_t; long int i; FL_t k; i = 27; k.F = i; printf("Integer bit pattern: %x\n", i); printf("Float bit pattern: %x\n", k.L); printf("Numerical value of float: %4.12g\n", k.F);
"Jerry Avins" <jya@ieee.org> wrote in message 
news:BeGdnZ2dnZ3YOcyAnZ2dnctUhN6dnZ2dRVn-zJ2dnZ0@rcn.net...
> Tim Wescott wrote: >> Markus wrote: >> >>> Hello, >>> >>> I am new in C programming. I currently start to work with an TI dsp >>> (C6713). My question is whats happen when I cast an 32Bit integer value >>> into an 32bit float value. >>> >>> Could anybody help me? >>> >>> thanks markus >>> >> During run time the computer will convert the integer to a float with the >> same mathematical value. The binary representation will change >> significantly. >> >> I like "C: A Reference Manual" by Harbison & Steele -- it goes into great >> depth on this sort of thing, which you need sometimes. >> >> You may also want to dig up the specification for IEEE floating point >> format. Not only is it representative of most floating point formats, >> it's also the format used in nearly all processors & programs these days. > > TI often uses a proprietary floating-point format. > > Jerry > -- > Engineering is the art of making what you want from things you can get. > &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
That's no longer the case. The old c3x processors used a proprietary format but all of the c67x processors fully comply with the IEEE standard and support both single and double precision floating point. Brad
Tim Wescott <tim@seemywebsite.com> writes:
> [...] > During run time the computer will convert the integer to a float with > the same mathematical value.
Tim, That can't be the case for a 32-bit float, no matter what the format. You will lose some precision in the mantissa, as Rune has already pointed out. -- % Randy Yates % "Though you ride on the wheels of tomorrow, %% Fuquay-Varina, NC % you still wander the fields of your %%% 919-577-9882 % sorrow." %%%% <yates@ieee.org> % '21st Century Man', *Time*, ELO http://home.earthlink.net/~yatescr
"Randy Yates" <yates@ieee.org> wrote in message 
news:acitstkk.fsf@ieee.org...
> Tim Wescott <tim@seemywebsite.com> writes: >> [...] >> During run time the computer will convert the integer to >> a float with >> the same mathematical value. > > Tim, > > That can't be the case for a 32-bit float, no matter what > the format. > You will lose some precision in the mantissa, as Rune has > already > pointed out.
So long as the number of bits in the integer is less than or equal to the number of bits in the mantissa, you will lose no precision in the conversion.