-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: Add tool for generating float24 printtable
- Loading branch information
Makman2
committed
Sep 18, 2016
1 parent
73fafe3
commit 8d52a1e
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from math import ceil, log2 | ||
import sys | ||
|
||
from float24 import bin_to_int | ||
|
||
|
||
digits = 2 | ||
relative_precision = 0.05 | ||
label = 'float24printtable' | ||
|
||
|
||
def print_table(): | ||
needed_precision = -ceil(log2(relative_precision**digits)) | ||
|
||
combinations = int(2**needed_precision) | ||
tablesize = digits * 2**needed_precision | ||
print('; {} most significant bits required.'.format(needed_precision)) | ||
print('; This table requires {} bytes.'.format(tablesize)) | ||
|
||
print('{}:'.format(label)) | ||
|
||
digit_list = [] | ||
for x in range(2**needed_precision): | ||
xbin = bin(x)[2:] | ||
# Pad with zeros if necessary. | ||
xbin = '0' * (needed_precision - len(xbin)) + xbin | ||
db = ('{:.' + str(digits) + 'f}').format( | ||
round(bin_to_int('0.' + xbin), digits))[2:] | ||
|
||
digit_list.append(db) | ||
|
||
# Convert trailing zero values to '9999...', as we accidentally round over | ||
# which would result into an absolute error of 1! | ||
first_nines = next(i | ||
for i, elem in | ||
enumerate(reversed(digit_list)) if elem != '0' * digits) | ||
|
||
if first_nines != 0: | ||
digit_list = digit_list[:-first_nines] + ['9' * digits] * first_nines | ||
|
||
# Assemble lines | ||
for db in digit_list: | ||
print(' ' * 4 + '.db "{}"'.format(db)) | ||
|
||
|
||
def main(): | ||
print_table() | ||
|
||
|
||
if __name__ == '__main__': | ||
sys.exit(main()) |