try this in blender python console:
lst = [1, 0.7]
0.7 in lst
output: True
vec = Vector ((1, 0.7))
0.7 in vec
output: False
vec
Vector((1.0, 0.699999988079071))
try this in blender python console:
lst = [1, 0.7]
0.7 in lst
output: True
vec = Vector ((1, 0.7))
0.7 in vec
output: False
vec
Vector((1.0, 0.699999988079071))
Off the top of my head, Python uses 64-bit floating point numbers out of the box. Vector however uses 32-bit float point numbers as far as I remember.
You may experience this issue elsewhere as well if you use in
with floats because of binary precision:
>>> (0.1 + 0.2) in [0.3]
False
You should not use âinâ with floats in general in Python. Use some other method for searching taking the precision into account.
Using any sort of equality (==) test with floating point numbers is a poor (not robust) software design choice.
For more dependable results, it is better to determine an appropriate âepsilonâ (Δ)(acceptable error range), and do a range test.
poor test:
x == y
better test:
Δ = 1e-6
x > (y - Δ) and x < (y + Δ)
see Geometry Nodesâ âCompareâ node in âEqualâ mode for this in operation:
64-bit âdoubleâ floating point numbers (as in Python) are more forgiving, but blender is hard-coded to use 32-bit âsingleâ floating point numbers â since available memory is often at a premium with large models. A conscious design choice of bigger models instead of more precise models. Blender was designed for Art, not for numerically accurate scientific results.
Also, the examples (7/10 and 3/10) are both values that are inexpressible in binary. No matter how many bits of mantissa your floating point numbers contained, they could Never EQUAL the quantities 7/10 and 3/10.
Such is life in the Digital Age.
Thanks.
It is good to know about the epsilon method.
I just suspect it is somehow slower.
,
or is it worth, to use numpy vectors instead ?
it depends ⊠?
it also appeared to me, that (physics) collision simulation (bullet) is not very accurate in blender,
it may be related or not,
but these FP errors can add over many calculations.
Start here
and work your way outward.
Part of the âScienceâ in âComputer Scienceâ to identify problems like you are noticing, and to design / implement / validate robust algorithms to deal with them.
Part of the âEngineeringâ in âSoftware Engineeringâ is to trade-off accuracy / reliability / speed / memory-requirements to provide cost-effective software to solve a given problem.
âRisk / Benefit Analysisâ can assist to allocate time & resources in solving any given problem.
32-bit or 64-bit, floating point error still there (lower but still there) and â==â operator does not works.