annotate Lib/fpformat.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents bae889f44cd1
children 71a294bd7950
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11271
diff changeset
1 """General floating point formatting functions.
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11271
diff changeset
3 Functions:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11271
diff changeset
4 fix(x, digits_behind)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11271
diff changeset
5 sci(x, digits_behind)
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11271
diff changeset
7 Each takes a number or a string and a number of digits as arguments.
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11271
diff changeset
9 Parameters:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11271
diff changeset
10 x: number to be formatted; or a string resembling a number
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11271
diff changeset
11 digits_behind: number of digits behind the decimal point
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11271
diff changeset
12 """
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 704
diff changeset
14 import re
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
16072
bae889f44cd1 more __all__ updates
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
16 __all__ = ["fix","sci","NotANumber"]
bae889f44cd1 more __all__ updates
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
17
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # Compiled regular expression to "decode" a number
6524
e9c67c192cdb re -> regex conversions by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
19 decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # \0 the whole thing
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # \1 leading sign or empty
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # \2 digits left of decimal point
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # \3 fraction (empty or begins with point)
6524
e9c67c192cdb re -> regex conversions by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
24 # \4 exponent part (empty or begins with 'e' or 'E')
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
11073
a84f9d3999ae Define NotANumber as a subclass of ValueError when using class-based
Fred Drake <fdrake@acm.org>
parents: 6524
diff changeset
26 try:
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
27 class NotANumber(ValueError):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
28 pass
11073
a84f9d3999ae Define NotANumber as a subclass of ValueError when using class-based
Fred Drake <fdrake@acm.org>
parents: 6524
diff changeset
29 except TypeError:
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
30 NotANumber = 'fpformat.NotANumber'
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 def extract(s):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
33 """Return (sign, intpart, fraction, expo) or raise an exception:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
34 sign is '+' or '-'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
35 intpart is 0 or more digits beginning with a nonzero
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
36 fraction is 0 or more digits
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
37 expo is an integer"""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
38 res = decoder.match(s)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
39 if res is None: raise NotANumber, s
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
40 sign, intpart, fraction, exppart = res.group(1,2,3,4)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
41 if sign == '+': sign = ''
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
42 if fraction: fraction = fraction[1:]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
43 if exppart: expo = int(exppart[1:])
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
44 else: expo = 0
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
45 return sign, intpart, fraction, expo
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 def unexpo(intpart, fraction, expo):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
48 """Remove the exponent by changing intpart and fraction."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
49 if expo > 0: # Move the point left
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
50 f = len(fraction)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
51 intpart, fraction = intpart + fraction[:expo], fraction[expo:]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
52 if expo > f:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
53 intpart = intpart + '0'*(expo-f)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
54 elif expo < 0: # Move the point right
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
55 i = len(intpart)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
56 intpart, fraction = intpart[:expo], intpart[expo:] + fraction
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
57 if expo < -i:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
58 fraction = '0'*(-expo-i) + fraction
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
59 return intpart, fraction
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 def roundfrac(intpart, fraction, digs):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
62 """Round or extend the fraction to size digs."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
63 f = len(fraction)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
64 if f <= digs:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
65 return intpart, fraction + '0'*(digs-f)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
66 i = len(intpart)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
67 if i+digs < 0:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
68 return '0'*-digs, ''
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
69 total = intpart + fraction
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
70 nextdigit = total[i+digs]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
71 if nextdigit >= '5': # Hard case: increment last digit, may have carry!
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
72 n = i + digs - 1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
73 while n >= 0:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
74 if total[n] != '9': break
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
75 n = n-1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
76 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
77 total = '0' + total
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
78 i = i+1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
79 n = 0
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
80 total = total[:n] + chr(ord(total[n]) + 1) + '0'*(len(total)-n-1)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
81 intpart, fraction = total[:i], total[i:]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
82 if digs >= 0:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
83 return intpart, fraction[:digs]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
84 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
85 return intpart[:digs] + '0'*-digs, ''
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 def fix(x, digs):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
88 """Format x as [-]ddd.ddd with 'digs' digits after the point
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
89 and at least one digit before.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
90 If digs <= 0, the point is suppressed."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
91 if type(x) != type(''): x = `x`
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
92 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
93 sign, intpart, fraction, expo = extract(x)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
94 except NotANumber:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
95 return x
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
96 intpart, fraction = unexpo(intpart, fraction, expo)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
97 intpart, fraction = roundfrac(intpart, fraction, digs)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
98 while intpart and intpart[0] == '0': intpart = intpart[1:]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
99 if intpart == '': intpart = '0'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
100 if digs > 0: return sign + intpart + '.' + fraction
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
101 else: return sign + intpart
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def sci(x, digs):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
104 """Format x as [-]d.dddE[+-]ddd with 'digs' digits after the point
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
105 and exactly one digit before.
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
106 If digs is <= 0, one digit is kept and the point is suppressed."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
107 if type(x) != type(''): x = `x`
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
108 sign, intpart, fraction, expo = extract(x)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
109 if not intpart:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
110 while fraction and fraction[0] == '0':
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
111 fraction = fraction[1:]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
112 expo = expo - 1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
113 if fraction:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
114 intpart, fraction = fraction[0], fraction[1:]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
115 expo = expo - 1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
116 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
117 intpart = '0'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
118 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
119 expo = expo + len(intpart) - 1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
120 intpart, fraction = intpart[0], intpart[1:] + fraction
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
121 digs = max(0, digs)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
122 intpart, fraction = roundfrac(intpart, fraction, digs)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
123 if len(intpart) > 1:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
124 intpart, fraction, expo = \
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
125 intpart[0], intpart[1:] + fraction[:-1], \
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
126 expo + len(intpart) - 1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
127 s = sign + intpart
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
128 if digs > 0: s = s + '.' + fraction
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
129 e = `abs(expo)`
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
130 e = '0'*(3-len(e)) + e
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
131 if expo < 0: e = '-' + e
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
132 else: e = '+' + e
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
133 return s + 'e' + e
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 def test():
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
136 """Interactive test run."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
137 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
138 while 1:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
139 x, digs = input('Enter (x, digs): ')
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
140 print x, fix(x, digs), sci(x, digs)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
141 except (EOFError, KeyboardInterrupt):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
142 pass