Ignore:
Timestamp:
Oct 14, 2010 5:03:02 PM (10 years ago)
Author:
toby
Message:

put formatSU in common area

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/sandbox/gsascmds.tcl

    r1024 r1026  
    464464    afterputontop
    465465    return $makenew(result)
     466}
     467
     468# format numbers & errors in crystallographic notation
     469proc formatSU {num err} {
     470    # errors less or equal to t are expressed as 2 digits
     471    set T 19
     472    set lnT [expr { log10($T) }] 
     473    # error is zero
     474    if {$err == 0} {
     475        # is this an integer?
     476        if {int($num) == $num} {
     477            return [format %d [expr int($num)]]
     478        }
     479        # allow six sig figs with a zero error (except for 0.0)
     480        set dec [expr int(5.999999-log10( abs($num) ))]
     481        if {$dec < -2 || $dec > 9} {
     482            return [format %.5E $num]
     483        } elseif {$dec <= 0} {
     484            return [format %d [expr int($num)]]
     485        } else {
     486            return [format %.${dec}f $num]
     487        }
     488    } else {
     489        #set sigfigs [expr log10( abs(10) / abs(.012/$T) ) + 1]
     490        # should the number be expressed in scientific notation?
     491        if {$err > $T || abs($num) < 0.0001} {
     492            # get the exponent
     493            set exp [lindex [split [format %E $num] E] end]
     494            # strip leading zeros
     495            regsub {([-\+])0+} $exp {\1} exp
     496            # number of decimals in exponetial notation
     497            set dec [expr int($lnT - log10( abs($err) ) + $exp)]
     498            # should the error be displayed?
     499            if {$err < 0} {
     500                return [format "%.${dec}E" $num]
     501            } else {
     502                # scale the error into a decimal number
     503                set serr [expr int(0.5 + $err * pow(10,$dec-$exp))]
     504                return [format "%.${dec}E(%d)" $num $serr]
     505            }
     506        } else {
     507            # number of digits
     508            set dec [expr int($lnT - log10( abs($err) ))]
     509            # should the error be displayed?
     510            if {$err < 0} {
     511                return [format "%.${dec}f" $num]
     512            } else {
     513                set serr [expr int(0.5 + $err * pow(10,$dec))]
     514                return [format "%.${dec}f(%d)" $num $serr]
     515            }
     516        }
     517    }
    466518}
    467519
Note: See TracChangeset for help on using the changeset viewer.