473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

struct, IEEE-754 and internal representation

ej

I ran into something today I don't quite understand and I don't know all the
nitty gritty details about how Python stores and handles data internally.
(I think) I understand why, when you type in certain floating point values,
Python doesn't display exactly what you typed (because not all decimal
numbers are exactly representable in binary, and Python shows you the full
precision of what is representable). For example:
0.9 0.90000000000000002

and
148.73 148.72999999999999

So, I think I've got a pretty good handle on what the struct module is all
about. Let's take that number, 148.73, and use struct functions to look at
the raw bit pattern of what would be in a 32-bit register using IEEE754
float representation:
hex(unpack('L', pack('f', x))[0]) '0x4314BAE1L'

That is, the four bytes representing this are 0x43, 0x14, 0xBA, 0xE1

Now let's go back the other way, starting with this 32 bit representation,
and turn it back into a float:
unpack('>f', pack('BBBB', 0x43, 0x14, 0xBA, 0xE1))[0] 148.72999572753906
Hmmmm... Close, but I seem to be losing more the I would expect here. I
initially thought I should be able to get back to at least what python
previously displayed: 148.72999999999999

I know there are 23 bits of mantissa in an IEEE-754, with a free '1'...
hex(14872999)

'0xe2f1a7'

Looks like it takes 6 * 4 = 24 bits to represent that as an int....

I am starting to think my expectation is wrong...

If that's true, then I guess I am confused why Python is displaying
148.72999572753906 when you unpack the 4 bytes, implying a lot more
precision that was available in the original 32-bits? Python is doing
64-bit floating point here? I'm obviously not understanding something...
help?

-ej


Nov 9 '05 #1
4 3125
On 2005-11-09, ej <> wrote:
If that's true, then I guess I am confused why Python is displaying
148.72999572753906 when you unpack the 4 bytes, implying a lot more
precision that was available in the original 32-bits? Python is doing
64-bit floating point here?


Yes. C-Python "float" objects are of the C type "double" and
use 64-bit IEEE-754 representation on all the common platforms
I know about.

--
Grant Edwards grante Yow! .. or were you
at driving the PONTIAC that
visi.com HONKED at me in MIAMI last
Tuesday?
Nov 9 '05 #2
ej wrote:
If that's true, then I guess I am confused why Python is displaying
148.72999572753906 when you unpack the 4 bytes, implying a lot more
precision that was available in the original 32-bits? Python is doing
64-bit floating point here? I'm obviously not understanding something...
help?


Yes, Python uses C double precision floats for float objects.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Nov 9 '05 #3
Use 'd' as the format character for 64-bit double precision numbers with struct.
x = 148.73
unpack("<d", pack("<d", x))[0] == x True unpack("<f", pack("<f", x))[0] == x

False

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDcohCJd01MZaTXX0RAgEKAJwNYt7Rb/yeaLle4c2XsbIpAoLVXACghpMo
XpcFtakHKmBkf+H4svGrZ5A=
=dw0q
-----END PGP SIGNATURE-----

Nov 9 '05 #4
ej
Ah! Well! That explains it. I started to suspect that but (obviously) did
not know that. LOL

Thanks for your prompt reply, Grant. :)

-ej
Nov 10 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt Feinstein | last post by:
Using the 'struct' module (Win32, python version 2.4.1)-- The library documentation says that 'no alignment is required for any type'. However, struct.calcsize('fd') gives 16 while...
10
by: Mad Butch | last post by:
void Test() { float fValue = 0.5678f; // Value is 0.567800 double dValue = (double)fValue; // Value is 0.56779998540878 } Is there anyway I can round a float at 6 positions behind the...
2
by: Ingo Nolden | last post by:
Hi, I am not really sure wether there is a better group for this. I already tried a VC group, but they cannot tell me how other stdlibs are doing it. I tested a lot with inf and NaNs. I read...
1
by: GooglePoster | last post by:
Hello, I am looking for a function/utility to read in an IEEE value and convert this to decimal, for testing purposes. Also, I need to convert decimal values to IEEE values to send out on a...
7
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be...
2
by: Michael Yanowitz | last post by:
Hello: I am relatively new to Python and this is my first post on this mailing list. I am confused as to why I am getting size differences in the following cases: >>> print...
42
by: Jeff P. Syverson | last post by:
Can anyone tell me the best way to find out the size of a struct, without using the sizeof function? I'm currently thinking of: struct foo s; int size_of_foo = (char *)&s - (char *)s; but is...
18
by: lovecreatesbea... | last post by:
1. The following code snippet uses minus operation on two pointers to calculate the distance between struct members. This is illegal, right? 2. s1 and s2 are type of the same struct S. Can the...
6
by: satheesh | last post by:
c language can protect the data in scopes private,protected and public(may be) in structure and union?
3
by: Anna | last post by:
Could you please help me? I got a segmentation fault message while trying to assign a pointer = pointer like this: bufferlist=(buffer_t*)buffernew; What's the error by doing this? Here is the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.