P
P
Programist189462021-09-03 00:21:31
Python
Programist18946, 2021-09-03 00:21:31

How to fix resulttoo large in code?

import math


a = int(input("Введите а "))
alpha = int(input("Введите альфа "))
x = int(input("Введите х "))
y1 = math.log(abs(x**3)) + math.tan(alpha)-pow(math.e, a*(x**2)+x)
y2 = math.log10(abs(a**7))+ math.atan(x**2)+((math.pi +4.56* 10**-4)/(math.sqrt(math.sqrt(abs(a+x)))))
print(y1 , y2)

Complains that y1 is a huge number, please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-09-03
@Programist18946

Try to use decimal everywhere:

import math

a, alpha, x = 10, 10, 10

y1 = math.log(abs(x**3)) + math.tan(alpha)-pow(math.e, a*(x**2)+x)

print(y1)

# OverflowError: (34, 'Result too large')

The same formula wrapped in decimal.
Looks cumbersome, but works correctly:
import math
import decimal

decimal.getcontext().prec = 100

a, alpha, x = 10, 10, 10

y1 = decimal.Decimal(math.log(decimal.Decimal(abs(decimal.Decimal(x)**decimal.Decimal(3))))) \
    + decimal.Decimal(math.tan(decimal.Decimal(alpha))) \
    - decimal.Decimal(pow(decimal.Decimal(math.e), decimal.Decimal(a)*(decimal.Decimal(x)**decimal.Decimal(2))+decimal.Decimal(x)))

print(y1)

# -4.339370400623091759291109148627508614055728677394889807059531826625028640984354352252576981241604834E+438

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question