2

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?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
rocky
  • 245
  • 2
  • 6
  • 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. – Michael E2 Aug 21 '21 at 15:29
  • 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 . – rocky Aug 21 '21 at 16:59
  • 2
    Probably an implementation detail rather than a design choice. – Daniel Lichtblau Aug 21 '21 at 17:15
  • 2
    Whether a design choice or not, symmetric results for ±x seems a nice feature. – Michael E2 Aug 21 '21 at 19:22
  • 1
    Strongly related: (1), (2), (3). – Alexey Popkov Aug 22 '21 at 04:34
  • Round[-11.5, 1] returns -12 in Mathematica. – Alexey Popkov Aug 22 '21 at 04:41
  • Very interesting @AlexeyPopkov. Rationalize and Round clearly work differently in their rounding, but it seems for very deliberate reasons. – rocky Aug 22 '21 at 05:03
  • Is there a place to read about how far along mathics is? Like what percentage of code is implemented already? – DrMrstheMonarch Aug 24 '21 at 13:09
  • Sort of off topic, but Length[Names["System`*"]] gives 1130 builtins; probably far fewer than you get from MMA. https://mathics.org/docs/mathics-latest.pdf has what was available the end of July. My take though is that if you aren't someone who is willing to work on this in any capacity (which doesn't mean necessarily the Python code, it could be improving docs, writing compatibility libraries for older MMA or functions in the Wolfram Language, etc.), then I suggest waiting until others get around to improving things. It is not hard to find bugs. Harder is finding people to fix them. – rocky Aug 24 '21 at 14:49

1 Answers1

1

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.

rocky
  • 245
  • 2
  • 6