This question already has answers here:
Integer square root in python
(14 answers)
Closed 9 years ago.
I am trying to get the square root of a really large number yet I get the error:
deltaSqrt = pow(delta,0.5)
OverflowError: long int too large to convert to float
In my case delta is equal to:
5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216
What should I do to get the square root of this number?
Use decimal:
import decimal
>>> d = decimal.Decimal('5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216')
>>> d.sqrt()
Decimal('7.139694782779097001143800270E+307')
If nothing else works, try this:
http://code.google.com/p/gmpy/
Related
This question already has answers here:
How to convert exponent in Python and get rid of the 'e+'?
(2 answers)
Closed 3 months ago.
I want to convert numbers like 1.28e+21 to a long digits only number but the following code doesn't make a difference.
n = 1.28e+21 b = 1.28*10**21 print(b)
b still has an e.
How do I get rid of e?
These numbers in exponential format are from type float in python.You can use int to convert it to an integer.
>>> n = int(1.28e+21)
>>> n
1280000000000000000000
You can also use decimal module like this:
>>> import decimal
>>> decimal.Decimal(1.28e+21)
Decimal('1280000000000000000000')
>>>
This question already has answers here:
Converting hex string representation to float in python
(2 answers)
Closed 2 years ago.
Is there a way to reverse the hex() method of a float in Python? For example,
n = 1280.03125
n_hex = n.hex()
print(n_hex) # result--> 0x1.4002000000000p+10
How can I convert 0x1.4002000000000p+10 back to 1280.03125? I know you can use int(num, base) to convert a number to integer but it doesn't support decimal.
Try float.fromhex(str):
>>> float.fromhex("0x1.4002000000000p+10")
1280.03125
This question already has answers here:
Safest way to convert float to integer in python?
(9 answers)
How do I parse a string to a float or int?
(32 answers)
How do you round UP a number?
(28 answers)
Closed 2 years ago.
I used the below code to calculate the average of an attribute
from pyspark.sql import functions as F
from pyspark.sql.functions import mean
result = df.select([mean("Age")])
result.show()
I got the output as 56.4567 i need to convert it into an integer
If you want the result as int and not df run
result = round(df.select(mean("Age")).collect()[0][0])
result will be of int type.
result_as_integer = int(result)
or
result_as_float = float(result)
First you need to convert pyspark dataframe result to real number:
result = result.take(1)[0].asDict()['avg(Age)']
or
result = result.collect()[0]['avg(Age)']
or
result = result.collect()[0][0]
if you need the floor of the number:
import math
math.floor(float(result))
#56
if you need the ceiling of the number:
import math
math.ceil(float(result))
#57
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Closed 3 years ago.
Pretty new to python, facing a problem that requires basically the opposite of the remainder "%" function. For example, if I wanted to divide 81.5 by 20, my output would be 4. My best attempt is as follows:
amount = 81.504
round(amount, 2)
num20s = amount / 20
int(num20s)
I've tried several different combinations of the above code, but nothing has worked so far. This is the closest I've gotten to what I want, but it won't work in edge cases, and for some reason still represents the number with a ".0" at the end, so that last line must not be doing anything.
Integer division operator in python is "//".
>>> amount = 81.504
>>> amount // 20
Out[3]: 4.0
>>> int(amount // 20)
Out[4]: 4
This question already has answers here:
Python: Converting string into decimal number
(8 answers)
Closed 4 years ago.
I have a number 3.8148116e-09
How do I convert it to a real number without the - ?
Thanks.
You can try:
>>> a = "3.8148116e-09"
>>> number = float(a)
>>> print "{:1.16f}".format(number)
0.0000000038148116
The first line parses the string as a number. If you need to print the number or format it for another reason, you can use string#format.