python - 复制内部格式 float64 uint64

标签 python numpy uint64

我正在使用 Numpy 和 Python。我需要复制数据,而无需在 np.uint64 和 np.float64 之间进行数字转换,例如1.5 <-> 0x3ff8000000000000 .

我知道 float.hex,但输出格式与 uint64 相差很远:

In [30]: a=1.5
In [31]: float.hex(a)
Out[31]: '0x1.8000000000000p+0'

我还知道其他方式的各种字符串输入例程。

有人可以建议更直接的方法吗?毕竟,它只是简单的复制和类型更改,但 python/numpy 在转换数据方面似乎非常严格。

最佳答案

使用中间数组和 frombuffer 方法将一种数组类型“转换”为另一种数组类型:

>>> v = 1.5
>>> fa = np.array([v], dtype='float64')
>>> ua = np.frombuffer(fa, dtype='uint64')
>>> ua[0]
4609434218613702656      # 0x3ff8000000000000

由于 frombuffer 创建原始缓冲区的 View ,因此即使对于重新解释大型数组中的数据也很有效。

关于python - 复制内部格式 float64 uint64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24646287/

相关文章:

python - 替换为 SymPy 中的多个子表达式

python - Pandas 与 Numpy 索引 : Why this fundamental difference in ordering of indices?

python - 在某些位置切换元素的值

用相同大小的数组替换 nan 子数组的 Pythonic 方法

c++ - 如何与 numeric_limits<int64_t>::min() 进行比较

python - 计算数据帧之间的公共(public)列值 (Python)

python - 如何编写在 Postgres 列中查找单词的 Django 查询?

python - 从 python 中的字符串中删除 LaTeX 字符

c++ - 访问冲突转换为 void* 并返回

c - sprintf 的什么输入会产生 0 个结果?