Note: blender python vectors are inaccurate

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.

1 Like

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.

1 Like

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:
Screenshot at 2024-05-04 02-25-45

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. :face_with_diagonal_mouth:

4 Likes

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 
 ?

1 Like

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.

1 Like