[Bf-python] Vector Comparison Issue
Stephen Swaney
sswaney at centurytel.net
Tue Sep 20 16:20:56 CEST 2005
On Tue, Sep 20, 2005 at 11:47:48PM +1000, Campbell Barton wrote:
> Hi, made a vector/math python program and had a bug that stemed from
> this error.
>
> from Blender import Mathutils
> f = 22.0 / 7.0
> vec = Mathutils.Vector(f,0,0)
> print vec.x == f
> ...False
>
> I think the problem is that Python floats are long and Vectors are just
> normal floats.
> Is there a way around this?
In Python, floats are IEEE double precision floating point
Blenders floats are C floats, which is to say not C double
which is the equivalent of the above.
Printing out the values from the code snippet makes the
difference apparent:
vec.x= 3.14285707474 <type 'float'>
f= 3.14285714286 <type 'float'>
The *real* problem here is that one *NEVER* compares
floating point numbers for equality. Always check if
the difference is less than some tiny, tiny number.
Example:
if math.fabs( vec.x - f ) < some_tiny_number :
You may not like this, but it is a basic fact of life
when dealing with numbers and computers.
--
Stephen Swaney
sswaney at centurytel.net
More information about the Bf-python
mailing list