I try to asign a float to the multiprocessing.Value shared ctype as follows:
JavaScript
x
10
10
1
import multiprocessing
2
import random
3
4
test_float = multiprocessing.Value('f', 0)
5
i = random.randint(1,10000)/(random.randint(1,10000))
6
test_float.value = i
7
print("i: type = {}, value = {}".format(type(i), i))
8
print("test_float: type = {}, value = {}".format(type(test_float.value), test_float.value))
9
print("i == test_float: {}".format(i == test_float.value))
10
However, the float stored in multiprocessing.Value is != the input float:
JavaScript
1
4
1
>>> i: type = <class 'float'>, value = 1.480021216407355
2
>>> test_float: type = <class 'float'>, value = 1.4800212383270264
3
>>> i == test_float: False
4
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