Skip to content

Instantly share code, notes, and snippets.

@nutbread
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nutbread/62f9ed5cb8b0a124d9b3 to your computer and use it in GitHub Desktop.
Save nutbread/62f9ed5cb8b0a124d9b3 to your computer and use it in GitHub Desktop.
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