ยปCore Development>Code coverage>Lib/fpformat.py

Python code coverage for Lib/fpformat.py

#countcontent
1n/a"""General floating point formatting functions.
2n/a
3n/aFunctions:
4n/afix(x, digits_behind)
5n/asci(x, digits_behind)
6n/a
7n/aEach takes a number or a string and a number of digits as arguments.
8n/a
9n/aParameters:
10n/ax: number to be formatted; or a string resembling a number
11n/adigits_behind: number of digits behind the decimal point
121"""
131from warnings import warnpy3k
141warnpy3k("the fpformat module has been removed in Python 3.0", stacklevel=2)
151del warnpy3k
16n/a
171import re
18n/a
191__all__ = ["fix","sci","NotANumber"]
20n/a
21n/a# Compiled regular expression to "decode" a number
221decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
23n/a# \0 the whole thing
24n/a# \1 leading sign or empty
25n/a# \2 digits left of decimal point
26n/a# \3 fraction (empty or begins with point)
27n/a# \4 exponent part (empty or begins with 'e' or 'E')
28n/a
291try:
302 class NotANumber(ValueError):
311 pass
320except TypeError:
330 NotANumber = 'fpformat.NotANumber'
34n/a
351def extract(s):
36n/a """Return (sign, intpart, fraction, expo) or raise an exception:
37n/a sign is '+' or '-'
38n/a intpart is 0 or more digits beginning with a nonzero
39n/a fraction is 0 or more digits
40n/a expo is an integer"""
41342 res = decoder.match(s)
42342 if res is None: raise NotANumber, s
43340 sign, intpart, fraction, exppart = res.group(1,2,3,4)
44340 if sign == '+': sign = ''
45340 if fraction: fraction = fraction[1:]
46340 if exppart: expo = int(exppart[1:])
47312 else: expo = 0
48340 return sign, intpart, fraction, expo
49n/a
501def unexpo(intpart, fraction, expo):
51n/a """Remove the exponent by changing intpart and fraction."""
52170 if expo > 0: # Move the point left
530 f = len(fraction)
540 intpart, fraction = intpart + fraction[:expo], fraction[expo:]
550 if expo > f:
560 intpart = intpart + '0'*(expo-f)
57170 elif expo < 0: # Move the point right
5814 i = len(intpart)
5914 intpart, fraction = intpart[:expo], intpart[expo:] + fraction
6014 if expo < -i:
6114 fraction = '0'*(-expo-i) + fraction
62170 return intpart, fraction
63n/a
641def roundfrac(intpart, fraction, digs):
65n/a """Round or extend the fraction to size digs."""
66340 f = len(fraction)
67340 if f <= digs:
68199 return intpart, fraction + '0'*(digs-f)
69141 i = len(intpart)
70141 if i+digs < 0:
710 return '0'*-digs, ''
72141 total = intpart + fraction
73141 nextdigit = total[i+digs]
74141 if nextdigit >= '5': # Hard case: increment last digit, may have carry!
750 n = i + digs - 1
760 while n >= 0:
770 if total[n] != '9': break
780 n = n-1
79n/a else:
800 total = '0' + total
810 i = i+1
820 n = 0
830 total = total[:n] + chr(ord(total[n]) + 1) + '0'*(len(total)-n-1)
840 intpart, fraction = total[:i], total[i:]
85141 if digs >= 0:
86141 return intpart, fraction[:digs]
87n/a else:
880 return intpart[:digs] + '0'*-digs, ''
89n/a
901def fix(x, digs):
91n/a """Format x as [-]ddd.ddd with 'digs' digits after the point
92n/a and at least one digit before.
93n/a If digs <= 0, the point is suppressed."""
94171 if type(x) != type(''): x = repr(x)
95171 try:
96171 sign, intpart, fraction, expo = extract(x)
971 except NotANumber:
981 return x
99170 intpart, fraction = unexpo(intpart, fraction, expo)
100170 intpart, fraction = roundfrac(intpart, fraction, digs)
101170 while intpart and intpart[0] == '0': intpart = intpart[1:]
102170 if intpart == '': intpart = '0'
103170 if digs > 0: return sign + intpart + '.' + fraction
10424 else: return sign + intpart
105n/a
1061def sci(x, digs):
107n/a """Format x as [-]d.dddE[+-]ddd with 'digs' digits after the point
108n/a and exactly one digit before.
109n/a If digs is <= 0, one digit is kept and the point is suppressed."""
110171 if type(x) != type(''): x = repr(x)
111171 sign, intpart, fraction, expo = extract(x)
112170 if not intpart:
113182 while fraction and fraction[0] == '0':
114112 fraction = fraction[1:]
115112 expo = expo - 1
11670 if fraction:
11770 intpart, fraction = fraction[0], fraction[1:]
11870 expo = expo - 1
119n/a else:
1200 intpart = '0'
121n/a else:
122100 expo = expo + len(intpart) - 1
123100 intpart, fraction = intpart[0], intpart[1:] + fraction
124170 digs = max(0, digs)
125170 intpart, fraction = roundfrac(intpart, fraction, digs)
126170 if len(intpart) > 1:
127n/a intpart, fraction, expo = \
1280 intpart[0], intpart[1:] + fraction[:-1], \
1290 expo + len(intpart) - 1
130170 s = sign + intpart
131170 if digs > 0: s = s + '.' + fraction
132170 e = repr(abs(expo))
133170 e = '0'*(3-len(e)) + e
134170 if expo < 0: e = '-' + e
13586 else: e = '+' + e
136170 return s + 'e' + e
137n/a
1381def test():
139n/a """Interactive test run."""
1400 try:
1410 while 1:
1420 x, digs = input('Enter (x, digs): ')
1430 print x, fix(x, digs), sci(x, digs)
1440 except (EOFError, KeyboardInterrupt):
1450 pass