-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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