Overview
| Artifact ID: | 1e56dcba026acd12ce006efc55cce57daca622a3c5632d6bfbbf598aef01c566 |
|---|---|
| Ticket: | 67b7bafb20656b607f2d861e334c82567518a0bb |
| Date: | 2018-09-22 17:40:55 |
| User: | anonymous |
| Artifact Attached: | b4b1af7d3a0b1ae87f9097b88681692642e0607254a26cd49ad5e1f97205b290 |
| Filename: | combined_market_rate.txt |
| Description: | 67b7bafb, from gold on TCL/WIKI ::math::combined_market_rate |
Content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45# 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.
