在Python中将double转换为float [英] convert double to float in Python

查看:1162
本文介绍了在Python中将double转换为float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python程序中,我有以下两个值:

In a Python program, I have these two values:

v1 = 0.00582811585976
v2 = 0.00582811608911

我的假设是v1是64位浮点值,而v2是v1转换为32位浮点值。我如何验证

详细信息:

第一个值来自硬件板,其计算得出的64-位精度。该板将值发送到PC,但也应将其转换为32位精度并将其发送到另一块板,后者再将其发送到PC。我只想确认这确实在发生,我所拥有的只是两个大数字数组。

Details:
The first value comes from a hardware board that calculates with 64-bits precision. The board sends the value to a PC, but it should also convert the value to 32-bits precision and send that to another board, which in turn sends it to a PC. I just want to verify that this is really happening and all I have are two large arrays of numbers.

推荐答案

您可以使用结构模块,以使用数字表示形式:

You can use the struct module to play with numerical representations:

import struct

>>> struct.unpack("f", struct.pack("f", 0.00582811585976))
(0.005828116089105606,)

这篇关于在Python中将double转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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