Changeset 1183 for trunk/GSASIIpy3.py


Ignore:
Timestamp:
Jan 5, 2014 9:22:22 AM (8 years ago)
Author:
toby
Message:

fixup number formatting; update produced doc files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIpy3.py

    r1181 r1183  
    3939    return val
    4040
    41 def FormatValue(val,maxdigits=[10,2]):
     41def FormatValue(val,maxdigits=None):
    4242    '''Format a float to fit in ``maxdigits[0]`` spaces with maxdigits[1] after decimal.
    4343
     
    4949    :returns: a string with <= maxdigits characters (I hope). 
    5050    '''
     51    if maxdigits is None:
     52        digits = [10,2]
     53    else:
     54        digits = maxdigits
    5155    # does the standard str() conversion fit?
    5256    string = str(val)
    53     if len(string) <= maxdigits[0]: return string.strip()
     57    if len(string) <= digits[0]: return string.strip()
    5458    # negative numbers, leave room for a sign
    55     if val < 0: maxdigits[0] -= 1
    56     decimals = maxdigits[0] - maxdigits[1]
     59    if val < 0: digits[0] -= 1
     60    decimals = digits[0] - digits[1]
    5761    if abs(val) < 1e-99 or abs(val) > 1e99:
    58         decimals = min(maxdigits[0]-6,maxdigits[1])
    59         fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}" # create format string
     62        decimals = min(digits[0]-6,digits[1])
     63        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}" # create format string
    6064    elif abs(val) < 1e-9 or abs(val) > 1e9:
    61         decimals = min(maxdigits[0]-5,maxdigits[1])
    62         fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
     65        decimals = min(digits[0]-5,digits[1])
     66        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
    6367    elif abs(val) < 10**(4-decimals): # make sure at least 4 decimals show
    64         decimals = min(maxdigits[0]-5,maxdigits[1])
    65         fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
     68        decimals = min(digits[0]-5,digits[1])
     69        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
    6670    elif abs(val) >= 10**decimals: # deal with large numbers in smaller spaces
    67         decimals = min(maxdigits[0]-5,maxdigits[1])
    68         fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
     71        decimals = min(digits[0]-5,digits[1])
     72        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
    6973    elif abs(val) < 1: # use f format for small numbers
    70         decimals = min(maxdigits[0]-3,maxdigits[1])
    71         fmt = "{" + (":{:d}.{:d}f".format(maxdigits[0],decimals))+"}"
     74        decimals = min(digits[0]-3,digits[1])
     75        fmt = "{" + (":{:d}.{:d}f".format(digits[0],decimals))+"}"
    7276    else: # in range where g formatting should do what I want
    73         decimals = maxdigits[0] - 1
    74         fmt = "{" + (":{:d}.{:d}g".format(maxdigits[0],decimals))+"}"
     77        decimals = digits[0] - 1
     78        fmt = "{" + (":{:d}.{:d}g".format(digits[0],decimals))+"}"
    7579    try:
    7680        return fmt.format(val).strip()
     
    7882        print 'FormatValue Error with val,maxdigits,fmt=',val,maxdigits,fmt
    7983        return str(val)
     84
     85def FormatSigFigs(val, maxdigits=10, sigfigs=5, treatAsZero=1e-20):
     86    '''Format a float to use ``maxdigits`` or fewer digits with ``sigfigs``
     87    significant digits showing (if room allows).
     88
     89    :param float val: number to be formatted.
     90
     91    :param int maxdigits: the number of digits to be used for display of the
     92       number (defaults to 10).
     93
     94    :param int sigfigs: the number of significant figures to use, if room allows
     95
     96    :param float treatAsZero: numbers that are less than this in magnitude
     97      are treated as zero. Defaults to 1.0e-20, but this can be disabled
     98      if set to None.
     99
     100    :returns: a string with <= maxdigits characters (I hope). 
     101    '''
     102    if treatAsZero is not None:
     103        if abs(val) < treatAsZero:
     104            return '0.0'
     105    # negative numbers, leave room for a sign
     106    if val < 0: maxdigits -= 1
     107    if abs(val) < 1e-99 or abs(val) > 9.999e99:
     108        decimals = min(maxdigits-6,sigfigs)
     109        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}" # create format string
     110    elif abs(val) < 1e-9 or abs(val) > 9.999e9:
     111        decimals = min(maxdigits-5,sigfigs)
     112        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     113    elif abs(val) < 9.9999999*10**(sigfigs-maxdigits):
     114        decimals = min(maxdigits-5,sigfigs)
     115        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     116    elif abs(val) >= 10**sigfigs: # deal with large numbers in smaller spaces
     117        decimals = min(maxdigits-5,sigfigs)
     118        fmt = "{" + (":{:d}.{:d}g".format(maxdigits,decimals))+"}"
     119    elif abs(val) < 1: # small numbers, add to decimal places
     120        decimals = sigfigs - int(np.log10(abs(val)))
     121        fmt = "{" + (":{:d}.{:d}f".format(maxdigits,decimals))+"}"
     122    else: # larger numbers, remove decimal places
     123        decimals = sigfigs - 1 - int(np.log10(abs(val)))
     124        fmt = "{" + (":{:d}.{:d}f".format(maxdigits,decimals))+"}"
     125        if decimals == 0: fmt += "." # force a decimal place
     126    try:
     127        return fmt.format(val).strip()
     128    except ValueError as err:
     129        print 'FormatValue Error with val,maxdigits, sigfigs, fmt=',val, maxdigits,sigfigs, fmt
     130        return str(val)
     131
     132if __name__ == '__main__':
     133    for i in (1.23456789e-129,1.23456789e129,1.23456789e-99,1.23456789e99,-1.23456789e-99,-1.23456789e99):
     134        print FormatSigFigs(i),i
     135    for i in (1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000):
     136        print FormatSigFigs(1.23456789e-9*i),1.23456789e-9*i
     137    for i in (1,10,100,1000,10000,100000,1000000,10000000,100000000):
     138        print FormatSigFigs(1.23456789e9/i),1.23456789e9/i
Note: See TracChangeset for help on using the changeset viewer.