aboutsummaryrefslogtreecommitdiff
path: root/support
diff options
context:
space:
mode:
authorVictor Zverovich <victor.zverovich@gmail.com>2018-04-29 06:33:05 -0700
committerVictor Zverovich <victor.zverovich@gmail.com>2018-04-29 06:33:05 -0700
commit2768af23888d1f598df5238b72fe7dbe9bf11640 (patch)
treed9244454245d39f0c38b4330193ef896019cb4be /support
parentdd296e1de08e512d5f308736e9f5767c7034d457 (diff)
downloadfmtlib-2768af23888d1f598df5238b72fe7dbe9bf11640.tar.gz
Add cached powers of 10
Diffstat (limited to 'support')
-rwxr-xr-xsupport/compute-powers.py42
1 files changed, 38 insertions, 4 deletions
diff --git a/support/compute-powers.py b/support/compute-powers.py
index db541be8..a68197d2 100755
--- a/support/compute-powers.py
+++ b/support/compute-powers.py
@@ -8,9 +8,43 @@ min_exponent = -348
max_exponent = 340
step = 8
significand_size = 64
-for exp in range(min_exponent, max_exponent + 1, step):
- n = 10 ** exp if exp >= 0 else 2 ** 2000 / 10 ** -exp
+exp_offset = 2000
+
+class fp:
+ pass
+
+powers = []
+for i, exp in enumerate(range(min_exponent, max_exponent + 1, step)):
+ result = fp()
+ n = 10 ** exp if exp >= 0 else 2 ** exp_offset / 10 ** -exp
k = significand_size + 1
# Convert to binary and round.
- n = (int('{:0<{}b}'.format(n, k)[:k], 2) + 1) / 2
- print('{:0<#16x}ull'.format(n))
+ binary = '{:b}'.format(n)
+ result.f = (int('{:0<{}}'.format(binary[:k], k), 2) + 1) / 2
+ result.e = len(binary) - (exp_offset if exp < 0 else 0) - significand_size
+ powers.append(result)
+ # Sanity check.
+ exp_offset10 = 400
+ actual = result.f * 10 ** exp_offset10
+ if result.e > 0:
+ actual *= 2 ** result.e
+ else:
+ for j in range(-result.e):
+ actual /= 2
+ expected = 10 ** (exp_offset10 + exp)
+ precision = len('{}'.format(expected)) - len('{}'.format(actual - expected))
+ if precision < 19:
+ print('low precision:', precision)
+ exit(1)
+
+print('Significands:', end='')
+for i, fp in enumerate(powers):
+ if i % 3 == 0:
+ print(end='\n ')
+ print(' {:0<#16x}ull'.format(fp.f, ), end=',')
+
+print('\n\nExponents:', end='')
+for i, fp in enumerate(powers):
+ if i % 11 == 0:
+ print(end='\n ')
+ print(' {:5}'.format(fp.e), end=',')