Find unique elements of floating point array in numpy (with comparison using a delta value)

Another possibility is to just round to the nearest desirable tolerance: np.unique(a.round(decimals=4)) where a is your original array. Edit: Just to note that my solution and @unutbu’s are nearly identical speed-wise (mine is maybe 5% faster) according to my timings, so either is a good solution. Edit #2: This is meant to address Paul’s concern. … Read more

How to produce a NaN float in c?

Using floating point numbers, 0.0 / 0.0 isn’t a “divide by zero” error; it results in NaN. This C program prints -nan: #include <stdio.h> int main() { float x = 0.0 / 0.0; printf(“%f\n”, x); return 0; } In terms what NaN looks like to the computer, two “invalid” numbers are reserved for “signaling” and … Read more

gcc -O0 still optimizes out “unused” code that should raise an FP exception. Is there a compile flag to change that?

Why does gcc not emit the specified instruction? A compiler produces code that must have the observable behavior specified by the Standard. Anything that is not observable can be changed (and optimized) at will, as it does not change the behavior of the program (as specified). How can you beat it into submission? The trick … Read more

What is the difference between std::min/std::max and fmin/fmax?

fmin and fmax are specifically for use with floating point numbers (hence the “f”). If you use it for ints, you may suffer performance or precision losses due to conversion, function call overhead, etc. depending on your compiler/platform. std::min and std::max are template functions (defined in header <algorithm>) which work on any type with a … Read more