Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test if an integer value can be converted to a float and retain its exact representation
import numpy;
def is_exact(intval):
f1 = float(intval);
f1_epsilon = numpy.nextafter(f1, 0);
f_diff = f1 - f1_epsilon;
return (f_diff < 1.0 or long(f1_epsilon) + long(f_diff) == intval);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment