| 505 | } |
| 506 | |
| 507 | static PyObject* |
| 508 | pyrational_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 | |
| 526 | static PyObject* |
| 527 | pyrational_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 | |
| 539 | static PyObject* |
| 540 | pyrational_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 | |
| 552 | static npy_hash_t |
| 553 | pyrational_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; \ |
nothing calls this directly
no test coverage detected