From: John Nagle on 30 Mar 2010 14:14 aditya wrote: > On Mar 30, 10:49 am, Raymond Hettinger <pyt...(a)rcn.com> wrote: >> On Mar 30, 8:13 am, aditya <bluemangrou...(a)gmail.com> wrote: >> >>> To get the decimal representation of a binary number, I can just do >>> this: >>> int('11',2) # returns 3 >>> But decimal binary numbers throw a ValueError: >>> int('1.1',2) # should return 1.5, throws error instead. >>> Is this by design? It seems to me that this is not the correct >>> behavior. >> The int() constructor returns integers. >> So, look to float() for nonintegral values. >> Binary representation isn't supported yet, >> but we do have hex: >> >> >>> float.fromhex('1.8') >> 1.5 >> >> Raymond > > That looks very elegant, thanks! Hex floats are useful because you can get a string representation of the exact value of a binary floating point number. It should always be the case that float.fromhex(float.hex(x)) == x That's not always true of decimal representations, due to rounding problems. Long discussion of this here: "http://bugs.python.org/issue1580" John Nagle
From: Mensanator on 30 Mar 2010 14:03 On Mar 30, 10:49 am, Raymond Hettinger <pyt...(a)rcn.com> wrote: > On Mar 30, 8:13 am, aditya <bluemangrou...(a)gmail.com> wrote: > > > To get the decimal representation of a binary number, I can just do > > this: > > > int('11',2) # returns 3 > > > But decimal binary numbers throw a ValueError: > > > int('1.1',2) # should return 1.5, throws error instead. > > > Is this by design? It seems to me that this is not the correct > > behavior. > > The int() constructor returns integers. > So, look to float() for nonintegral values. > Binary representation isn't supported yet, It is supported in the gmpy module. >>> import gmpy >>> help(gmpy.mpf) Help on builtin function mpf in module gmpy: mpf(...) mpf(n): builds an mpf object with a numeric value n (n may be any Python number, or an mpz, mpq, or mpf object) and a default precision (in bits) depending on the nature of n mpf(n,bits=0): as above, but with the specified number of bits (0 means to use default precision, as above) mpf(s,bits=0,base=10): builds an mpf object from a string s made up of digits in the given base, possibly with fractionpart (with period as a separator) and/or exponentpart (with exponent marker 'e' for base<=10, else '@'). If base=256, s must be a gmpy.mpf portable binary representation as built by the function gmpy.fbinary (and the .binary method of mpf objects). The resulting mpf object is built with a default precision (in bits) if bits is 0 or absent, else with the specified number of bits. >>> gmpy.mpf('1.1',0,2) mpf('1.5e0') > but we do have hex: > > >>> float.fromhex('1.8') > 1.5 > > Raymond
From: Grant Edwards on 30 Mar 2010 14:08 On 20100330, John Nagle <nagle(a)animats.com> wrote: > Hex floats are useful because you can get a string representation of > the exact value of a binary floating point number. It should always > be the case that > > float.fromhex(float.hex(x)) == x Until you try running your program on a machine that represents floats using a radix other than 2,4, or 16. ;) And it works for NaN and Inf too! It would have been nice to have had that 56 years ago when I had to write my own pickle/unpickle methods for floating point values so that inf and nan were portable between Windows and Linux.  Grant Edwards grant.b.edwards Yow! But they went to MARS at around 1953!! gmail.com
From: MRAB on 30 Mar 2010 14:52 John Nagle wrote: > aditya wrote: >> On Mar 30, 10:49 am, Raymond Hettinger <pyt...(a)rcn.com> wrote: >>> On Mar 30, 8:13 am, aditya <bluemangrou...(a)gmail.com> wrote: >>> >>>> To get the decimal representation of a binary number, I can just do >>>> this: >>>> int('11',2) # returns 3 >>>> But decimal binary numbers throw a ValueError: >>>> int('1.1',2) # should return 1.5, throws error instead. >>>> Is this by design? It seems to me that this is not the correct >>>> behavior. >>> The int() constructor returns integers. >>> So, look to float() for nonintegral values. >>> Binary representation isn't supported yet, >>> but we do have hex: >>> >>> >>> float.fromhex('1.8') >>> 1.5 >>> >>> Raymond >> >> That looks very elegant, thanks! > > Hex floats are useful because you can get a string representation > of the exact value of a binary floating point number. It should > always be the case that > > float.fromhex(float.hex(x)) == x > > That's not always true of decimal representations, due to rounding > problems. > Floats have a limited length, unlike ints which are virtually unlimited. > Long discussion of this here: "http://bugs.python.org/issue1580" >
From: Mensanator on 30 Mar 2010 17:05 On Mar 30, 1:52 pm, MRAB <pyt...(a)mrabarnett.plus.com> wrote: > John Nagle wrote: > > aditya wrote: > >> On Mar 30, 10:49 am, Raymond Hettinger <pyt...(a)rcn.com> wrote: > >>> On Mar 30, 8:13 am, aditya <bluemangrou...(a)gmail.com> wrote: > > >>>> To get the decimal representation of a binary number, I can just do > >>>> this: > >>>> int('11',2) # returns 3 > >>>> But decimal binary numbers throw a ValueError: > >>>> int('1.1',2) # should return 1.5, throws error instead. > >>>> Is this by design? It seems to me that this is not the correct > >>>> behavior. > >>> The int() constructor returns integers. > >>> So, look to float() for nonintegral values. > >>> Binary representation isn't supported yet, > >>> but we do have hex: > > >>> >>> float.fromhex('1.8') > >>> 1.5 > > >>> Raymond > > >> That looks very elegant, thanks! > > > Hex floats are useful because you can get a string representation > > of the exact value of a binary floating point number. It should > > always be the case that > > > float.fromhex(float.hex(x)) == x > > > That's not always true of decimal representations, due to rounding > > problems. > > Floats have a limited length, unlike ints which are virtually unlimited. gmpy gives you arbitrary precision floats, also. > > > > > Long discussion of this here: "http://bugs.python.org/issue1580" Hide quoted text  > >  Show quoted text  Hide quoted text  > >  Show quoted text 
First

Prev

Pages: 1 2 3 Prev: It's a fact not lost on the opportunity to see yourself Next: (a==b) ? 'Yes' : 'No' 