[Numpy-discussion] Precision/value change moving from C to Python

Bart Baker bartbkr at gmail.com
Tue Nov 12 06:01:39 EST 2013


Hi all,

First time posting.

I've been working on a class for solving a group of affine models and
decided to drop down to C for a portion of the code for improved
performance.

There are two questions that I've had come up from this.

1) I've written the section of code, which involved a series of fully
populated masked arrays as arguments. There are a few array operations
such as dot products and differences that are taken on the arrays to
generate the final value. I've written versions in both Python/NumPy and
C and now have a verification issue.

The issue is that there are some minor (10^-16) differences in the
values when I do the calculation in C vs Python. The way that I test
these differences is by passing the NumPy arrays to the C extension and
testing for equality in gdb. I'm not relying on any of the NumPy API for
the array operations such as dot product, addition, and subtraction. All
of the arrays I'm treating as two-dimensional in C right now. Once
I figure out this bug, I'm planning on rewriting the computations in
terms of one dimensional arrays.

All of the values that I'm dealing with in C right now are double
precision.

2) Beyond this issue, I also noticed that the values that are equal in
gdb are not eqaul once I return to Python! I pass the same arrays back
from C to Python, test eqaulity by:

npy_array == c_array

and find that almost the entire arrays are non-equal, even though
equality tests in gdb had been true. Is there some obvious reason why
this would be. Is there some sort of implicit value conversion performed
when moving from C back to Python?

I can post some code if need be, but I wanted to get anyone's first
insight if there is anything obvious that I'm not doing right.

I appreciate your help.

Thanks,
Bart



More information about the NumPy-Discussion mailing list