2
$\begingroup$

In the open source Mathematica implementation called Mathics, an issue was raised where Mathics computes Rationalize[-11.5, 1] as -12 rather than Mathematica which is reported to compute this as -11.

Looking at the docs for Rationalize, -11 seems equally valid.

Is this is purely an implementation choice, or is there more reasons why one answer is preferred over the other?

$\endgroup$
9
  • $\begingroup$ I believe Rationalize[] uses the continued fraction expansion to find a rational approximation with least denominator. For negative numbers it proabably returns -Rationalize[-x]. I don't know how Mathics does it. $\endgroup$
    – Michael E2
    Aug 21 '21 at 15:29
  • $\begingroup$ Mathics[] uses the continued fraction expansion as well on the bounds given. Here those values are -11.5 +/- 1 When the continued fraction of both of these differs in a digit that is when it stops. However the value it picks is min(-13, -11) + 1 which is -12. Of course, applying -Rationalize[-x] first in Mathics will give the answer reported . $\endgroup$
    – rocky
    Aug 21 '21 at 16:59
  • 1
    $\begingroup$ Probably an implementation detail rather than a design choice. $\endgroup$ Aug 21 '21 at 17:15
  • 2
    $\begingroup$ Whether a design choice or not, symmetric results for ±x seems a nice feature. $\endgroup$
    – Michael E2
    Aug 21 '21 at 19:22
  • 1
    $\begingroup$ Strongly related: (1), (2), (3). $\endgroup$ Aug 22 '21 at 4:34
0
$\begingroup$

Summarizing the comments to the question, Michael E2 points out that, for negative x, having Rationalize[x] == -Rationalize[-x] gives symmetric results, whether or not this was a specific design choice or an implementation detail.

So the next release of Mathics will do the same.

Alexey Popkov suggests comparing with the behavior of Round which is different in its behavior in that it rounds to the nearest even integer (and this clearly does not happen here). If nothing else, this circumstantial evidence suggests that the behavior choice for Rationalize was probably deliberate.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.