1 # Copyright 2000 by Jeffrey Chang. All rights reserved.
2 # This code is part of the Biopython distribution and governed by its
3 # license. Please see the LICENSE file that should have been included
4 # as part of this package.
6 """This provides useful general math tools (DEPRECATED).
8 This module and its C code equivalent are considered to be deprecated, and
9 are likely to be removed in a future release of Biopython. Please get in
10 touch via the mailing list if this will affect you.
13 fcmp Compare two floating point numbers, up to a specified precision.
14 intd Represent a floating point number as an integer.
15 safe_log log, but returns an arbitrarily small number for log(0).
16 safe_exp exp, but returns a large or small number instead of overflows.
20 warnings.warn("Bio.mathfns and its C code equivalent Bio.cmathfns are" \
21 +" deprecated, and will be removed in a future release of"\
22 +" Biopython. If you want to continue to use this code,"\
23 +" please get in contact with the Biopython developers via"\
24 +" the mailing lists to avoid its permanent removal from"\
30 def fcmp(x, y, precision):
31 """fcmp(x, y, precision) -> -1, 0, or 1"""
32 if math.fabs(x-y) < precision:
38 def intd(x, digits_after_decimal=0):
39 """intd(x[, digits_after_decimal]) -> int x, rounded
41 Represent a floating point number with some digits after the
42 decimal point as an integer. This is useful when floating point
43 comparisons are failing due to precision problems. e.g.
47 precision = 10.**digits_after_decimal
49 x = int(x * precision + 0.5)
51 x = int(x * precision - 0.5)
54 def safe_log(n, zero=None, neg=None):
55 """safe_log(n, zero=None, neg=None) -> log(n)
57 Calculate the log of n. If n is 0, returns the value of zero. If n is
58 negative, returns the value of neg.
68 def safe_log2(n, zero=None, neg=None):
69 """safe_log2(n, zero=None, neg=None) -> log(n)
71 Calculate the log base 2 of n. If n is 0, returns the value of
72 zero. If n is negative, returns the value of neg.
75 l = safe_log(n, zero=zero, neg=neg)
80 def safe_exp(n, under=None, over=None):
81 """safe_exp(n, under=None, over=None) -> e**n
83 Guaranteed not to overflow. Instead of overflowing, it returns
84 the values of 'under' for underflows or 'over' for overflows.
93 raise "How did I get here?"
95 # Try and load C implementations of functions. If I can't,
96 # then just ignore and use the pure python implementations.
98 from cmathfns import *