Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2018
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 1.95 KB | None | 0 0
  1. proc math::combined_market_rate { args} {
  2.     set sum 0.
  3.     set N [llength $args]
  4.     #puts "Debug: llength is $N of $args"
  5.     if { $N == 0 } { return 0 }
  6.     #puts "Line: "#__LINE__
  7.     if {[lindex $args 0] == 0} {return 0}
  8.     # if { ($N == 1) || ([lindex $args 0 ] == 0) } { return 0 }
  9.     #puts "Line: "#__LINE__
  10.     foreach val $args {
  11.         # puts "Debug: adding $val"
  12.         #set sum [ expr { $sum + 1.0/$val } ]
  13.         += sum [/ 1.0 $val]
  14.     }
  15.     set combined_market_rate1 [ expr { 1.0/($sum/$N) } ]
  16.     return $combined_market_rate1
  17. }
  18.  
  19. puts " [ ::math::combined_market_rate 0.5524 0.4807 0.42918 0.47846 ] (Answer 0.40131)"
  20. # various testcases
  21. #puts [::math::combined_market_rate 0.5524 0.4807 0.42918 0.47846 ]
  22. assert [::math::combined_market_rate 0.5524 0.4807 0.42918 0.47846 ] {== [round $_ 5] 0.48131} -v -line #__LINE__
  23. # answer 0.48131 "
  24. #puts [ ::math::combined_market_rate .1 .2 .3 .4 ]
  25. assert [::math::combined_market_rate .1 .2 .3 .4 ] {== [round $_ 3] 0.192} -v -line #__LINE__
  26. # answer  
  27. #::math::combined_market_rate -.1 -.2 -.3 -.4 #answer -0.192
  28. assert [::math::combined_market_rate -.1 -.2 -.3 -.4 ] {== [round $_ 3] -0.192} -v -line #__LINE__
  29. # check [/ 1. [/ [+ [/ 1. -.1] [/ 1. -.2] [/ 1. -0.3 ] [/ 1. -0.4] ] 4. ] ]
  30. #returns -0.192
  31. #puts " [ ::math::combined_market_rate .1 ] "
  32. #:math::combined_market_rate -.1 -.2 .3 .4 #answer -0.4363636363636364
  33. assert [::math::combined_market_rate -.1 -.2 .3 .4 ] {== [round $_ 6] -0.436364} -v -line #__LINE__
  34.  
  35. #set check [/ 1. [/ [+ [/ 1. -.1] [/ 1. -.2] [/ 1. 0.3 ] [/ 1. 0.4] ] 4. ] ]
  36. # check equals -0.4363636363636364
  37.  
  38. # ::math::combined_market_rate # null returns zero, correct.
  39. assert [::math::combined_market_rate] {== $_ 0} -v -line #__LINE__
  40. # ::math::combined_market_rate 0 # arg 0 returns zero, correct.
  41. assert [::math::combined_market_rate 0] {== $_ 0} -v -line #__LINE__
  42. #puts " for (::math::combined_market_rates .1) returns .1 "
  43. assert [::math::combined_market_rate .1 ] {== $_ 0.1} -v -line #__LINE__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement