[ prog / sol / mona ]

prog


Challenge^2: Floating Point without Errors

7 2020-10-16 19:15
import decimal
import math
import sys

if sys.version_info[:3] < (3,):
   print('https://www.python.org/doc/sunset-python-2/')
   exit(-1)

decimal.getcontext().prec = 150
d = decimal.Decimal

print('task 1')

v = {'n-2' : d(2), 'n-1' : d(-4.0)}

for n in range(3, 101):
   v['n'] = 111 - 1130 / v['n-1'] + 3000 / (v['n-1'] * v['n-2'])
   v['n-2'], v['n-1'] = v['n-1'], v['n']
   if n in [3,4,5,6,7,8,20,30,50,100]:
      print('v[%d] = %.16f' % (n, v['n']))

print('task 2')

balance = d('2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746') - 1

for year in range(1,26):
   balance = balance * year - 1

print('Balance after %d years: %.16f' % (year, balance))

print('task 3')

a = d(77617.0)
b = d(33096.0)

def f(a,b):
   return d(333.75) * b**6 + a**2 * (11 * a**2 * b**2 - b**6 - 121 * b**4 - 2) + d(5.5) * b**8 + a / (2 * b)

print('f(%.1f, %.1f) = %.15f' % (a, b, f(a, b)))
59


VIP:

do not edit these