I had to compute 2 to the power of 8635. I came across this error when i was computing 2^8635. Any suggestion how to solve this in python. Using Decimal module also didn't help.
math.exp(2**8635)
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
long(math.exp(2**8635))
OverflowError: long int too large to convert to float
Handle Overflow Error exception in Python using the try-except block. An OverflowError exception is raised when an arithmetic operation exceeds the limits to be represented. This is part of the ArithmeticError Exception class.
The Python "OverflowError: integer division result too large for a float" occurs when the result of a division is too large. Use the floor division // operator to solve the error, e.g. result = large_num // 5 .
A long is an integer type value that has unlimited length. By converting a string into long we are translating the value of string type to long type. In Python3 int is upgraded to long by default which means that all the integers are long in Python3. So we can use int() to convert a string to long in Python.
You can calculate exp(2**8635)
using the mpmath arbitrary precision mathematics module:
>>> from mpmath import mp
>>> mp.exp(2**mp.mpf(8635))
The result is rather large, even in scientific notation, so I did a little bit of string processing on it, by copy & pasting the exponent into a string s
and then reformatting it with:
>>> ' '.join([''.join(b) for b in zip(*[iter(s)]*10)])
The result, using a precision of mp.dps=20
is
4.2745087809955028863988E+ 1075961842 0748495519 2355783856 9202899773 5836295555 7242537851 6693465178 9188332936 7912863215 8269919370 9351573282 2091597135 7873818954 8423038425 5471700446 0909815025 4915349824 6380686906 6065142412 7999166978 5222679350 6349971415 2079241766 6069616716 5214806906 8965805654 9049342395 4257087497 9202056938 3115899033 9617474292 7677302965 5753590474 3880492978 3310677063 3380119492 7251778893 3532333942 0392908196 6724211094 8421111629 7159351144 1795137181 2264799291 0856206603 1305457648 0092179442 8275386674 9796135348 4212345607 2596906933 7416259320 1466097812 2578572779 3349115312 8876785712 0147507083 6261226991 0422767260 2197316370 2566751899 4709067324 2810166509 7120800169 7485704027 5902848998 8322436088 0980908767 3758921061 0035161163 7079806523 1330002249 6901129466 0981598858 6254175013 7068344219 8144416569 4188190080 3725722501 2061326523 2288670566 9409102206 8066363145 9074202764 8831634730 7112255017 0796958442 3491574250 3284769002 8003076564 8628481483 0439060173 9895022099 1254178687 3665797407 1814255571 0142720172 8605229862 9458410467 4672777325 9445377886 5938198959 1752282268 4759702578 8305033658 4673474227 0701134707 7350267519 5446056950 6855231007 2274227123 3745529068 2622945712 5542911110 4405852782 0415855498 0153628764 5474618275 7401652756 8947182158 7839446628 2805776521 9227970761 2076840252 6317298197 2928759787 3166056075 4718446805 4085909927 3696546013 4104199677 6666307004 0490874208 2916409726 3519895965 9647475416 4225305349 6334839659 5665063673 3375427908 5570705561 0968733246 7656866381 6185956211 6475910345 4507247539 2187977091 2735607271 0209770526 5707982249 7378999875 8729821041 8908550791 3750558609 2896112135 9430559215 0044675914 5643599469 1254786634 5242493728 3502270440 3874100770 4799209491 3826434565 6967844371 7035989909 7698785578 9911981690 6907193705 6149599295 7385801544 7008446783 4130808551 5517628873 4403074667 6152264003 5246435484 0092524145 7903102870 5636975728 9853241717 1112914752 3204974235 3214176498 6118461362 7598845054 2855525271 2496489615 8680550278 4583386686 7070831392 2705583569 2224816732 3325036729 8106736491 3995562245 2081730125 0118717575 0946706166 8838482200 0110510511 5193674369 7359353196 7070446633 3514990982 6428484539 3692837968 6448096531 0406301917 5207050949 5578924420 0588400753 2802517808 1953501443 3445927237 8496133188 6299359291 6240744112 8041383838 2523556917 1966060664 8100342007 9597089073 1150067540 7733669287 2770028938 5557716323 9545436149 8913712891 0452403558 6380308865 8375826610 3861873050 5349593724 7443661231 6344789431 9248320333 3901279921 2985101147 6464375714 1302826973 5468251818 0411981792 4830271478 1578285904 7739571097 8513979667 0471302144 9500998430 2500602635 1255157768 2597448484 2711867118 3605984621 3175011866 9369283240 7857863664 6903750271 5353454119 0302118673
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With