from typing import Union
import math
from decimal import Decimal, getcontext, setcontext, ROUND_UP, ROUND_05UP, ROUND_HALF_EVEN, ROUND_HALF_UP, Context
#print(#f'{dir(math)}\n'
#f'{dir(Decimal)}\n')
print(f'Decimal math.exp (for example):\n'
f'{Decimal(math.exp(-4.0093*(10**(-4)))):.5f}\n')
def exp_func(number: Union[int, float, Decimal]) -> Union[int, float, Decimal]:
'''Exponential function
with exception handling,
return integer or float
'''
try:
if isinstance(number, int) or isinstance(number, float):
return round(math.exp(number), 5)
elif isinstance(number, Decimal):
return Decimal(number).exp()
else:
raise ValueError('Number must be an integer, float or Decimal')
except ValueError as V:
return f'Error!!!\n{str(V)}'
print(f'exp_func floats:\n'
f'{exp_func(-9.3148*(10**(-4)))}\n')
print('***raise ValueError manually:','\n',exp_func('boobs'),'\n')
ef1 = exp_func(-4.6574/10000)
ef2 = exp_func(Decimal('-0.00046574'))
print(f'x = {ef1 = :.5f}\n'
f'square root of \'x\' = {math.sqrt(ef1):.5f}\n')
print(f'x = {ef2 = }\n')
print(f'Annotation of function {exp_func.__name__}:\n{exp_func.__doc__}')
def P_limits(l1_high: float=0.1006, l1_low: float=0.0433, N: int=16) -> tuple:
k2 = 108 # quantity of actuations
lambda_line = l1_high/k2
lambda_roof = l1_low/k2
sigma_lambda = math.sqrt(lambda_roof/(N*k2))
sigma_q = (1 - lambda_roof)*sigma_lambda*1
P_high = exp_func(lambda_roof*(-1))
P_low = exp_func(lambda_line*(-1))
return P_high, P_low
#return sigma_lambda
#return sigma_q
P_h, P_l = P_limits()
print(f'high limit of reliability:\n'
f'P_high = {P_h:.5f}\n'
f'low limit of reliability:\n'
f'P_low = {P_l:.5f}\n')
#s_l = P_limits()
#print(s_l)
#s_q = P_limits()
#print(s_q)
c = getcontext()
context2 = getcontext().prec=6
print(f'{Decimal(str(ef1)):.10f}')
print(Decimal(str(P_l)))
print(Decimal(str(P_h)))
print(c,'\n', context2)
q_raschet = Decimal('0.02') * (Decimal('10')**Decimal('-6'))
q_sum = Decimal('0.40093') * (Decimal('10')**Decimal('-3'))
k_ss = q_raschet/q_sum
lambda_ss = k_ss * q_sum
print(f'\u03BB_cc = {lambda_ss}')
print(f'floats: {(0.70 + 1.05):.5f}')
#print((Decimal('0.1') + Decimal('0.2')).quantize(Decimal('.00000'), rounding=ROUND_HALF_EVEN))
print(f'Decimals with precision={context2}:')
print(Decimal('0.123455') + Decimal('0.24545665'))
print(f'{ef2 = }')
print((ef2))
#setcontext(Context(prec=2))
print(f'Decimal quantize:')
print((Decimal('-2.745')).exp().quantize(Decimal('.00000'), rounding=ROUND_05UP))
print(Decimal('0.1234567'))
#print(help(Decimal.sqrt))
print()
To embed this program on your website, copy the following code and paste it into your website's HTML: