~ubuntu-branches/ubuntu/karmic/python3.0/karmic

« back to all changes in this revision

Viewing changes to Modules/mathmodule.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-16 17:18:23 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090216171823-1d5cm5qnnjvmnzzm
Tags: 3.0.1-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
137
137
}
138
138
 
139
139
/*
 
140
    Various platforms (Solaris, OpenBSD) do nonstandard things for log(0),
 
141
    log(-ve), log(NaN).  Here are wrappers for log and log10 that deal with
 
142
    special values directly, passing positive non-special values through to
 
143
    the system log/log10.
 
144
 */
 
145
 
 
146
static double
 
147
m_log(double x)
 
148
{
 
149
        if (Py_IS_FINITE(x)) {
 
150
                if (x > 0.0)
 
151
                        return log(x);
 
152
                errno = EDOM;
 
153
                if (x == 0.0)
 
154
                        return -Py_HUGE_VAL; /* log(0) = -inf */
 
155
                else
 
156
                        return Py_NAN; /* log(-ve) = nan */
 
157
        }
 
158
        else if (Py_IS_NAN(x))
 
159
                return x; /* log(nan) = nan */
 
160
        else if (x > 0.0)
 
161
                return x; /* log(inf) = inf */
 
162
        else {
 
163
                errno = EDOM;
 
164
                return Py_NAN; /* log(-inf) = nan */
 
165
        }
 
166
}
 
167
 
 
168
static double
 
169
m_log10(double x)
 
170
{
 
171
        if (Py_IS_FINITE(x)) {
 
172
                if (x > 0.0)
 
173
                        return log10(x);
 
174
                errno = EDOM;
 
175
                if (x == 0.0)
 
176
                        return -Py_HUGE_VAL; /* log10(0) = -inf */
 
177
                else
 
178
                        return Py_NAN; /* log10(-ve) = nan */
 
179
        }
 
180
        else if (Py_IS_NAN(x))
 
181
                return x; /* log10(nan) = nan */
 
182
        else if (x > 0.0)
 
183
                return x; /* log10(inf) = inf */
 
184
        else {
 
185
                errno = EDOM;
 
186
                return Py_NAN; /* log10(-inf) = nan */
 
187
        }
 
188
}
 
189
 
 
190
 
 
191
/*
140
192
   math_1 is used to wrap a libm function f that takes a double
141
193
   arguments and returns a double.
142
194
 
633
685
        return NULL;
634
686
}
635
687
 
636
 
PyDoc_STRVAR(math_factorial_doc, "Return n!");
 
688
PyDoc_STRVAR(math_factorial_doc,
 
689
"factorial(x) -> Integral\n"
 
690
"\n"
 
691
"Find x!. Raise a ValueError if x is negative or non-integral.");
637
692
 
638
693
static PyObject *
639
694
math_trunc(PyObject *self, PyObject *number)
785
840
"modf(x)\n"
786
841
"\n"
787
842
"Return the fractional and integer parts of x.  Both results carry the sign\n"
788
 
"of x.  The integer part is returned as a real.");
 
843
"of x and are floats.");
789
844
 
790
845
/* A decent logarithm is easy to compute even for huge longs, but libm can't
791
846
   do that by itself -- loghelper can.  func is log or log10, and name is
831
886
        if (!PyArg_UnpackTuple(args, "log", 1, 2, &arg, &base))
832
887
                return NULL;
833
888
 
834
 
        num = loghelper(arg, log, "log");
 
889
        num = loghelper(arg, m_log, "log");
835
890
        if (num == NULL || base == NULL)
836
891
                return num;
837
892
 
838
 
        den = loghelper(base, log, "log");
 
893
        den = loghelper(base, m_log, "log");
839
894
        if (den == NULL) {
840
895
                Py_DECREF(num);
841
896
                return NULL;
854
909
static PyObject *
855
910
math_log10(PyObject *self, PyObject *arg)
856
911
{
857
 
        return loghelper(arg, log10, "log10");
 
912
        return loghelper(arg, m_log10, "log10");
858
913
}
859
914
 
860
915
PyDoc_STRVAR(math_log10_doc,