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
    Commented Aug 21, 2021 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
    Commented Aug 21, 2021 at 16:59
  • 2
    $\begingroup$ Probably an implementation detail rather than a design choice. $\endgroup$ Commented Aug 21, 2021 at 17:15
  • 2
    $\begingroup$ Whether a design choice or not, symmetric results for ±x seems a nice feature. $\endgroup$
    – Michael E2
    Commented Aug 21, 2021 at 19:22
  • 1
    $\begingroup$ Strongly related: (1), (2), (3). $\endgroup$ Commented Aug 22, 2021 at 4:34

1 Answer 1

1
$\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 and acknowledge you have read our privacy policy.

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