Copying Bio-python to globplot to satisfy the dependency
[jabaws.git] / binaries / src / globplot / biopython-1.50 / Bio / cmathfnsmodule.c
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.
5  *
6  * cmathfnsmodule.c
7  * Created 3 Jun 2000
8  */
9
10 #include "Python.h"
11 #include <math.h>
12
13 #include "csupport.h"
14
15
16
17 /************************************** Exported Functions ***********/
18
19 static char cmathfns_intd__doc__[] = 
20 "intd(x[, digits_after_decimal]) -> int x, rounded\n\
21 \n\
22 Represent a floating point number with some digits after the\n\
23 decimal point as an integer.  This is useful when floating point\n\
24 comparisons are failing due to precision problems.  e.g.\n\
25 intd(5.35, 1) -> 54.\n\
26 \n\
27 ";
28
29 static PyObject *cmathfns_intd(
30     PyObject *self, PyObject *args, PyObject *keywds)
31 {
32     PyObject *digits_after_decimal = Py_None;
33     double x, digits;
34     double precision;
35
36     static char *kwlist[] = {"x", "digits_after_decimal", NULL};
37     if(!PyArg_ParseTupleAndKeywords(args, keywds, "d|O", kwlist, 
38                                     &x, &digits_after_decimal))
39         return NULL;
40
41     if(digits_after_decimal == Py_None)
42         digits = 0;
43     else {
44         digits = PyNumber_AsDouble(digits_after_decimal);
45         if(PyErr_Occurred()) {
46             return NULL;
47         }
48     }
49     precision = pow(10, digits);
50     if(x >= 0)
51         x = (int)(x * precision + 0.5);
52     else
53         x = (int)(x * precision - 0.5);
54     return PyFloat_FromDouble(x);
55 }
56
57
58
59
60 static char cmathfns_fcmp__doc__[] = 
61 "fcmp(x, y, precision) -> -1, 0, or 1";
62
63 static PyObject *cmathfns_fcmp(
64     PyObject *self, PyObject *args, PyObject *keywds)
65 {
66     double x, y, precision;
67     int result;
68
69     static char *kwlist[] = {"x", "y", "precision", NULL};
70     if(!PyArg_ParseTupleAndKeywords(args, keywds, "ddd", kwlist, 
71                                     &x, &y, &precision))
72         return NULL;
73
74     if(fabs(x-y) < precision)
75         result = 0;
76     else if(x < y)
77         result = -1;
78     else result = 1;
79     return PyInt_FromLong(result);
80 }
81
82
83
84 static char cmathfns_safe_log__doc__[] = 
85 "safe_log(n, zero=None, neg=None) -> log(n)\n\
86 \n\
87 Calculate the log of n.  If n is 0, returns the value of zero.  If n is\n\
88 negative, returns the value of neg.\n\
89 \n\
90 ";
91
92 static PyObject *cmathfns_safe_log(
93     PyObject *self, PyObject *args, PyObject *keywds)
94 {
95     PyObject *zero = Py_None,
96         *neg = Py_None;
97     double n;
98
99     static char *kwlist[] = {"n", "zero", "neg", NULL};
100
101     if(!PyArg_ParseTupleAndKeywords(args, keywds, "d|OO", kwlist, 
102                                     &n, &zero, &neg))
103         return NULL;
104     
105     if(n < 0) {
106         Py_INCREF(neg);
107         return neg;
108     } else if(n < 1E-100) {
109         Py_INCREF(zero);
110         return zero;
111     }
112
113     return PyFloat_FromDouble(log(n));
114 }
115
116
117
118
119 /************************************** Module definition stuff ******/
120
121 static PyMethodDef cmathfnsMethods[] = {
122     {"fcmp", (PyCFunction)cmathfns_fcmp, METH_VARARGS|METH_KEYWORDS, 
123      cmathfns_fcmp__doc__},
124     {"intd", (PyCFunction)cmathfns_intd, METH_VARARGS|METH_KEYWORDS, 
125      cmathfns_intd__doc__},
126     {"safe_log", (PyCFunction)cmathfns_safe_log, METH_VARARGS|METH_KEYWORDS, 
127      cmathfns_safe_log__doc__},
128     {NULL, NULL}
129 };
130
131 static char cmathfns__doc__[] =
132 "This provides helper functions for the mathfns module.\n\
133 You should never import this module on its own.\n\
134 \n\
135 ";
136
137 void initcmathfns(void)
138 {
139     (void) Py_InitModule3("cmathfns", cmathfnsMethods, cmathfns__doc__);
140 }