Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16456 > unrolled thread
| Started by | kuaile xu <kuaile.xu@gmail.com> |
|---|---|
| First post | 2011-11-30 14:24 -0800 |
| Last post | 2011-11-30 16:49 -0700 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
unpack('>f', b'\x00\x01\x00\x00') kuaile xu <kuaile.xu@gmail.com> - 2011-11-30 14:24 -0800
Re: unpack('>f', b'\x00\x01\x00\x00') Chris Rebert <clp2@rebertia.com> - 2011-11-30 15:02 -0800
Re: unpack('>f', b'\x00\x01\x00\x00') kuaile xu <kuaile.xu@gmail.com> - 2011-11-30 15:25 -0800
Re: unpack('>f', b'\x00\x01\x00\x00') Hrvoje Niksic <hniksic@xemacs.org> - 2011-12-01 11:21 +0100
Re: unpack('>f', b'\x00\x01\x00\x00') Mark Dickinson <mdickinson@enthought.com> - 2011-12-02 08:25 -0800
Re: unpack('>f', b'\x00\x01\x00\x00') Ian Kelly <ian.g.kelly@gmail.com> - 2011-11-30 16:49 -0700
| From | kuaile xu <kuaile.xu@gmail.com> |
|---|---|
| Date | 2011-11-30 14:24 -0800 |
| Subject | unpack('>f', b'\x00\x01\x00\x00') |
| Message-ID | <6b7251ef-3479-412f-8acb-882be1e25633@n35g2000yqf.googlegroups.com> |
Hi:
I am working on a python script that parses mp4 video header. Once of
the field is a 32-bit fixed-point number.
I know that the four bytes are: 00, 01, 00, 00. I have a third party
mp4 parsing program which displays this field's value is:1.0.
However, the struct.unpack gets a value of 0.0.
Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from struct import *
>>> unpack('>f', b'\x00\x01\x00\x00')
(9.183549615799121e-41,)
>>>
In addition, there is another field which is a 16-bit fixed point
number. How do I unpack two bytes into a float?
Thank you very much,
[toc] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-11-30 15:02 -0800 |
| Message-ID | <mailman.3181.1322694176.27778.python-list@python.org> |
| In reply to | #16456 |
On Wed, Nov 30, 2011 at 2:24 PM, kuaile xu <kuaile.xu@gmail.com> wrote:
> Hi:
>
> I am working on a python script that parses mp4 video header. Once of
> the field is a 32-bit fixed-point number.
>
> I know that the four bytes are: 00, 01, 00, 00. I have a third party
> mp4 parsing program which displays this field's value is:1.0.
>
> However, the struct.unpack gets a value of 0.0.
>
> Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit
> (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from struct import *
>>>> unpack('>f', b'\x00\x01\x00\x00')
> (9.183549615799121e-41,)
Floating-point and fixed-point are *separate* number formats with
distinct representations. You cannot expect to correctly (un)pack one
as if it was the other. Similarly, converting between the two formats
can introduce range and/or imprecision error.
C does not have a built-in fixed-point datatype, so the `struct`
module doesn't handle fixed-point numbers directly. You're going to
have to unpack it (or parts of it) as something more raw, and then do
the necessary bit manipulation yourself.
Cheers,
Chris
--
http://rebertia.com
[toc] | [prev] | [next] | [standalone]
| From | kuaile xu <kuaile.xu@gmail.com> |
|---|---|
| Date | 2011-11-30 15:25 -0800 |
| Message-ID | <6628ac4b-f6d2-40b6-b1e6-6b0a4bf4e5e2@c18g2000yqj.googlegroups.com> |
| In reply to | #16461 |
On Nov 30, 6:02 pm, Chris Rebert <c...@rebertia.com> wrote:
> On Wed, Nov 30, 2011 at 2:24 PM, kuaile xu <kuaile...@gmail.com> wrote:
> > Hi:
>
> > I am working on a python script that parses mp4 video header. Once of
> > the field is a 32-bit fixed-point number.
>
> > I know that the four bytes are: 00, 01, 00, 00. I have a third party
> > mp4 parsing program which displays this field's value is:1.0.
>
> > However, the struct.unpack gets a value of 0.0.
>
> > Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit
> > (AMD64)] on win32
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> from struct import *
> >>>> unpack('>f', b'\x00\x01\x00\x00')
> > (9.183549615799121e-41,)
>
> Floating-point and fixed-point are *separate* number formats with
> distinct representations. You cannot expect to correctly (un)pack one
> as if it was the other. Similarly, converting between the two formats
> can introduce range and/or imprecision error.
>
> C does not have a built-in fixed-point datatype, so the `struct`
> module doesn't handle fixed-point numbers directly. You're going to
> have to unpack it (or parts of it) as something more raw, and then do
> the necessary bit manipulation yourself.
>
> Cheers,
> Chris
> --http://rebertia.com
>
>
Thanks a lot.
[toc] | [prev] | [next] | [standalone]
| From | Hrvoje Niksic <hniksic@xemacs.org> |
|---|---|
| Date | 2011-12-01 11:21 +0100 |
| Message-ID | <87obvsbnja.fsf@xemacs.org> |
| In reply to | #16461 |
Chris Rebert <clp2@rebertia.com> writes: > C does not have a built-in fixed-point datatype, so the `struct` > module doesn't handle fixed-point numbers directly. The built-in decimal module supports fixed-point arithmetic, but the struct module doesn't know about it. A bug report (or patch) by someone who works with binary representations of fixed-point would be a good start to improve it.
[toc] | [prev] | [next] | [standalone]
| From | Mark Dickinson <mdickinson@enthought.com> |
|---|---|
| Date | 2011-12-02 08:25 -0800 |
| Message-ID | <1bf12228-a5bc-4f1f-8928-3bc2274fdb34@gl2g2000vbb.googlegroups.com> |
| In reply to | #16487 |
On Dec 1, 10:21 am, Hrvoje Niksic <hnik...@xemacs.org> wrote: > Chris Rebert <c...@rebertia.com> writes: > > C does not have a built-in fixed-point datatype, so the `struct` > > module doesn't handle fixed-point numbers directly. > > The built-in decimal module supports fixed-point arithmetic, but the > struct module doesn't know about it. A bug report (or patch) by someone > who works with binary representations of fixed-point would be a good > start to improve it. Not really: the decimal module is for floating-point, not fixed- point, though its semantics for significant trailing zeros can help give the appearance of fixed-point arithmetic for simple operations (addition, subtraction) that stay within suitable bounds. And decimal fixed-point isn't so much use for a binary fixed-point format, anyway. -- Mark
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-11-30 16:49 -0700 |
| Message-ID | <mailman.3184.1322697025.27778.python-list@python.org> |
| In reply to | #16456 |
On Wed, Nov 30, 2011 at 3:24 PM, kuaile xu <kuaile.xu@gmail.com> wrote:
> Hi:
>
> I am working on a python script that parses mp4 video header. Once of
> the field is a 32-bit fixed-point number.
>
> I know that the four bytes are: 00, 01, 00, 00. I have a third party
> mp4 parsing program which displays this field's value is:1.0.
>
> However, the struct.unpack gets a value of 0.0.
>
> Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit
> (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from struct import *
>>>> unpack('>f', b'\x00\x01\x00\x00')
> (9.183549615799121e-41,)
>>>>
Like Chris said, you can't unpack it as a float. Assuming that's Q16
fixed-point, what you can do is unpack it as an int and then multiply
by 2.0 ** -16 to get the corresponding float value. There should be
no loss of precision converting to a float since floats store 52 bits
of precision, but be aware that you could have overflow or loss of
precision when converting in the opposite direction.
> In addition, there is another field which is a 16-bit fixed point
> number. How do I unpack two bytes into a float?
Same as above, just change the multiplier to match the scale.
Cheers,
Ian
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web