浮点数的二进制表示 [英] Binary representation of floating point numbers

查看:92
本文介绍了浮点数的二进制表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在使用python使用PyVisa运行一些实验室设备。当我从设备读取一个

值列表时,其中一个字段是32位的

标志,但该值作为浮点数返回,

ASCII格式,或纯二进制格式。在任何一种情况下,因为我正在使用PyVisa,

它将数字转换为单个精度浮点数,并且那是'

我必须使用的。


问题是如何从这个浮点恢复比特

值以便我可以读取此处所示的标志?


此外,它有点(或大?)端序。无论如何...我如何操纵

的结尾?


谢谢

Michael

Hi,

I''m using python to run some lab equipment using PyVisa. When I read a
list of values from the equipment, one of the fields is 32 bits of
flags, but the value is returned as a floating point number, either in
ASCII format, or pure binary. In either case, since I''m using PyVisa,
it converts the number to a single precision floating point, and that''s
what I have to work with.

The question is how do I recover the bits out of this floating point
value so I can read the flags represented here?

Also, it''s little (or big?) endian. Whatever... how do I manipulate
the endianness?

thanks
Michael

推荐答案

2005-12-06, 63 ***** **@sneakemail.com < 63 ******* @ sneakemail.com>写道:
On 2005-12-06, 63*******@sneakemail.com <63*******@sneakemail.com> wrote:
我正在使用python运行一些使用PyVisa的实验室设备。当我从设备读取一个值列表时,其中一个字段是32位的
标志,但该值以ASCII格式返回为浮点数,或者纯二进制。


什么?这没有任何意义。如果它是32位的标志,

则它不是纯二进制中的浮点数。


我想你可以假装32位块是一个浮点数和

打印结果值,但那只是疯了。并且它不会工作。
不会工作。对于某些位模式,没有办法从

ASCII返回到相同的位模式。 IOW,位模式和字符串表示之间没有1:1

的映射,所以

如果这就是你所拥有的,那你就是'重新拧紧。

在任何一种情况下,由于我使用PyVisa,它将数字转换为单个精度浮点数,这就是我所拥有的
跟...共事。


听起来好像PyVisa被打破了,因为当它不在时,它会把它当作一个

浮动。

问题是如何从这个漂浮的点值中恢复这些位,以便我可以读取此处所示的标志?


使用struct模块。

此外,它还有一点(或大?)端序。无论如何...我如何操纵字节序?
I''m using python to run some lab equipment using PyVisa. When I read a
list of values from the equipment, one of the fields is 32 bits of
flags, but the value is returned as a floating point number, either in
ASCII format, or pure binary.
Wha? That doesn''t make any sense. If it''s 32 bits of flags,
then it''s not a floating point number "in pure binary".

I suppose you could pretend the 32 bit chunk is a float and
print the resulting value, but that''s just insane. And it
won''t work. For some bit patterns, there''s no way to go from
ASCII back to that same bit pattern. IOW, there is not a 1:1
mapping between bit patterns and the string representation, so
if that''s what you''ve got, you''re screwed.
In either case, since I''m using PyVisa, it converts the number
to a single precision floating point, and that''s what I have
to work with.
It sounds like PyVisa is broken if it''s treating someting as a
float when it isn''t.
The question is how do I recover the bits out of this floating
point value so I can read the flags represented here?
Use the struct module.
Also, it''s little (or big?) endian. Whatever... how do I
manipulate the endianness?




再次,结构模块。


- -

格兰特爱德华兹格兰特哇! ......我在INSTRUCTIONS上看了

...

visi.com



Again, the struct module.

--
Grant Edwards grante Yow! ... I have read the
at INSTRUCTIONS...
visi.com


63 ******* @ sneakemail.com 写道:

我我正在使用python使用PyVisa运行一些实验室设备。当我从设备读取一个值列表时,其中一个字段是32位的
标志,但该值以ASCII格式返回为浮点数,或者纯二进制。


由* * *返回的值你的设备没有返回浮动

点数,它返回字节。这些可以用任何方式解释。你要做的就是把这些字节放到

