'Test accuracy of math.hypot()' from math import hypot, fsum, sqrt from random import expovariate from decimal import Decimal from sys import float_info from collections import Counter from pprint import pprint EPS = float_info.epsilon hist = Counter() def althypot(*args): scale = max(map(abs, args)) return scale * sqrt(fsum((x/scale)**2.0 for x in args)) k = 100 n = 100_000 reverse = True # hypot = althypot for i in range(n): args = [expovariate(1.0) for i in range(k)] args.sort(reverse=reverse) expected = float(sum(x**2 for x in map(Decimal, args)).sqrt()) actual = hypot(*args) ulps = ((actual / expected) - 1.0) / EPS hist[ulps] += 1 #print(expected.hex(), actual.hex(), ulps) print(f'k={k}, n={n}, reverse={reverse!r}') pprint(hist.most_common())