Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- proc math::combined_market_rate { args} {
- set sum 0.
- set N [llength $args]
- #puts "Debug: llength is $N of $args"
- if { $N == 0 } { return 0 }
- #puts "Line: "#__LINE__
- if {[lindex $args 0] == 0} {return 0}
- # if { ($N == 1) || ([lindex $args 0 ] == 0) } { return 0 }
- #puts "Line: "#__LINE__
- foreach val $args {
- # puts "Debug: adding $val"
- #set sum [ expr { $sum + 1.0/$val } ]
- += sum [/ 1.0 $val]
- }
- set combined_market_rate1 [ expr { 1.0/($sum/$N) } ]
- return $combined_market_rate1
- }
- 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 ]
- assert [::math::combined_market_rate 0.5524 0.4807 0.42918 0.47846 ] {== [round $_ 5] 0.48131} -v -line #__LINE__
- # answer 0.48131 "
- #puts [ ::math::combined_market_rate .1 .2 .3 .4 ]
- assert [::math::combined_market_rate .1 .2 .3 .4 ] {== [round $_ 3] 0.192} -v -line #__LINE__
- # answer
- #::math::combined_market_rate -.1 -.2 -.3 -.4 #answer -0.192
- assert [::math::combined_market_rate -.1 -.2 -.3 -.4 ] {== [round $_ 3] -0.192} -v -line #__LINE__
- # 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
- assert [::math::combined_market_rate -.1 -.2 .3 .4 ] {== [round $_ 6] -0.436364} -v -line #__LINE__
- #set check [/ 1. [/ [+ [/ 1. -.1] [/ 1. -.2] [/ 1. 0.3 ] [/ 1. 0.4] ] 4. ] ]
- # check equals -0.4363636363636364
- # ::math::combined_market_rate # null returns zero, correct.
- assert [::math::combined_market_rate] {== $_ 0} -v -line #__LINE__
- # ::math::combined_market_rate 0 # arg 0 returns zero, correct.
- assert [::math::combined_market_rate 0] {== $_ 0} -v -line #__LINE__
- #puts " for (::math::combined_market_rates .1) returns .1 "
- assert [::math::combined_market_rate .1 ] {== $_ 0.1} -v -line #__LINE__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement