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