I am trying to find out the square root of a number. But the numbers are large. Python gives the error
OverflowError: int too large to convert to float
My code is here:
number=pow(255,200)-10000000
sqrt=math.sqrt(number)
i tried it with the decimal class but it doesn't work.
How to handle these large numbers in python?
import math
pow(10, 1/2 * math.log10(pow(255,200)-10000000))
The result is: 4.508354347658245e+240
Related
I'm trying to construct a neural network for the Mnist database. When computing the softmax function I receive an error to the same ends as "you can't store a float that size"
code is as follows:
def softmax(vector): # REQUIRES a unidimensional numpy array
adjustedVals = [0] * len(vector)
totalExp = np.exp(vector)
print("totalExp equals")
print(totalExp)
totalSum = totalExp.sum()
for i in range(len(vector)):
adjustedVals[i] = (np.exp(vector[i])) / totalSum
return adjustedVals # this throws back an error sometimes?!?!
After inspection, most recommend using the decimal module. However when I've messed around with the values being used in the command line with this module, that is:
from decimal import Decimal
import math
test = Decimal(math.exp(720))
I receive a similar error for any values which are math.exp(>709).
OverflowError: (34, 'Numerical result out of range')
My conclusion is that even decimal cannot handle this number. Does anyone know of another method I could use to represent these very large floats.
There is a technique which makes the softmax function more feasible computationally for a certain kind of value distribution in your vector. Namely, you can subtract the maximum value in the vector (let's call it x_max) from each of its elements. If you recall the softmax formula, such operation doesn't affect the outcome as it reduced to multiplication of the result by e^(x_max) / e^(x_max) = 1. This way the highest intermediate value you get is e^(x_max - x_max) = 1 so you avoid the overflow.
For additional explanation I recommend the following article: https://nolanbconaway.github.io/blog/2017/softmax-numpy
With a value above 709 the function 'math.exp' exceeds the floating point range and throws this overflow error.
If, instead of math.exp, you use numpy.exp for such large exponents you will see that it evaluates to the special value inf (infinity).
All this apart, I wonder why you would want to produce such a big number (not sure you are aware how big it is. Just to give you an idea, the number of atoms in the universe is estimated to be in the range of 10 to the power of 80. The number you are trying to produce is MUCH larger than that).
Using pandas to deal with timestamps, I am concatening two columns and then convert the result in floating. It appears that when I display the two columns I observe two different results. How can the conversion from string to float can affect the value? Thanks for your help.
Here is the content of the data.csv file
epoch_day,epoch_ns
1533081601,224423000
Here is my test program:
import pandas as pd
pd.options.display.float_format = '{:.10f}'.format
df_mid = pd.read_csv("data.csv")
df_mid['result_1']=df_mid['epoch_day'].astype(str).str.cat(df_mid['epoch_ns'].astype(str), sep =".")
df_mid['result_2'] = df_mid['epoch_day'].astype(str).str.cat(df_mid['epoch_ns'].astype(str), sep =".").astype(float)
print(df_mid)
The result is :
epoch_day epoch_ns result_1 result_2
0 1533081601 224423000 1533081601.224423000 1533081601.2244229317
Thanks for your help
FX
Floating-point numbers are represented in computer hardware as base 2 (binary) fractions. Most decimal fractions cannot be represented exactly as binary fractions.
When you convert your string, python creates a float which is the closest binary fraction for your input.
You can actually see to which decimal number this corresponds by running the following:
from decimal import Decimal
Decimal(1533081601.224423000)
OUTPUT: Decimal('1533081601.224422931671142578125')
You can see the Python documentation for more info https://docs.python.org/2/tutorial/floatingpoint.html
So, I've learned quite a few ways to control the precision when I'm dealing with floats.
Here is an example of 3 different techniques:
somefloat=0.0123456789
print("{0:.10f}".format(somefloat))
print("%.5f" % somefloat)
print(Decimal(somefloat).quantize(Decimal(".01")))
This will print:
0.0123456789
0.01235
0.01
In all of the above examples, the precision itself is a fixed value, but how could I turn the precision itself a variable that could be
be entered by the end-user?
I mean, the fixed precision values are now inside quatations marks, and I can't seem to find a way to add any variable there. Is there a way, anyway?
I'm on Python 3.
Using format:
somefloat=0.0123456789
precision = 5
print("{0:.{1}f}".format(somefloat, precision))
# 0.01235
Using old-style string interpolation:
print("%.*f" % (precision, somefloat))
# 0.01235
Using decimal:
import decimal
D = decimal.Decimal
q = D(10) ** -precision
print(D(somefloat).quantize(q))
# 0.01235
I am using python 3.4
and I want to limiting the a float number to two decimal points
round(1.2377, 2)
format(1.2377, '.2f')
These two would give my 1.24, but I don't want 1.24, I need 1.23, how do I do it?
You can convert to string and slice then convert to float :
>>> num=1.2377
>>> float(str(num)[:-2])
1.23
read more about Floating Point Arithmetic: Issues and Limitations
I've been checking some topics around here about the same problem I'm getting but they don't seem to help.
My problem is when I try to execute the following code, I get the error found in the title. How do I go around this?
d=2
while(n != 1):
n = 2
d = (math.sqrt(2 + d))
n= (n/d)
f = (f * (n))
print (f)
That's because math.sqrt, as a consequence of using the C sqrt function, works on floating point number which are not unlimited in size. Python is unable to convert the long integer into a floating point number because it is to big.
See this question on ways to square root large integers.
Better, you could use the decimal module, which is an unlimited size number type stored in base-10. Use decimal.Decimal(number).sqrt() to find the sqrt of a number.