annotate Lib/fpformat.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 0c02e15a7c1f
children
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 """
46845
0c02e15a7c1f Deprecate the fpformat module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 31515
diff changeset
13 from warnings import warnpy3k
0c02e15a7c1f Deprecate the fpformat module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 31515
diff changeset
14 warnpy3k("the fpformat module has been removed in Python 3.0", stacklevel=2)
0c02e15a7c1f Deprecate the fpformat module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 31515
diff changeset
15 del warnpy3k
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
6519
24da035ceb6e Convert all remaining *simple* cases of regex usage to re usage.
Guido van Rossum <guido@python.org>
parents: 704
diff changeset
17 import re
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
16072
bae889f44cd1 more __all__ updates
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
19 __all__ = ["fix","sci","NotANumber"]
bae889f44cd1 more __all__ updates
Skip Montanaro <skip@pobox.com>
parents: 15867
diff changeset
20
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # Compiled regular expression to "decode" a number
6524
e9c67c192cdb re -> regex conversions by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
22 decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # \0 the whole thing
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # \1 leading sign or empty
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 # \2 digits left of decimal point
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 # \3 fraction (empty or begins with point)
6524
e9c67c192cdb re -> regex conversions by Sjoerd.
Guido van Rossum <guido@python.org>
parents: 6519
diff changeset
27 # \4 exponent part (empty or begins with 'e' or 'E')
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
11073
a84f9d3999ae Define NotANumber as a subclass of ValueError when using class-based
Fred Drake <fdrake@acm.org>
parents: 6524
diff changeset
29 try:
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
30 class NotANumber(ValueError):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
31 pass
11073
a84f9d3999ae Define NotANumber as a subclass of ValueError when using class-based
Fred Drake <fdrake@acm.org>
parents: 6524
diff changeset
32 except TypeError:
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
33 NotANumber = 'fpformat.NotANumber'
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def extract(s):
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
36 """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
37 sign is '+' or '-'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
38 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
39 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
40 expo is an integer"""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
41 res = decoder.match(s)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
42 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
43 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
44 if sign == '+': sign = ''
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
45 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
46 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
47 else: expo = 0
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
48 return sign, intpart, fraction, expo
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 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
51 """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
52 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
53 f = len(fraction)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
54 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
55 if expo > f:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
56 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
57 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
58 i = len(intpart)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
59 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
60 if expo < -i:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
61 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
62 return intpart, fraction
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 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
65 """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
66 f = len(fraction)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
67 if f <= digs:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
68 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
69 i = len(intpart)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
70 if i+digs < 0:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
71 return '0'*-digs, ''
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
72 total = intpart + fraction
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
73 nextdigit = total[i+digs]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
74 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
75 n = i + digs - 1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
76 while n >= 0:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
77 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
78 n = n-1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
79 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
80 total = '0' + total
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
81 i = i+1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
82 n = 0
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
83 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
84 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
85 if digs >= 0:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
86 return intpart, fraction[:digs]
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
87 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
88 return intpart[:digs] + '0'*-digs, ''
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 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
91 """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
92 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
93 If digs <= 0, the point is suppressed."""
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 16072
diff changeset
94 if type(x) != type(''): x = repr(x)
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
95 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
96 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
97 except NotANumber:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
98 return x
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
99 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
100 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
101 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
102 if intpart == '': intpart = '0'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
103 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
104 else: return sign + intpart
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 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
107 """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
108 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
109 If digs is <= 0, one digit is kept and the point is suppressed."""
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 16072
diff changeset
110 if type(x) != type(''): x = repr(x)
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
111 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
112 if not intpart:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
113 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
114 fraction = 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 if fraction:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
117 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
118 expo = expo - 1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
119 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
120 intpart = '0'
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
121 else:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
122 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
123 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
124 digs = max(0, digs)
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
125 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
126 if len(intpart) > 1:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
127 intpart, fraction, expo = \
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
128 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
129 expo + len(intpart) - 1
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
130 s = sign + intpart
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
131 if digs > 0: s = s + '.' + fraction
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 16072
diff changeset
132 e = repr(abs(expo))
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
133 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
134 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
135 else: e = '+' + e
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
136 return s + 'e' + e
704
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
a8022b2a77e6 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 def test():
11706
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
139 """Interactive test run."""
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
140 try:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
141 while 1:
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
142 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
143 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
144 except (EOFError, KeyboardInterrupt):
b6f52c1ef6da More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
145 pass