Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Large Numbers Question

Status
Not open for further replies.

astrogirl77

Programmer
Aug 14, 2008
45
CA
Parsing Large Numbers Question

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 munderstand 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 ;

Code:
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)
 
I would convert the number into string and at the end from the string again to a number.
With string you can use regular expressions to extract from the decimal part only the group without leading zeros.

Code:
[COLOR=#800080]import[/color] re
x_num = [COLOR=#ff00ff]1739.000554505[/color]
[COLOR=#008b8b]print[/color] [COLOR=#ff00ff]"x_num = %14.9f"[/color] % x_num

[COLOR=#0000ff]# convert to string[/color]
x_str = [COLOR=#ff00ff]"%14.9f"[/color] % x_num
[COLOR=#008b8b]print[/color] [COLOR=#ff00ff]"x_str = '%s'"[/color] % x_str

[COLOR=#0000ff]# extract integer and decimal part[/color]
x_str_int_part, x_str_dec_part = x_str.split([COLOR=#ff00ff]"."[/color])
[COLOR=#008b8b]print[/color] [COLOR=#ff00ff]"x_str_int_part = '%s'"[/color] % x_str_int_part
[COLOR=#008b8b]print[/color] [COLOR=#ff00ff]"x_str_dec_part = '%s'"[/color] % x_str_dec_part

[COLOR=#0000ff]# extract from decimal part a group without leading zeros[/color]
regex_successful = re.search([COLOR=#ff00ff]'0+(\d+)'[/color], x_str_dec_part)
[COLOR=#a52a2a][b]if[/b][/color] regex_successful:
  x_str_dec_part_new = regex_successful.group([COLOR=#ff00ff]1[/color])
[COLOR=#a52a2a][b]else[/b][/color]:
  x_str_dec_part_new = x_str_dec_part
[COLOR=#008b8b]print[/color] [COLOR=#ff00ff]"x_str_dec_part_new = '%s'"[/color] % x_str_dec_part_new

[COLOR=#0000ff]# create new string[/color]
x_str_new = x_str_int_part + [COLOR=#ff00ff]"."[/color] + x_str_dec_part_new
[COLOR=#008b8b]print[/color] [COLOR=#ff00ff]"x_str_new = '%s'"[/color] % x_str_new

[COLOR=#0000ff]# convert to number[/color]
x_num_new = [COLOR=#008b8b]float[/color](x_str_new)
[COLOR=#008b8b]print[/color] [COLOR=#ff00ff]"x_num_new = %f"[/color] % x_num_new

Output:
Code:
$ python astrogirl77.py 
x_num = 1739.000554505
x_str = '1739.000554505'
x_str_int_part = '1739'
x_str_dec_part = '000554505'
x_str_dec_part_new = '554505'
x_str_new = '1739.554505'
x_num_new = 1739.554505
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top