Skip to content
Advertisement

multiprocessing.Value doesn’t store float correctly

I try to asign a float to the multiprocessing.Value shared ctype as follows:

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))

However, the float stored in multiprocessing.Value is != the input float:

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

Whats the problem here?

EDIT: Found the solution (see answers) However, I do not understand, why a “double” is the correct type here and not a “float”. If someone can elaborate on that and include the solution, I will mark it as the correct answer.

Advertisement

Answer

Python floats are double-precision floats, or what other languages would call double‘s. That is why you need to use 'd': 'f' does not correspond to the precision level python uses for float‘s

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement