Skip to content

Instantly share code, notes, and snippets.

@jrsmith3
Created May 6, 2013 22:06
Show Gist options
  • Save jrsmith3/5528627 to your computer and use it in GitHub Desktop.
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?
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
@steveklabnik
Copy link

I'd just have it test your few examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment