Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test failures due to global mpmath state #38776

Merged
merged 4 commits into from
Oct 12, 2024

Conversation

orlitzky
Copy link
Contributor

@orlitzky orlitzky commented Oct 6, 2024

mpmath has a global mp.pretty variable that affects how its numbers are printed. On my machine, it's not reliable. For example:

Failed example:
    repr(mp.mpc(2,3))
Expected:
    '(2.0 + 3.0j)'
Got:
    "mpc(real='2.0', imag='3.0')"

I've only fixed the tests that are failing for me, since mpmath-1.4 is on the TODO list. A few different strategies are used:

  • print() will use the pretty format if all we're doing is displaying an answer.
  • with workdps can be used to change the precision only locally.
  • In TESTS blocks, the ugly output is acceptable.
  • When actually testing mp.pretty, we can first clone the mp object with mp2 = mp.clone(), and then work on the new one.

We have an mpmath test in this file that sets the global precision
(decimal places) and "pretty" variable for all of mpmath. Instead it
is safer to use a context manager for the precision, and to call
print(foo) to obtain the string representation of foo. This fixes
the following two test failures:

  Failed example:
    gegenbauer_mp(-7,0.5,0.3)
  Expected:
    0.1291811875
  Got:
    mpf('0.1291811874999999999999999952')

and

  Failed example:
    gegenbauer_mp(2+3j, -0.75, -1000j)
  Expected:
    (-5038991.358609026523401901 + 9414549.285447104177860806j)
  Got:
    mpc(real='-5038991.358609026523401901035', imag='9414549.285447104177860806274')
One test in this file sets mp.pretty=True but is failing for me, most
likely due to the shared state inherent to mp.pretty. Since this test
is inside a TESTS block and not visible to end users, the simplest way
to fix it is to delete the mp.pretty=True and to look for the ugly
output.
There's a test in this file that is trying to use the global property
mp.pretty=True but is failing on my machine. Since the test is inside
a TESTS block and not visible to end users, the simplest way to fix it
is to expect the ugly output from mpmath; although in one instance we
do call str() on an mpmath real number to obtain '2.3' instead of some
2.999... thing that would require a tolerance.
There are two tests in this file that are attempting to test the
repr() of an mpmath number with mp.pretty=True set globally. The
shared state is causing test failures, but we can't simply leave it
off, because that's the point of the test. Instead we can clone()
the "mp" object to obtain a new one (whose state is not so global)
and then work with that instead.
Copy link

github-actions bot commented Oct 6, 2024

Documentation preview for this PR (built with commit 441ee05; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@dimpase dimpase self-assigned this Oct 7, 2024
Copy link
Member

@dimpase dimpase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@orlitzky
Copy link
Contributor Author

orlitzky commented Oct 7, 2024

Thanks, I may finally be able to focus on whatever is happening with GAP soon.

vbraun pushed a commit to vbraun/sage that referenced this pull request Oct 9, 2024
    
mpmath has a global `mp.pretty` variable that affects how its numbers
are printed. On my machine, it's not reliable. For example:

```
Failed example:
    repr(mp.mpc(2,3))
Expected:
    '(2.0 + 3.0j)'
Got:
    "mpc(real='2.0', imag='3.0')"
```

I've only fixed the tests that are failing for me, since mpmath-1.4 is
on the TODO list. A few different strategies are used:

* `print()` will use the pretty format if all we're doing is displaying
an answer.
* `with workdps` can be used to change the precision only locally.
* In TESTS blocks, the ugly output is acceptable.
* When actually testing `mp.pretty`, we can first clone the `mp` object
with `mp2 = mp.clone()`, and then work on the new one.
    
URL: sagemath#38776
Reported by: Michael Orlitzky
Reviewer(s): Dima Pasechnik
@vbraun vbraun merged commit 367b516 into sagemath:develop Oct 12, 2024
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants