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

Can not temporarily change the current context settings #540

Closed
Shengyis opened this issue Dec 24, 2024 · 3 comments · Fixed by #541
Closed

Can not temporarily change the current context settings #540

Shengyis opened this issue Dec 24, 2024 · 3 comments · Fixed by #541

Comments

@Shengyis
Copy link

Shengyis commented Dec 24, 2024

As described in the Tutorial, the following code in the with statement should work with precision = 100 and outside with statement should work with precision = 53(default)

from gmpy2 import *
print(const_pi()) # should print 3.1415926535897931

with context(precision=100) as ctx:
  print(const_pi()) # should print 3.1415926535897932384626433832793
  ctx.precision += 20
  print(const_pi()) # should print 3.1415926535897932384626433832795028847

print(const_pi()) # should print 3.1415926535897931

However, it prints

3.1415926535897931
3.1415926535897932384626433832793
3.1415926535897932384626433832795028847
3.1415926535897932384626433832793

It seems 'with context(precision=100)' change the global context and in the with statement, +20 change the context temporarily.

I change the code and put precision = 100 inside with statement , result becomes reasonable.

with context() as ctx:
  ctx.precision = 100
  print(const_pi()) # should print 3.1415926535897932384626433832793
  ctx.precision += 20
  print(const_pi()) # should print 3.1415926535897932384626433832795028847

print(const_pi()) # should print 3.1415926535897931

My gmpy2 version is 2.1.5 from conda-forge

@skirpichev
Copy link
Contributor

My gmpy2 version is 2.1.5 from conda-forge

Could you reproduce this with latest stable version (tutorial describes one)? I don't think 2.1.x is maintained anymore.

@Shengyis
Copy link
Author

My gmpy2 version is 2.1.5 from conda-forge

Could you reproduce this with latest stable version (tutorial describes one)? I don't think 2.1.x is maintained anymore.

Many thanks, I test it using gmpy2 2.2.1, the example in tutorial works. I have another question

from gmpy2 import *

a = mpfr('1')
b = mpfr('10')

ctxD = context(round=RoundDown)

print(ctxD.div(a, b)) # expect to print 0.099999999999999992

I create a context with RoundDown mode, and call the div method under this context, but I still get 0.10000000000000001 which is the default round to the nearest result. Many thanks!

@skirpichev
Copy link
Contributor

skirpichev commented Dec 24, 2024

Ah, that's a bug, thanks. Context argument actually not passed. Will fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants