Skip to content

Commit

Permalink
Rationalize result that are an integer stay int
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Aug 22, 2021
1 parent 8158359 commit f6188b0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Bugs

* Fix and document better behavior of ``Quantile``
* Improve Asymptote ``BezierCurve``implementation
* ``Rationalize`` gives symmetric results for +/- like MMA does. If
the result is an integer, it stays that way.

4.0.0
-----
Expand Down
8 changes: 4 additions & 4 deletions mathics/builtin/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ class Rationalize(Builtin):
For negative $x$, '-Rationalize[-$x$] == Rationalize[$x$]' which gives symmetric results:
>> Rationalize[-11.5, 1]
= -11 / 1
= -11
Not all numbers can be well approximated.
>> Rationalize[N[Pi]]
Expand Down Expand Up @@ -1412,11 +1412,11 @@ def apply_dx(self, x, dx, evaluation):
if py_x.is_positive:
a = self.approx_interval_continued_fraction(py_x - py_dx, py_x + py_dx)
sym_x = sympy.ntheory.continued_fraction_reduce(a)
return Rational(sym_x)
else:
a = self.approx_interval_continued_fraction(-py_x - py_dx, -py_x + py_dx)
sym_x = sympy.ntheory.continued_fraction_reduce(a)
return Rational(-sym_x)
sym_x = -sympy.ntheory.continued_fraction_reduce(a)

return Integer(sym_x) if sym_x.is_integer else Rational(sym_x)

@staticmethod
def approx_interval_continued_fraction(xmin, xmax):
Expand Down
9 changes: 4 additions & 5 deletions test/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ def test_rationalize():
"Rationalize[42]",
"42",
),
# We get 3 / 1 instead of 3
# (
# "Rationalize[3, 1]",
# "3",
# ),
(
"Rationalize[3, 1]",
"3",
),
(
"Rationalize[N[Pi] + 0.8 I, 0]",
"245850922 / 78256779 + 4 I / 5",
Expand Down

0 comments on commit f6188b0

Please sign in to comment.