Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import random
- from math import log10,log
- def roundf(f):
- epsilon = 2.0**(log(f,2) - 51.0)
- return round(f + cmp(f,0)*epsilon,2)
- def test_rounding():
- " Test the rounding function for up to 14 digits precision "
- fractions = [.015, .01499, .675, .67499]
- expecteds = ['.02', '.01', '.68', '.67']
- magnitudes = range(10)
- c = 0
- for mag in magnitudes:
- for i in xrange(len(fractions)):
- for x in xrange(10000):
- frac = fractions[i]
- exp = expecteds[i]
- n = int(random()*pow(10,mag)) + frac
- rounded = str(roundf(n))
- assert rounded.endswith(exp)
- test_rounding()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement