FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-37132: Add the imath module. by serhiy-storchaka · Pull Request #13741 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) .rst  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
Remove as_integer_ratio().
  • Loading branch information
serhiy-storchaka committed Jun 2, 2019
commit d0abc8dad8626d905f02f0fa070f2441e537b022
17 changes: 4 additions & 13 deletions Doc/library/imath.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,13 @@
--------------

This module provides access to the mathematical functions for integer arguments.
Most functions in this module accept integers and objects that implement the
These functions accept integers and objects that implement the
:meth:`__index__` method which is used to convert the object to an integer
number. These functions cannot be used with floating-point numbers or complex
number. They cannot be used with floating-point numbers or complex
numbers.

The following functions are provided by this module. Except when explicitly
noted otherwise, all return values are integers.


.. function:: as_integer_ratio(number)

Return integer ratio.

This function returns ``number.as_integer_ratio()`` if the argument has
the ``as_integer_ratio()`` method, otherwise it returns a pair
``(number.numerator, number.denominator)``.
The following functions are provided by this module. All return values are
integers.


.. function:: comb(n, k)
Expand Down
27 changes: 0 additions & 27 deletions Lib/test/test_imath.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,6 @@ def assertIntEqual(self, actual, expected):
self.assertEqual(actual, expected)
self.assertIs(type(actual), int)

def testAsIntegerRatio(self):
as_integer_ratio = imath.as_integer_ratio
self.assertEqual(as_integer_ratio(0), (0, 1))
self.assertEqual(as_integer_ratio(3), (3, 1))
self.assertEqual(as_integer_ratio(-3), (-3, 1))
self.assertEqual(as_integer_ratio(False), (0, 1))
self.assertEqual(as_integer_ratio(True), (1, 1))
self.assertEqual(as_integer_ratio(0.0), (0, 1))
self.assertEqual(as_integer_ratio(-0.0), (0, 1))
self.assertEqual(as_integer_ratio(0.875), (7, 8))
self.assertEqual(as_integer_ratio(-0.875), (-7, 8))
self.assertEqual(as_integer_ratio(Decimal('0')), (0, 1))
self.assertEqual(as_integer_ratio(Decimal('0.875')), (7, 8))
self.assertEqual(as_integer_ratio(Decimal('-0.875')), (-7, 8))
self.assertEqual(as_integer_ratio(Fraction(0)), (0, 1))
self.assertEqual(as_integer_ratio(Fraction(7, 8)), (7, 8))
self.assertEqual(as_integer_ratio(Fraction(-7, 8)), (-7, 8))

self.assertRaises(OverflowError, as_integer_ratio, float('inf'))
self.assertRaises(OverflowError, as_integer_ratio, float('-inf'))
self.assertRaises(ValueError, as_integer_ratio, float('nan'))
self.assertRaises(OverflowError, as_integer_ratio, Decimal('inf'))
self.assertRaises(OverflowError, as_integer_ratio, Decimal('-inf'))
self.assertRaises(ValueError, as_integer_ratio, Decimal('nan'))

self.assertRaises(TypeError, as_integer_ratio, '0')

def testFactorial(self):
factorial = imath.factorial
self.assertEqual(factorial(0), 1)
Expand Down
136 changes: 136 additions & 0 deletions Modules/clinic/imathmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 0 additions & 76 deletions Modules/imathmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -10,81 +10,6 @@ module imath
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=0b5ec353335010dd]*/


/*[clinic input]
imath.as_integer_ratio

x: object
/

greatest common divisor of x and y
[clinic start generated code]*/

static PyObject *
imath_as_integer_ratio(PyObject *module, PyObject *x)
/*[clinic end generated code: output=cd8105e5f41e2c52 input=4c6839316976ed71]*/
{
_Py_IDENTIFIER(as_integer_ratio);
_Py_IDENTIFIER(numerator);
_Py_IDENTIFIER(denominator);
PyObject *ratio, *as_integer_ratio, *numerator, *denominator;

if (PyLong_CheckExact(x)) {
return PyTuple_Pack(2, x, _PyLong_One);
}

if (_PyObject_LookupAttrId(x, &PyId_as_integer_ratio, &as_integer_ratio) < 0) {
return NULL;
}
if (as_integer_ratio) {
ratio = _PyObject_CallNoArg(as_integer_ratio);
Py_DECREF(as_integer_ratio);
if (ratio == NULL) {
return NULL;
}
if (!PyTuple_Check(ratio)) {
PyErr_Format(PyExc_TypeError,
"unexpected return type from as_integer_ratio(): "
"expected tuple, got '%.200s'",
Py_TYPE(ratio)->tp_name);
Py_DECREF(ratio);
return NULL;
}
if (PyTuple_GET_SIZE(ratio) != 2) {
PyErr_SetString(PyExc_ValueError,
"as_integer_ratio() must return a 2-tuple");
Py_DECREF(ratio);
return NULL;
}
}
else {
if (_PyObject_LookupAttrId(x, &PyId_numerator, &numerator) < 0) {
return NULL;
}
if (numerator == NULL) {
PyErr_Format(PyExc_TypeError,
"required a number, not '%.200s'",
Py_TYPE(x)->tp_name);
return NULL;
}
if (_PyObject_LookupAttrId(x, &PyId_denominator, &denominator) < 0) {
Py_DECREF(numerator);
return NULL;
}
if (denominator == NULL) {
Py_DECREF(numerator);
PyErr_Format(PyExc_TypeError,
"required a number, not '%.200s'",
Py_TYPE(x)->tp_name);
return NULL;
}
ratio = PyTuple_Pack(2, numerator, denominator);
Py_DECREF(numerator);
Py_DECREF(denominator);
}
return ratio;
}


/*[clinic input]
imath.gcd

Expand Down Expand Up @@ -993,7 +918,6 @@ imath_comb_impl(PyObject *module, PyObject *n, PyObject *k)


static PyMethodDef imath_methods[] = {
IMATH_AS_INTEGER_RATIO_METHODDEF
IMATH_COMB_METHODDEF
IMATH_FACTORIAL_METHODDEF
IMATH_GCD_METHODDEF
Expand Down

Back | FazBrowse Home | New Git URL