Python Forum
Parsing Large Numbers Question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing Large Numbers Question
#1
Hi All, I am writing some simple code to raise a base value to a power
then perform some operations on the output. I have included the code I am
using now, below ( see end of this message ), also see immediately below
what this looks like when I run my script from the console ;
.........................................
C:\PYTHON34>python tst.py
INPUT Value: 431.1
INPUT Power Value; 1.9907
RESULT 1739.554505641426658257842063903808593750000000000000000000000000
.........................................

What I need to be able to do ;

1.) Store whole number value to left of the decimal point without decimals to
a variable = x

2.) Grab entire decimal value to the right of the decimal point and store in a variable
= y

Parse (y) according to some simple rules. Here's where it gets trick for me.

What I want to do is examine (y) to see if there are any leading leading and trailing zeros

In the example ; "1739.554505....." IF this value was instead, something like any of the following ;

1739.0554505
1739.00554505
1739.000554505

Then I want to extract the whole number less any continuous leading zeroes

NEXT, I want to cut the number so that when there are four continuous trailing zeros, all zeros starting from the first one that started off the first four continuous zeros and all following zeroes are truncated

So in our example ;
"1739.554505641426658257842063903808593750000000000000000000000000"

(y) becomes "55450564142665825784206390380859375"

Next I want to take the mod of above value by %1999

This returns "1407"

Next I want to join this value AS the decimal value to the right as ;

1739.1407

Ok, so now why am I stuck at this point?

I'm still fairly new to learning Python so I don't actually kknow what the best ways are
to do this.

I'm thinking I could maybe just do a trim on the value to the right of the decimal, take
that, convert the whole thing to a string, then chop up each digit and store each individually to an array then code a rule set for stepping through strings in array cells, but this also seems like it could take more CPU time than I'd like. I'm hoping to perform
this sort of operation fast, as fast as possible.

Also, I do not even know how to best approach this even if I had to store this as a string
into an array, and if I understand correctly string processing tends to takes more CPU
time than number calcs.

Any insight, direction, code suggestions greatly appreciated!

This is the code I am currently using ;

import time
e0=time.time()
c0=time.clock()

import sys
import math
from sympy import mpmath
from mpmath import *
mpmath.mp.dps = 10000


inputx = float(input('Enter inputx Value: '))
powerx =float(input('Enter Power Value; '))

inputx_squared = float((mpmath.power(inputx, powerx)))%1999

print('\r')
print('RESULT ', '%.60f' % inputx_squared)

elapsed_time=time.time() -e0
cpu_time=time.clock() -c0

print('\r')
print("CPU TIME ;", cpu_time)
Reply
#2
The correct use of multiprecision is as follows; mp.dps, not math.mp.dps, Cheers
>>> from mpmath import *
>>> mp.dps = 2000
>>> print (pi)
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328
2306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648
2337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917
1536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237
9962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293
1767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922
7968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502
4459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468
7311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533
8182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509
8381754637464939319255060400927701671139009848824012858361603563707660104710181942955596198946767837449448255379774
7268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732
6391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750
9302955321165344987202755960236480665499119881834797753566369807426542527862551818417574672890977772793800081647060
0161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694
8556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006
4225125205117392984896084128488626945604241965285022210661186306744278622039194945047123713786960956364371917287467
7646575739624138908658326459958133904780275901
>>>
Reply
#3
Thanks Samsonite, will ensure I follow the syntax mention for
mp.dps - Still hoping to get feedback and any possible direction
on the most important items listed above (1) & (2)

Thx All

:)
Reply
#4
Hi post-opener.
Back to your question, in order to compute 431.1**1.9907 (as per your above example) with 60 significant figures, the code can be strongly simplified as shown underneath. Cheers
>>> from mpmath import *
>>> mp.dps = 60
>>> a=float(431.1)
>>> b=float(1.9907)
>>> c= power(a,b)
>>> print (c)
175652.554505641455099365134478793074168970888475271620418457
Reply
#5
I did not know this library, but I never needed this high precision.
Do you know the biggest differences to Decimal which is in the stdlib of Python?
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Hi DeaD_EyE.
You wrote: Do you know the biggest differences to Decimal which is in the stdlib of Python? What I know, up to now, is shown in next applied example:

# -------- tempo4.py ---------------
from astropy.time import Time 
from decimal import Decimal;  # up to 40 significant figures
t = Time('2006-01-15 21:24:37.5', format='iso', scale='utc', precision=6)
#
print (t.ut1.iso, t.ut1.mjd) 
print(Decimal(t.ut1.mjd))
#
print (t.ut1.jd1, t.ut1.jd2)
print (t.tt.iso, t.tt.mjd) 
print (t.tt.jd1, t.tt.jd2) 
#---------- OUTPUT ----------
# 2006-01-15 21:24:37.834136 53750.89210456175
#                            53750.8921045617535128258168697357177734375
# 2453751.0 0.3921045617535457
# 2006-01-15 21:25:42.684000 53750.89285513889
# 2453751.0 0.3928551388888888
With reference to your statement I did not know this library, but I never needed this high precision let me say why I'm familiar with the multiprecision subject. Several years ago I uploaded the following Python webpages [written in italian, because I'm italian as well as some followers] http://precismultipla.altervista.org/index.htm One of the most appreciated has been the item: Python: Numeri di Fibonacci.

Thanks, and cheers
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing large JSON josvink66 5 558 Jan-10-2024, 05:46 PM
Last Post: snippsat
  parsing question ridgerunnersjw 7 4,931 Oct-10-2020, 09:04 PM
Last Post: ridgerunnersjw
  parsing question ridgerunnersjw 3 1,932 Oct-06-2020, 02:06 AM
Last Post: micseydel
  string parsing question ( I think) diggee17 4 2,970 Jul-24-2019, 02:37 PM
Last Post: diggee17
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,664 May-09-2019, 12:19 PM
Last Post: Pleiades
  Perfect Number formula in Python Question an Mersenne Numbers Pleiades 5 5,968 May-16-2018, 04:56 PM
Last Post: Pleiades
  I'm dividing large numbers of about 25 million digits I need to retain decimal format Pleiades 2 3,142 Apr-26-2018, 07:50 AM
Last Post: Pleiades
  dividing large numbers charlemayn 7 9,445 Feb-13-2018, 06:03 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020