[Bf-python] Vector Comparison Issue

Gilbert, Joseph jgilbert at tigr.ORG
Tue Sep 20 16:48:21 CEST 2005


All of the mathutils classes have floating-point precision not
double-point precision. "The 24 bits (including the hidden bit) of
mantissa in a 32-bit floating-point number represent approximately 7
significant decimal digits." Because PyFloats are really doubles - when
you print vec.x your casting a C float to a PyFloat (which is a C
double) and stuff after the significant digits is garbage. So what makes
these 2 numbers not equal is the non-significant digits in the
comparison.

The best way around this currently is to determine a limit of comparison
using epsilon as Mr. Merritt as stated. (pick a small number epsilon and
do a comparison if (A+eps > B AND B-eps < B) 

Of course the other way of doing things is to force all the mathutils
classes to use double precision. We haven't done this is the past as the
original classes (as much else in BPy) uses floats to represent thing
such as vert position, etc. It may make sense from the fact that PyFloat
are really C doubles. 

Comparing floating point values directly is probably not a good idea.
However, we could move more items into the realm of double-point
precision if peeps thinks it's necessary.


-----Original Message-----
From: bf-python-bounces at projects.blender.org
[mailto:bf-python-bounces at projects.blender.org] On Behalf Of Jonathan
Merritt
Sent: Tuesday, September 20, 2005 10:12 AM
To: cbarton at metavr.com; Blender Foundation Python list
Subject: Re: [Bf-python] Vector Comparison Issue

Campbell Barton wrote:

> from Blender import Mathutils
> f = 22.0 / 7.0
> vec = Mathutils.Vector(f,0,0)
> print vec.x == f
> ...False


I suppose it's an example of the general rule: "Never do floating point 
equality comparisons!"

The general way around this is to check equality up to a reasonable 
limit: epsilon.  When epsilon is a small number greater than zero:
    equal = ((vec.x + epsilon) >= f) and ((vec.x - epsilon) <= f)

Why do you want to check for floating-point equality to begin with 
though?  Can't you establish "logical equality", between objects or maps

of objects, rather than "numerical equality"?

Jonathan Merritt.

_______________________________________________
Bf-python mailing list
Bf-python at projects.blender.org
http://projects.blender.org/mailman/listinfo/bf-python



More information about the Bf-python mailing list