python - multiprocessing.Value 无法正确存储 float

标签 python multiprocessing python-multiprocessing

我尝试将 float 分配给 multiprocessing.Value 共享 ctype,如下所示:

import multiprocessing
import random

test_float = multiprocessing.Value('f', 0)
i = random.randint(1,10000)/(random.randint(1,10000))
test_float.value = i
print("i: type = {}, value = {}".format(type(i), i))
print("test_float: type = {}, value = {}".format(type(test_float.value), test_float.value))
print("i == test_float: {}".format(i == test_float.value))

但是,multiprocessing.Value 中存储的 float 是 != 输入 float :

>>> i: type = <class 'float'>, value = 1.480021216407355
>>> test_float: type = <class 'float'>, value = 1.4800212383270264
>>> i == test_float: False

这里有什么问题吗?

编辑: 找到了解决方案(参见答案)但是,我不明白为什么“ double ”是这里的正确类型而不是“ float ”。如果有人可以详细说明并包含解决方案,我会将其标记为正确答案。

最佳答案

Python float 是 double float ,或者其他语言所称的double。这就是为什么你需要使用 'd':'f' 与 python 用于 float's 的精度级别不对应。 p>

关于python - multiprocessing.Value 无法正确存储 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51768260/

相关文章:

python - Beautiful Soup 的空元素错误

python - processpoolexecutor 子进程突然停止

python - 如何在不处于顶层的情况下解决python多处理的 pickle 错误?

python - 当你使用Process或Pool实现多处理时,python解释器会做什么?

python - 多处理在 Ubuntu 中有效,在 Windows 中无效

python - Numpy,从文件中读取,没有分隔符但固定模式

python - 如何前瞻性地使用 Pandas rolling_* 函数

python - 加载 MySQLdb 模块时出错 Mountain Lion

python - python中的多线程系统调用

python - 看来 multiprocessing.pool 两次接受相同的参数