Anshul -4 years ago 72
Python Question

multiply very large floating point numbers in python

I want to multiply a decimal number with 5000 digits with a integer having 5000 digits or less.I have tried Decimal Module but it doesn't seem to work.Also how do I print all the digits of a large number as my number may have 10000 digits?
I have tried this(How to print all digits of a large number in python?) also but it didn't help.

```from decimal import * n=int(raw_input()) e=Decimal('2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035402123407849819334321068170121005627880235193033224745015853904730419957777093503660416997329725088687696640355570716226844716256079882651787134195124665201030592123667719432527867539855894489697096409754591856956380236370162112047742722836489613422516445078182442352948636372141740238893441247963574370263755294448337998016125492278509257782562092622648326277933386566481627725164019105900491644998289315056604725802778631864155195653244258698294695930801915298721172556347546396447910145904090586298496791287406870504895858671747985466775757320568128845920541334053922000113786300945560688166740016984205580403363795376452030402432256613527836951177883863874439662532249850654995886234281899707733276171783928034946501434558897071942586398772754710962953741521115136835062752602326484728703920764310059584116612054529703023647254929666938115137322753645098889031360205724817658511806303644281231496550704751025446501172721155519486685080036853228183152196003735625279449515828418829478761085263981395599006737648292244375287184624578036192981971399147564488262603903381441823262515097482798777996437308997038886778227138360577297882412561190717663946507063304527954661855096666185664709711344474016070462621568071748187784437143698821855967095910259686200235371858874856965220005031173439207321139080329363447972735595527734907178379342163701205005451326383544000186323991490705479778056697853358048966906295119432473099587655236812859041383241160722602998330535370876138939639177957454016137223618789365260538155841587186925538606164779834025435128439612946035291332594279490433729908573158029095863138268329147711639633709240031689458636060645845925126994655724839186564209752685082307544254599376917041977780085362730941710163434907696423722294352366125572508814779223151974778060569672538017180776360346245927877846585065605078084421152969752189087401966090665180351650179250461950136658543663271254963990854914420001457476081930221206602433009641270489439039717719518069908699860663658323227870937650226014929101151717763594460202324930028040186772391028809786660565118326004368850881715723866984224220102495055188169480322100251542649463981287367765892768816359831247788652014117411091360116499507662907794364600585194199856016264790761532103872755712699251827568798930276176114616254935649590379804583818232336861201624373656984670378585330527583333793990752166069238053369887956513728559388349989470741618155012539706464817194670834819721448889879067650379590366967249499254527903372963616265897603949857674139735944102374432970935547798262961459144293645142861715858733974679189757121195618738578364475844842355558105002561149239151889309946342841393608038309166281881150371528496705974162562823609216807515017772538740256425347087908913729172282861151591568372524163077225440633787593105982676094420326192428531701878177296023541306067213604600038966109364709514141718577701418060644363681546444005331608778314317444081194942297559931401188868331483280270655383300469329011574414756313999722') x = Decimal(n*e) getcontext().prec=4100 print x ```

For n=1 it gives 2.718281828459045235360287471
I want the answer for this to be all the digits in e.

I suggest first count how many numbers are after the decimal point. Then multiply with using integers. For example, you have

``````>>>a = 1111111111.2222222222
>>>b = 2222222222.333333333
``````

Then there are 10 numbers after the decimal point of a and 9 numbers after the decimal point of b, 19 in total. Then use

``````>>>c = 11111111112222222222*2222222222333333333
``````

Then c will be 24691358028395061724320987653925925926, 19 numbers are after the decimal point, so you answer for a * b will be 2469135802839506172.4320987653925925926 The position of decimal point in c could be found by converting c into str.

I summarized in the following function:

``````def multi(a, b):
"""
"""
a = str(a)
b = str(b)
num = 0
tmp = a.find('.')
if tmp != -1:
num = num + len(a) - tmp - 1
tmp = b.find('.')
if tmp != -1:
num = num  + len(b) - tmp -1
a1 = a.replace(".", "")
b1 = b.replace(".", "")
a1 = int(a1)
b1 = int(b1)
c = a1 * b1
ans = [c, num]
c = str(c)
res = c[:len(c)-num]
tmp = c[len(c)-num:]
res = res + '.' + tmp
return res, ans
``````

Two returns, the first is the str format of the float, the second is an list consist of two numbers, the first is integer and the second is the number of numbers after the decimal point.

Hope this helps you!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download