MCPcopy Create free account
hub / github.com/numpy/numpy / pyrational_richcompare

Function pyrational_richcompare

numpy/core/src/umath/_rational_tests.c:507–1369  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

505 }
506
507static PyObject*
508pyrational_richcompare(PyObject* a, PyObject* b, int op) {
509 rational x, y;
510 int result = 0;
511 AS_RATIONAL(x,a);
512 AS_RATIONAL(y,b);
513 #define OP(py,op) case py: result = rational_##op(x,y); break;
514 switch (op) {
515 OP(Py_LT,lt)
516 OP(Py_LE,le)
517 OP(Py_EQ,eq)
518 OP(Py_NE,ne)
519 OP(Py_GT,gt)
520 OP(Py_GE,ge)
521 };
522 #undef OP
523 return PyBool_FromLong(result);
524}
525
526static PyObject*
527pyrational_repr(PyObject* self) {
528 rational x = ((PyRational*)self)->r;
529 if (d(x)!=1) {
530 return PyUnicode_FromFormat(
531 "rational(%ld,%ld)",(long)x.n,(long)d(x));
532 }
533 else {
534 return PyUnicode_FromFormat(
535 "rational(%ld)",(long)x.n);
536 }
537}
538
539static PyObject*
540pyrational_str(PyObject* self) {
541 rational x = ((PyRational*)self)->r;
542 if (d(x)!=1) {
543 return PyUnicode_FromFormat(
544 "%ld/%ld",(long)x.n,(long)d(x));
545 }
546 else {
547 return PyUnicode_FromFormat(
548 "%ld",(long)x.n);
549 }
550}
551
552static npy_hash_t
553pyrational_hash(PyObject* self) {
554 rational x = ((PyRational*)self)->r;
555 /* Use a fairly weak hash as Python expects */
556 long h = 131071*x.n+524287*x.dmm;
557 /* Never return the special error value -1 */
558 return h==-1?2:h;
559}
560
561#define RATIONAL_BINOP_2(name,exp) \
562 static PyObject* \
563 pyrational_##name(PyObject* a, PyObject* b) { \
564 rational x, y, z; \

Callers

nothing calls this directly

Calls 15

rational_negativeFunction · 0.85
rational_absFunction · 0.85
rational_intFunction · 0.85
rational_doubleFunction · 0.85
make_rational_intFunction · 0.85
rational_nonzeroFunction · 0.85
rational_addFunction · 0.85
rational_subtractFunction · 0.85
rational_multiplyFunction · 0.85
rational_divideFunction · 0.85
rational_remainderFunction · 0.85
rational_floorFunction · 0.85

Tested by

no test coverage detected