python - 使用python编写 double 二进制文件

标签 python double-precision

如何使用 Python 编写 double 文件二进制文件?我目前正在执行以下操作,但它为我提供了单精度文件:

#!/usr/bin/env python

import struct

data =[2.3715231753176,9.342983274982732]

output_file = "test.dat"
out_file = open(output_file,"wb")
s = struct.pack('f'*len(data), *data)
out_file.write(s)
out_file.close()

最佳答案

使用d写 double float :

s = struct.pack('d'*len(data), *data)

f 仅为精度。查看Format characters struct 模块文档部分:

For the 'f' and 'd' conversion codes, the packed representation uses the IEEE 754 binary32 (for 'f') or binary64 (for 'd') format, regardless of the floating-point format used by the platform.

关于python - 使用python编写 double 二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16190115/

相关文章:

java - 在 Java 中比较 double 值是否相等。

c++ - 我应该使用 double 还是 float ?

Python 3 : Insertion Sort comparisons counter

Python 2.6 Scapy rdpcap错误

python - 值错误: No JSON object could be decoded

python - 如何从 lambda 函数 ssh 到 EC2 实例

Java Doubles 不擅长数学

javascript - 要求 OPTIONS 的 DOJO AJAX 请求

C++ : sin(M_PI/6) = 0. 5?

c++ - 浮点与浮点文字比较的奇怪输出