Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
fpformat Namespace Reference

Data Structures

class  NotANumber
 

Functions

def extract
 
def unexpo
 
def roundfrac
 
def fix
 
def sci
 
def test
 

Variables

list __all__ = ["fix","sci","NotANumber"]
 
tuple decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
 
string NotANumber = 'fpformat.NotANumber'
 

Detailed Description

General floating point formatting functions.

Functions:
fix(x, digits_behind)
sci(x, digits_behind)

Each takes a number or a string and a number of digits as arguments.

Parameters:
x:             number to be formatted; or a string resembling a number
digits_behind: number of digits behind the decimal point

Function Documentation

def fpformat.extract (   s)
Return (sign, intpart, fraction, expo) or raise an exception:
sign is '+' or '-'
intpart is 0 or more digits beginning with a nonzero
fraction is 0 or more digits
expo is an integer

Definition at line 32 of file fpformat.py.

32 
33 def extract(s):
34  """Return (sign, intpart, fraction, expo) or raise an exception:
35  sign is '+' or '-'
36  intpart is 0 or more digits beginning with a nonzero
37  fraction is 0 or more digits
38  expo is an integer"""
39  res = decoder.match(s)
40  if res is None: raise NotANumber, s
41  sign, intpart, fraction, exppart = res.group(1,2,3,4)
42  if sign == '+': sign = ''
43  if fraction: fraction = fraction[1:]
44  if exppart: expo = int(exppart[1:])
45  else: expo = 0
46  return sign, intpart, fraction, expo
def fpformat.fix (   x,
  digs 
)
Format x as [-]ddd.ddd with 'digs' digits after the point
and at least one digit before.
If digs <= 0, the point is suppressed.

Definition at line 87 of file fpformat.py.

References extract(), roundfrac(), and unexpo().

87 
88 def fix(x, digs):
89  """Format x as [-]ddd.ddd with 'digs' digits after the point
90  and at least one digit before.
91  If digs <= 0, the point is suppressed."""
92  if type(x) != type(''): x = `x`
93  try:
94  sign, intpart, fraction, expo = extract(x)
95  except NotANumber:
96  return x
97  intpart, fraction = unexpo(intpart, fraction, expo)
98  intpart, fraction = roundfrac(intpart, fraction, digs)
99  while intpart and intpart[0] == '0': intpart = intpart[1:]
100  if intpart == '': intpart = '0'
101  if digs > 0: return sign + intpart + '.' + fraction
102  else: return sign + intpart
def fpformat.roundfrac (   intpart,
  fraction,
  digs 
)
Round or extend the fraction to size digs.

Definition at line 61 of file fpformat.py.

61 
62 def roundfrac(intpart, fraction, digs):
63  """Round or extend the fraction to size digs."""
64  f = len(fraction)
65  if f <= digs:
66  return intpart, fraction + '0'*(digs-f)
67  i = len(intpart)
68  if i+digs < 0:
69  return '0'*-digs, ''
70  total = intpart + fraction
71  nextdigit = total[i+digs]
72  if nextdigit >= '5': # Hard case: increment last digit, may have carry!
73  n = i + digs - 1
74  while n >= 0:
75  if total[n] != '9': break
76  n = n-1
77  else:
78  total = '0' + total
79  i = i+1
80  n = 0
81  total = total[:n] + chr(ord(total[n]) + 1) + '0'*(len(total)-n-1)
82  intpart, fraction = total[:i], total[i:]
83  if digs >= 0:
84  return intpart, fraction[:digs]
85  else:
86  return intpart[:digs] + '0'*-digs, ''
def fpformat.sci (   x,
  digs 
)
Format x as [-]d.dddE[+-]ddd with 'digs' digits after the point
and exactly one digit before.
If digs is <= 0, one digit is kept and the point is suppressed.

Definition at line 103 of file fpformat.py.

References extract(), sre_parse.max, and roundfrac().

104 def sci(x, digs):
105  """Format x as [-]d.dddE[+-]ddd with 'digs' digits after the point
106  and exactly one digit before.
107  If digs is <= 0, one digit is kept and the point is suppressed."""
108  if type(x) != type(''): x = `x`
109  sign, intpart, fraction, expo = extract(x)
110  if not intpart:
111  while fraction and fraction[0] == '0':
112  fraction = fraction[1:]
113  expo = expo - 1
114  if fraction:
115  intpart, fraction = fraction[0], fraction[1:]
116  expo = expo - 1
117  else:
118  intpart = '0'
119  else:
120  expo = expo + len(intpart) - 1
121  intpart, fraction = intpart[0], intpart[1:] + fraction
122  digs = max(0, digs)
123  intpart, fraction = roundfrac(intpart, fraction, digs)
124  if len(intpart) > 1:
125  intpart, fraction, expo = \
126  intpart[0], intpart[1:] + fraction[:-1], \
127  expo + len(intpart) - 1
128  s = sign + intpart
129  if digs > 0: s = s + '.' + fraction
130  e = `abs(expo)`
131  e = '0'*(3-len(e)) + e
132  if expo < 0: e = '-' + e
133  else: e = '+' + e
134  return s + 'e' + e
def fpformat.test ( )
Interactive test run.

Definition at line 135 of file fpformat.py.

References fix(), fileinput.input(), and sci().

136 def test():
137  """Interactive test run."""
138  try:
139  while 1:
140  x, digs = input('Enter (x, digs): ')
141  print x, fix(x, digs), sci(x, digs)
142  except (EOFError, KeyboardInterrupt):
143  pass
def fpformat.unexpo (   intpart,
  fraction,
  expo 
)
Remove the exponent by changing intpart and fraction.

Definition at line 47 of file fpformat.py.

47 
48 def unexpo(intpart, fraction, expo):
49  """Remove the exponent by changing intpart and fraction."""
50  if expo > 0: # Move the point left
51  f = len(fraction)
52  intpart, fraction = intpart + fraction[:expo], fraction[expo:]
53  if expo > f:
54  intpart = intpart + '0'*(expo-f)
55  elif expo < 0: # Move the point right
56  i = len(intpart)
57  intpart, fraction = intpart[:expo], intpart[expo:] + fraction
58  if expo < -i:
59  fraction = '0'*(-expo-i) + fraction
60  return intpart, fraction

Variable Documentation

list __all__ = ["fix","sci","NotANumber"]

Definition at line 16 of file fpformat.py.

tuple decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')

Definition at line 19 of file fpformat.py.

string NotANumber = 'fpformat.NotANumber'

Definition at line 30 of file fpformat.py.