On 5 April 2016 at 01:56, Amy Valhausen <[email protected]> wrote:
>
> import numpy
> (np.longdouble(1.4142)** 6000 )%400
...
>
> # The library mpmath is a good solution
>>>> import sympy as smp
>>>> mp = smp.mpmath
>
>>>> mp.mp.dps = 50 # Computation precision is 50 digits
50 digits is nowhere near enough. Think about what the number
1.4142**6000 is: it's a big number. If I wrote it out in digits it
might look something like:
5432321453425234...234234234.123235345345
The digits that contain the precision needed to get the remainder
modulo 400 begin just before the decimal point. So how many digits are
there before that? If d is the number of digits preceding the decimal
point then loosely:
10**d = 1.4142**6000
Which gives that
d = log10(1.4142**6000) = 6000*log10(1.4142) ~= 903
So if you want an answer that's good for m digits you'll need to use
about 900+m digits for the exponentiation:
In [1]: from sympy import mpmath
In [2]: mpmath.mp.dps = 950
In [3]: mpmath.mpf('1.4142') ** 6000 % 400
Out[3]:
mpf('271.048181008630921815939109488389292518324580362344398848121124779167483584534976647550313880646779627157871790825164801629065802757168057723902165889739990234375')
BTW it's also possible to do this particular calculation easily enough
just with Python stdlib (and without numpy or sympy):
In [1]: from fractions import Fraction
In [2]: float(Fraction('1.4142')**6000 % 400)
Out[2]: 271.04818100863093
I though that it should be possible to easily do this with sympy
Floats but it doesn't seem to work:
In [1]: x = S(1.4142)
In [2]: x
Out[2]: 1.41420000000000
In [3]: x**6000
Out[3]: 1.16144178843571e+903
In [4]: x**6000 % 400
Out[4]: 32.0000000000000
This doesn't work because auto-evaluation of x**6000 destroyed all the
precision so I tried:
In [5]: Pow(x, 6000, evaluate=False)
Out[5]:
6000
1.4142
In [6]: Pow(x, 6000, evaluate=False) % 400
Out[6]: Mod(1.16144178843571e+903, 400)
Well that's annoying. It's gone ahead and auto-evaluated the x**6000
but created a Mod object instead of evaluating the modular division.
So let's disable evaluation of at the mod step as well:
In [10]: Mod(Pow(x, 6000, evaluate=False), 400, evaluate=False).evalf(50)
Out[10]:
⎛ 6000 ⎞
Mod⎝1.4142 , 400⎠
How do I get that to actually evaluate?
--
Oscar
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAHVvXxRWe%2BDykG15%3Dks%2Bs04mF%2BgS2LmtRJ_kmAWQdwXNHAg5Bw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.