中,让你读出它的内容。你如何做到这一点取决于你获得字节的方式是多少?

在任何一种情况下,由于我使用PyVisa,
它转换了编号为单精度浮点数,这就是我必须使用的内容。


如果转换是微不足道的,那么您感兴趣的

信息很可能会被它破坏。

问题是如何从这个浮点值中恢复这些值,以便我可以读取这里表示的标志?


我不确定这在Python中是否可行。我不确定我是否希望*它可以用Python来实现
。在C中,你使用一个演员。

此外,它是小的(或大?)端。无论如何...我如何操纵字节序?
Hi,
I''m using python to run some lab equipment using PyVisa. When I read a
list of values from the equipment, one of the fields is 32 bits of
flags, but the value is returned as a floating point number, either in
ASCII format, or pure binary.
Value returned by *what*? Your equipment doesn''t return floating
point numbers, it returns bytes. Those can be interpreted in any
number of ways. What you have to do is get those bytes into something
that lets you read the bits out of it. How you do that depends on how
you''re getting the bytes.
In either case, since I''m using PyVisa,
it converts the number to a single precision floating point, and that''s
what I have to work with.
If the conversion is anything but trivial, there''s a fair chance the
information you''re interested is destroyed by it.
The question is how do I recover the bits out of this floating point
value so I can read the flags represented here?
I''m not sure this is doable in Python. I''m not sure I *want* it to be
doable in Python. In C, you use a cast.
Also, it''s little (or big?) endian. Whatever... how do I manipulate
the endianness?




在大多数处理器上,你不是。如果可以的话,这样做会导致OS

在大多数情况下戛然而止。我不知道是否有人

仍在构建将底层平台视为上下文的盒子

你要求的学位。


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



On most processors, you don''t. If you can, doing so will cause the OS
to come to a screeching halt on most of them. I don''t know if anyone
still building boxes that treats the underlying platform as context to
the degree you''re asking for here.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


您好,

$ b $好的,让我更简洁一点。实验室设备有两种格式

模式,ascii和float。


在ascii模式下,它返回表示数值的字符串,所以

例如3.14以3.14的形式返回。 PyVisa,当设置为读取ascii模式时,

将使用visa.read_values()将这些字符串转换为float。 (很容易

足够自己也可以使用visa.read(),split()和eval())。


在浮动模式下,仪器在浮点数的情况下返回一个比特序列

正好是ieee754数字,或者在标志的情况下只返回

中的标志。 PyVisa,当设置为浮动模式时,会将所有内容转换为浮动,因为它不知道apriori返回的

字段之一实际上是用作二进制标志。


其次,我必须设置仪器返回小的

endian中的位才能正确读取(我可以将PyVisa设置为交换

endianness for me)。我可能需要操纵它。


实际上现在我读了很混乱的手册,看起来好像

标志以十进制数字形式返回,但是不清楚如何以ascii或float模式返回这个

。在任何情况下,我认为我将需要操纵本机。数字到二进制表示。看起来很像我应该弄清楚结构模块...


Michael

Hi,

okay, let me be more concise. The lab equipment has two formatting
modes, ascii, and float.

In ascii mode, it returns strings that represent the numeric value, so
e.g. 3.14 is returned as ''3.14''. PyVisa, when set to read ascii mode,
will convert these strings to float with "visa.read_values()". (easy
enough to do myself too with visa.read(), split(), and eval()).

In float mode, the instrument returns a sequence of bits that are
exactly the ieee754 number in the case of floats, or just the flags in
the case of flags. PyVisa, when set to float mode, will convert
everything to float, because it is unaware apriori that one of the
fields returned is actually intended to be used as binary flags.

Secondarily, I had to set the instrument to return the bits in small
endian for it to read properly (I could aternately set PyVisa to swap
endianness for me). I may need to manipulate this.

Actually now that I read the very confusing manual, it looks like maybe
the flags is returned as a decimal number, but it''s not clear how this
is returned in either ascii or float mode. In any case, I think I will
need to manipulate "native" numbers into binary representation. Looks
like I should figure out the struct module...

Michael


这篇关于浮点数的二进制表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