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

Incorrect result, regardless of precision settings. emax didn't help either #116

Closed
ghost opened this issue Aug 9, 2023 · 1 comment
Closed

Comments

@ghost
Copy link

ghost commented Aug 9, 2023

I get a result i don't expect when i add a small number to larger number. In the decimal system it is obvious that the result is mathematically wrong.
I tried a higher precision, and this didn't work. After reading issue #112 i tried to set emin but this didn't change anything.

Any idea how to get the number i expected?

#!/usr/bin/env python3
from bigfloat import *

precision(300)

#Doesn't change anything if this line is here or not
Context(precision=300, emin=-9999999)


#########################
# add 21, works somewhat#
#########################
r = BigFloat(1)
a = BigFloat(10**33)
r = r + 21*(1/a)
print(r)
print(int(r*10**40))

########################
# add 22, doesn't work #
########################
r = BigFloat(1)
a = BigFloat(10**35)
r = r + 22*(1/a)
print(r)
print(int(r*10**40))

What i expected:

1.00000000000000000000000000000002100
10000000000000000000000000000000210000000
1.00000000000000000000000000000000022
10000000000000000000000000000000002200000

What i got:

1.00000000000000000000000000000002099
10000000000000000000000000000000209715200
1.00000000000000000000000000000000019
10000000000000000000000000000000002097152

Other things i tried:

  • Setting emax=999999
  • Using import bigfloat instead of from bigfloat import * and then using r = bigfloat.BigFloat("1",bigfloat.precision(4000))

No success so far.

Version:

>>> import bigfloat
>>> bigfloat.__version__
'0.4.0'
@mdickinson
Copy link
Owner

Thanks for the report. precision is a context manager, which needs to be used with a with statement. Here's an example:

>>> from bigfloat import *
>>> r = BigFloat(1)
>>> a = BigFloat(10**35)
>>> with precision(300):
...     r = r + 22 * (1 / a)
...     print(r)
...     print(int(r*10**40))
... 
1.0000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000
10000000000000000000000000000000002200000

See this page of the tutorial for more.

@mdickinson mdickinson closed this as not planned Won't fix, can't repro, duplicate, stale Aug 15, 2023
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

No branches or pull requests

1 participant