Clarify portability and main program.
[python/dscho.git] / Lib / fpformat.py
blob8ddd73cf9019ad8f0b91a755e95a5cb2b86aba70
1 # General floating point formatting functions.
3 # Functions:
4 # fix(x, digits_behind)
5 # sci(x, digits_behind)
7 # Each takes a number or a string and a number of digits as arguments.
9 # Parameters:
10 # x: number to be formatted; or a string resembling a number
11 # digits_behind: number of digits behind the decimal point
14 import re
16 # Compiled regular expression to "decode" a number
17 decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
18 # \0 the whole thing
19 # \1 leading sign or empty
20 # \2 digits left of decimal point
21 # \3 fraction (empty or begins with point)
22 # \4 exponent part (empty or begins with 'e' or 'E')
24 NotANumber = 'fpformat.NotANumber'
26 # Return (sign, intpart, fraction, expo) or raise an exception:
27 # sign is '+' or '-'
28 # intpart is 0 or more digits beginning with a nonzero
29 # fraction is 0 or more digits
30 # expo is an integer
31 def extract(s):
32 res = decoder.match(s)
33 if res is None: raise NotANumber
34 sign, intpart, fraction, exppart = res.group(1,2,3,4)
35 if sign == '+': sign = ''
36 if fraction: fraction = fraction[1:]
37 if exppart: expo = eval(exppart[1:])
38 else: expo = 0
39 return sign, intpart, fraction, expo
41 # Remove the exponent by changing intpart and fraction
42 def unexpo(intpart, fraction, expo):
43 if expo > 0: # Move the point left
44 f = len(fraction)
45 intpart, fraction = intpart + fraction[:expo], fraction[expo:]
46 if expo > f:
47 intpart = intpart + '0'*(expo-f)
48 elif expo < 0: # Move the point right
49 i = len(intpart)
50 intpart, fraction = intpart[:expo], intpart[expo:] + fraction
51 if expo < -i:
52 fraction = '0'*(-expo-i) + fraction
53 return intpart, fraction
55 # Round or extend the fraction to size digs
56 def roundfrac(intpart, fraction, digs):
57 f = len(fraction)
58 if f <= digs:
59 return intpart, fraction + '0'*(digs-f)
60 i = len(intpart)
61 if i+digs < 0:
62 return '0'*-digs, ''
63 total = intpart + fraction
64 nextdigit = total[i+digs]
65 if nextdigit >= '5': # Hard case: increment last digit, may have carry!
66 n = i + digs - 1
67 while n >= 0:
68 if total[n] != '9': break
69 n = n-1
70 else:
71 total = '0' + total
72 i = i+1
73 n = 0
74 total = total[:n] + chr(ord(total[n]) + 1) + '0'*(len(total)-n-1)
75 intpart, fraction = total[:i], total[i:]
76 if digs >= 0:
77 return intpart, fraction[:digs]
78 else:
79 return intpart[:digs] + '0'*-digs, ''
81 # Format x as [-]ddd.ddd with 'digs' digits after the point
82 # and at least one digit before.
83 # If digs <= 0, the point is suppressed.
84 def fix(x, digs):
85 if type(x) != type(''): x = `x`
86 try:
87 sign, intpart, fraction, expo = extract(x)
88 except NotANumber:
89 return x
90 intpart, fraction = unexpo(intpart, fraction, expo)
91 intpart, fraction = roundfrac(intpart, fraction, digs)
92 while intpart and intpart[0] == '0': intpart = intpart[1:]
93 if intpart == '': intpart = '0'
94 if digs > 0: return sign + intpart + '.' + fraction
95 else: return sign + intpart
97 # Format x as [-]d.dddE[+-]ddd with 'digs' digits after the point
98 # and exactly one digit before.
99 # If digs is <= 0, one digit is kept and the point is suppressed.
100 def sci(x, digs):
101 if type(x) != type(''): x = `x`
102 sign, intpart, fraction, expo = extract(x)
103 if not intpart:
104 while fraction and fraction[0] == '0':
105 fraction = fraction[1:]
106 expo = expo - 1
107 if fraction:
108 intpart, fraction = fraction[0], fraction[1:]
109 expo = expo - 1
110 else:
111 intpart = '0'
112 else:
113 expo = expo + len(intpart) - 1
114 intpart, fraction = intpart[0], intpart[1:] + fraction
115 digs = max(0, digs)
116 intpart, fraction = roundfrac(intpart, fraction, digs)
117 if len(intpart) > 1:
118 intpart, fraction, expo = \
119 intpart[0], intpart[1:] + fraction[:-1], \
120 expo + len(intpart) - 1
121 s = sign + intpart
122 if digs > 0: s = s + '.' + fraction
123 e = `abs(expo)`
124 e = '0'*(3-len(e)) + e
125 if expo < 0: e = '-' + e
126 else: e = '+' + e
127 return s + 'e' + e
129 # Interactive test run
130 def test():
131 try:
132 while 1:
133 x, digs = input('Enter (x, digs): ')
134 print x, fix(x, digs), sci(x, digs)
135 except (EOFError, KeyboardInterrupt):
136 pass