简体   繁体   English

将大整数转换为在python中浮动

[英]convert large integer to float in python

an API that I cannot influence is saving certain floating point numbers as contiguousarray with 64 bit, so that a value of -0.30273306 is saved as -4624246862885421056. 我无法影响的API将某些浮点数保存为具有64位的contiguousarray,因此,-0.30273306的值保存为-4624246862885421056。

Now how can I retrieve the original value from this large integer? 现在如何从这个大整数中检索原始值?

You can use struct module. 您可以使用struct模块。 Firstly we pack i as long long (8 bytes signed integer). 首先,我们将i打包为long long(8个字节的有符号整数)。 Then we unpack it as double (8-byte floating point number). 然后,将其解压缩为双精度(8字节浮点数)。

import struct

i = -4624246862885421056
f = struct.unpack("d", struct.pack("q", i))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM