OverflowError: int too large to convert to float for p = 10**200-1 and q = -10**200
As the error points out, the int is too large to be converted to a float normally.
Python has this module named decimal, you could use decimal.Decimal to do your float conversions.
from decimal import Decimal
def ec_grad2(p, q):
delta = p ** 2 - 4 * q
sqrt_delta = Decimal(delta) ** Decimal(0.5)
if delta < 0:
return "No real solutions"
elif delta == 0:
x1 = -p / 2
x2 = x1
return (x1, x2)
elif delta > 0:
x1 = -2 * q / (p + sqrt_delta)
x2 = 2 * q / (sqrt_delta - p)
return (x1, x2)
def printing(function_return):
if type(function_return) == str:
print(function_return)
else:
a, b = function_return
print("x1 = {:.20f} si x2 = {:.20f}".format(a, b))
This should work for you
OverflowError: int too large to convert to float - Python
According to Python Tutorial in cases where precision is important it's better to use decimal
or fractions
modules.
For example, instead of writing f2 = 1/(exp+1)
you should write
from fractions import Fraction
f2 = Fraction(1, exp+1)
Read this article to get a better understanding.
Note that doing such heavy computations is not recommended in Python itself even with built-in libraries like fractions
. You should use libraries such as NumPy for better performance and also better precision.
Python OverflowError: int too large to convert to float
The best solution to this question runs in linear time. Here's the idea:
let's look at a shorter example: 1234
consider the total sum of the substrings which end on the ith digit (0..3)
substringsum[0]: 1 (we have only a single substring)
substringsum[1]: 2 + 12 (two substrings)
substringsum[2]: 3+23+123 (three substrings)
substringsum[3]: 4 + 34+234+1234
see a pattern?
let's look at substringsum[2]:
3 + 23 + 123 = 3 + 20+3 + 120+3 = 3*3 + 20+120 = 3*3 + 10*(2+12) = 3*3 +10*substringsum[1]
in general:
substringsum[k] = (k+1)*digit[k] + 10 * substringsum[k-1]
This you can compute in linear time.
This type of ideas is called "Dynamic Programming"
OverflowError: int too large to convert to float
For this problem you should not need to floating-point numbers at all.
You probably didn't intend this, but accidentally used them by the line
r = r / 10
because this is the floating-point division operator.
To remove the last digit from a number, use integer division:
r = r // 10
Related Topics
Spliting a Row to Multiple Row Pyspark
Plotting Data from Multiple Pandas Data Frames in One Plot
How to Pad a String With Leading Zeros in Python 3
Pandas: Calculate Total Percent Difference Between Two Data Frames
How to Prevent Brokenpipeerror When Doing a Flush in Python
How to Read a Column Without Header from CSV and Save the Output in a Txt File Using Python
Calculate the Lcm of a List of Given Numbers in Python
Get Usb Device Address Through Python
Visual Studio Code Pylint: Unable to Import 'Protorpc'
How to Read a List of Parquet Files from S3 as a Pandas Dataframe Using Pyarrow
Python Pip Install Fails: Invalid Command Egg_Info
Creating Random Pairs from Lists
How to Test Multiple Variables for Equality Against a Single Value
Splitting Strings into Numbers (Python)
Efficient Date Range Overlap Calculation