Created
May 6, 2013 22:06
-
-
Save jrsmith3/5528627 to your computer and use it in GitHub Desktop.
Round a value to an integer [1-9] times a power of 10. How would you write a test for this method?
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 as np | |
def round_exponential(val): | |
""" | |
Round a value to an integer [1-9] times a power of 10. | |
Example | |
>>> val1 = 7.83126342297e-07 | |
>>> print round_exponential(val1) | |
>>> 8e-07 | |
>>> val2 = -6.9322190125e+13 | |
>>> print round_exponential(val2) | |
>>> -7e+13 | |
""" | |
expt = np.floor(np.log10(np.abs(val))) | |
mant = val / 10**expt | |
return np.round(mant) * 10**expt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd just have it test your few examples.