-
Notifications
You must be signed in to change notification settings - Fork 0
/
demonstration.py
executable file
·63 lines (51 loc) · 2.35 KB
/
demonstration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Eric Moyer github.com/epmoyer [email protected]'
from math import log10
from tabulate import tabulate
from to_precision import to_precision
def main():
"""
Displays a table containing example conversions for to_precision()
"""
# Build test values
seed = [float(int(123456789. / 10**x)) for x in range(7, -1, -1)]
test_values = ([0.0, 1.0, 10.0, 100.0, -1.0] +
[x for x in seed] +
[x / 10**int(log10(x)) for x in seed] +
[x / 10**9 for x in seed])
option_cases = (
('Default (Auto Notation)', dict()),
('Standard Notation', dict(notation='std')),
('Engineering Notation', dict(notation='eng')),
('Scientific Notation', dict(notation='sci')),
('Standard Notation with zero stripping', dict(notation='std', strip_zeros=True)),
('Scientific Notation with zero stripping', dict(notation='sci', strip_zeros=True)),
('Standard Notation with integer preservation', dict(notation='std', preserve_integer=True)),
('Auto Notation with exponent limit of 5', dict(auto_limit=5)),
)
precisions = tuple(range(1, 6))
# prints out the label, function call, and precision table
for options_description, options_dict in option_cases:
'''
Prints label for table.
Ex:
Default (Auto Notation):
to_precision(value, precision)
'''
print(options_description + ':')
options_string = ', '.join(
['value', 'precision'] +
[note + '=' + repr(inputs) for note, inputs in options_dict.items()])
print('to_precision({inputs})'.format(inputs=options_string), end='\n' * 2)
table = []
for val in test_values:
table_row = ['{:0.10f}'.format(val).rstrip('0').rstrip('.')]
for precision in precisions:
result_string = to_precision(val, precision, **options_dict)
table_row.append(result_string)
table.append(table_row)
headers = ['value'] + ['precision={}'.format(x) for x in precisions]
print(tabulate(table, headers, disable_numparse=True), end='\n' * 3)
if __name__ == '__main__':
main()