Tcl Library Source Code

Artifact [b4b1af7d3a]
Login

Artifact b4b1af7d3a0b1ae87f9097b88681692642e0607254a26cd49ad5e1f97205b290:

Attachment "combined_market_rate.txt" to ticket [67b7bafb20] added by anonymous 2018-09-22 17:40:55. (unpublished)
        # adapted from tcl Stats 2011-05-22, arithmetic mean  [RLE]
        # gold on TCL/WIKI
        # ::math::combined_market_rate --
        #
        # Return the combined_market_rate by  one,two, or more given rates
        # market rate defined as quantity per price
        # or 1 over (price per quantity)
        # 
        # Arguments:
        #    
        #    args  values are one, two, or more given rates
        #
        # Results: combined_market_rate
        #  works for positive numbers, negative numbers,
        #  and mixed positive & negative numbers.
        #  arg of zero returns zero  
        #  arg of null returns zero  
        proc ::math::combined_market_rate { args} {
            set sum 0.
            set N [ expr { [ llength $args ] } ]
            if { $N == 0 } { return 0 }
            if { $N == 1 || [ lindex $args 0 ] == 0 } { return 0 }
            foreach val $args {
              set sum [ expr { $sum + 1./$val } ] 
            }
            set combined_market_rate1 [ expr { 1./($sum/$N) } ]
            return $combined_market_rate1
        }
        console
        puts  " [ ::math::combined_market_rate  0.5524 0.4807 0.42918 0.47846 ] answer 0.40131"
        # various testcases
        #puts  [::math::combined_market_rate  0.5524 0.4807 0.42918 0.47846 ] 
        #  answer 0.48131    "
        #puts   [ ::math::combined_market_rate .1 .2 .3 .4 ]     
        #  answer 0.192 
        #::math::combined_market_rate -.1 -.2 -.3 -.4  #answer -0.192
        # check [/ 1. [/ [+ [/ 1. -.1] [/ 1. -.2] [/ 1. -0.3 ] [/ 1. -0.4] ] 4. ] ]
        #returns  -0.192 
        #puts " [ ::math::combined_market_rate .1 ] "
        #:math::combined_market_rate -.1 -.2  .3 .4 #answer -0.4363636363636364
        #set check [/ 1. [/ [+ [/ 1. -.1] [/ 1. -.2] [/ 1.  0.3 ] [/ 1. 0.4] ] 4. ] ] 
        # check equals    -0.4363636363636364 
        #puts " for (::math::combined_market_rates .1) returns .1 "  
        # ::math::combined_market_rate      # null returns zero, correct.
        # ::math::combined_market_rate 0    # arg 0 returns zero, correct.