Home > Mobile >  The precision of Decimal library in python
The precision of Decimal library in python

Time:10-27

From the documentation page of enter image description here

I want to calculate the digit of 6**500000 in base-6 representation, so my expect result would be int(b.ln() / a.ln()) 1, which should be 500001. However, when I set the prec to 250, it gives me the wrong result. How can I solve this?

Also, if I want to output the result without the scientific notation (i.e. 5E 5), what should I do?

CodePudding user response:

The Documentation clearly show the parameters of getcontext()

enter image description here

when you simply execute getcontext() it can show its built-in parameters.

Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])

When you can change getcontext().prec = 250 then it can only overwrite prec value.

enter image description here

the main thing which can effect the result is rounding parameter right after the prec, It is built-in parameter rounding=ROUND_HALF_EVEN. I want to change this to None but it can show an error.

enter image description here

so it can clarify that no matter what we can do it must change even slightly because of rounding parameter.

Note: your result may also effect because of other built-in parameters.

CodePudding user response:

The documentation for Decimal.ln() states:

Return the natural (base e) logarithm of the operand. The result is correctly rounded using the ROUND_HALF_EVEN rounding mode.

When you changed the precision, more digits of the numbers were calculated and they are then rounded down instead of up. The number needs to be rounded because it is not necessarily within the specified precision, or even rational at all.

For outputting results in scientific notation, see this question.

CodePudding user response:

If you're after very precise or symbolic math and/or being bitten by IEEE 754 float aliasing, check out SymPy

>>> from sympy import log, N
>>> expr = log(6**50000) / log(6)
>>> N(expr)
50000.0000000000
  • Related