diff options
author | Reinier Heeres <reinier@heeres.eu> | 2010-07-29 09:41:22 (GMT) |
---|---|---|
committer | Reinier Heeres <reinier@heeres.eu> | 2010-07-29 09:41:22 (GMT) |
commit | a4ee179c7088b79a31f366ee5c080bfb19abf39f (patch) | |
tree | 4496fd35d7a74df1f1d36964b367437ba2e9516b /functions.py | |
parent | 937be6a3a9e36876dce037dc72966097b7efa096 (diff) |
Add function b10bin to support binary on python2.5
Diffstat (limited to 'functions.py')
-rw-r--r-- | functions.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/functions.py b/functions.py index 2e69f6a..b057d4a 100644 --- a/functions.py +++ b/functions.py @@ -41,6 +41,7 @@ _FUNCTIONS = [ _('atan'), _('atanh'), _('and'), + _('b10bin'), _('ceil'), _('cos'), _('cosh'), @@ -88,7 +89,7 @@ def _d(val): else: return None -class ClassValue(): +class ClassValue: """ Class to share a value with the outside world. This is required because plain floats / integers are not asigned by @@ -158,6 +159,25 @@ atanh.__doc__ = _( 'atanh(x), return the arc hyperbolic tangent of x. This is the value y for \ which the hyperbolic tangent equals x.') +def b10bin(x): + ret = 0 + while x > 0: + ret <<= 1 + + y = x % 10 + if y == 1: + ret += 1 + elif y != 0: + raise ValueError(_('Number does not look binary in base 10')) + + x /= 10 + + return ret + +b10bin.__doc__ = _( +'b10bin(x), interpret a number written in base 10 as binary, e.g.: \ +b10bin(10111) = 23,') + def ceil(x): return math.ceil(float(x)) ceil.__doc__ = _('ceil(x), return the smallest integer larger than x.') |