DSPRelated.com
Forums

integer to float

Started by Markus September 3, 2005
"John E. Hadstate" <jh113355@hotmail.com> writes:

> "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.
Right. And last time I checked, the number of bits in the mantissa of a 32-bit float is less than 32. -- % Randy Yates % "With time with what you've learned, %% Fuquay-Varina, NC % they'll kiss the ground you walk %%% 919-577-9882 % upon." %%%% <yates@ieee.org> % '21st Century Man', *Time*, ELO http://home.earthlink.net/~yatescr
"Randy Yates" <yates@ieee.org> wrote in message 
news:3bolst5e.fsf@ieee.org...
> "John E. Hadstate" <jh113355@hotmail.com> writes: > >> "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. > > Right. And last time I checked, the number of bits in the > mantissa of > a 32-bit float is less than 32.
Yes, but quite a few 32-bit integers do not require more than 24 bits to represent them. None of those integers will suffer any loss of precision when converted to float.
Randy Yates wrote:

> 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.
I missed that detail -- as already pointed out, if the integer has magnitude less than 2^24 (for IEEE single-precision) then the conversion will be exact, otherwise it'll be approximate. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
in article YrpSe.7214$e47.6209@bignews1.bellsouth.net, John E. Hadstate at
jh113355@hotmail.com wrote on 09/03/2005 18:29:

> > "Randy Yates" <yates@ieee.org> wrote in message > news:3bolst5e.fsf@ieee.org... >> "John E. Hadstate" <jh113355@hotmail.com> writes: >> >>> "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. >> >> Right. And last time I checked, the number of bits in the >> mantissa of >> a 32-bit float is less than 32. > > Yes, but quite a few 32-bit integers do not require more > than 24 bits to represent them.
actually, it's 25 bits.
> None of those integers will > suffer any loss of precision when converted to float.
how does one, a postori know if any bits *did* get truncated? i guess, if we already say in advance that these are integer values, then you're right, John. any integer from -2^24 to +2^24 can be represented exactly in 32-bit IEEE 748. that's a millismidgen more than a 25-bit 2's compliment int. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
"John E. Hadstate" <jh113355@hotmail.com> writes:
> [...] > Yes, but quite a few 32-bit integers do not require more > than 24 bits to represent them. None of those integers will > suffer any loss of precision when converted to float.
John, The logic you are using is specious. By definition of the problem we must assume that the entire range of the 32-bit integer is being utilized. -- % Randy Yates % "Midnight, on the water... %% Fuquay-Varina, NC % I saw... the ocean's daughter." %%% 919-577-9882 % 'Can't Get It Out Of My Head' %%%% <yates@ieee.org> % *El Dorado*, Electric Light Orchestra http://home.earthlink.net/~yatescr
"Randy Yates" <yates@ieee.org> wrote in message 
news:u0h1rd0c.fsf@ieee.org...
> "John E. Hadstate" <jh113355@hotmail.com> writes: >> [...] >> Yes, but quite a few 32-bit integers do not require more >> than 24 bits to represent them. None of those integers >> will >> suffer any loss of precision when converted to float. > > John, > > The logic you are using is specious. By definition of the > problem we must assume that the entire range of the 32-bit > integer is being utilized.
My logic is impeccable and we do not have to assume any such thing. For example, if we know that our 32-bit integers came from a 14-bit A/D, our numbers will not require any more than 14 bits and we will not suffer any loss of precision when we convert to float. This is not an uncommon scenario.
"robert bristow-johnson" <rbj@audioimagination.com> wrote in 
message news:BF3F9DFB.A246%rbj@audioimagination.com...
> in article YrpSe.7214$e47.6209@bignews1.bellsouth.net, > John E. Hadstate at > jh113355@hotmail.com wrote on 09/03/2005 18:29: > >> >> Yes, but quite a few 32-bit integers do not require more >> than 24 bits to represent them. > > actually, it's 25 bits. > >> None of those integers will >> suffer any loss of precision when converted to float. > > how does one, a postori know if any bits *did* get > truncated?
If the magnitude of the floating point number is greater than 16,777,216.0 than you will have suffered some loss of precision. On the other hand, you might have suffered a loss of precision and still not have lost accuracy for that particular number. Consider what happens when you convert this 32-bit binary integer (commas every 8 bits for clarity): 00110110,10101111,01001100,00000000 to a float. You will still have the 24 bits you need to represent the upper part of the number, so the converted number will be represented accurately, even though the converted value is less precise.
"John E. Hadstate" <jh113355@hotmail.com> writes:

> "Randy Yates" <yates@ieee.org> wrote in message > news:u0h1rd0c.fsf@ieee.org... >> "John E. Hadstate" <jh113355@hotmail.com> writes: >>> [...] >>> Yes, but quite a few 32-bit integers do not require more >>> than 24 bits to represent them. None of those integers >>> will >>> suffer any loss of precision when converted to float. >> >> John, >> >> The logic you are using is specious. By definition of the >> problem we must assume that the entire range of the 32-bit >> integer is being utilized. > > My logic is impeccable and we do not have to assume any such > thing. For example, if we know that our 32-bit integers > came from a 14-bit A/D, our numbers will not require any > more than 14 bits and we will not suffer any loss of > precision when we convert to float. This is not an uncommon > scenario.
If we don't make the assumption that the integer size is relevent to the question, then we can fit any size integer into a 32-bit float. -- % Randy Yates % "I met someone who looks alot like you, %% Fuquay-Varina, NC % she does the things you do, %%% 919-577-9882 % but she is an IBM." %%%% <yates@ieee.org> % 'Yours Truly, 2095', *Time*, ELO http://home.earthlink.net/~yatescr
"Randy Yates" <yates@ieee.org> wrote in message 
news:3bolplpo.fsf@ieee.org...
> "John E. Hadstate" <jh113355@hotmail.com> writes: > >> "Randy Yates" <yates@ieee.org> wrote in message >> news:u0h1rd0c.fsf@ieee.org... >>> "John E. Hadstate" <jh113355@hotmail.com> writes: >>>> [...] >>>> Yes, but quite a few 32-bit integers do not require >>>> more >>>> than 24 bits to represent them. None of those integers >>>> will >>>> suffer any loss of precision when converted to float. >>> >>> John, >>> >>> The logic you are using is specious. By definition of >>> the >>> problem we must assume that the entire range of the >>> 32-bit >>> integer is being utilized. >> >> My logic is impeccable and we do not have to assume any >> such >> thing. For example, if we know that our 32-bit integers >> came from a 14-bit A/D, our numbers will not require any >> more than 14 bits and we will not suffer any loss of >> precision when we convert to float. This is not an >> uncommon >> scenario. > > If we don't make the assumption that the integer size is > relevent to > the question, then we can fit any size integer into a > 32-bit float.
You can fit some integers into a 32-bit float that are far larger than you can fit into a 32-bit integer. For example: 2^63 is an integer that will be accurately and exactly represented by the 32-bit float as 0.5*(2^64). There are quite a few other examples.