console show proc quantity_into_ratios {quantity args} { if { $quantity <= 0 } { return -code error "The quantity must be larger than 0" } if { [ llength $args ] < 2 } { return -code error "The number of args must be 2 or more" } set sum 0 foreach val $args { set sum [ expr { $sum + $val } ] puts " $quantity $args $sum $val " } puts " quantity_into_ratios: sum of args equals $sum " if { $sum <= 0 } { return -code error "The sum of args must be more than zero" } if {[ llength $args ] == 0} { return 0 } set answer "" set sum2 0 foreach val $args { lappend answer [ expr { $quantity * ((1.*$val)/(1.*$sum)) } ] set sum2 [ expr { $sum2 + ($quantity * ((1.*$val)/(1.*$sum))) } ] } puts " sum of shares $sum2 " set answer } puts " quantity_into_ratios ( 84 1 2 4 ) answer 12.0 24.0 48.0 " puts " [ quantity_into_ratios 84 1 2 4 ] " puts " quantity_into_ratios ( 14 1 2 4 ) answer 2.0 4.0 8.0 " puts " [ quantity_into_ratios 14 1 2 4 ] " puts " quantity_into_ratios ( 10 2 3 ) answer 4.0 6.0 " puts " [ quantity_into_ratios 10 2 3 ] " puts " quantity_into_ratios ( 11 2 3 ) answer 4.40 6.59 " puts " [ quantity_into_ratios 11 2 3 ] " puts " quantity_into_ratios ( 9 1 1 1 ) answer 3.0 3.0 3.0 " puts " [ quantity_into_ratios 9 1 1 1 ] " puts " quantity_into_ratios ( 10 1 1 1 1 1 1 1 1 1 1 ) answer 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 " puts " [ quantity_into_ratios 10 1 1 1 1 1 1 1 1 1 1 ] " puts " quantity_into_ratios ( 0.10 1 1 1 1 1 1 1 1 1 1 ) answer 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01" puts " [ quantity_into_ratios 0.10 1 1 1 1 1 1 1 1 1 1 ] " puts " quantity_into_ratios ( 0.10 .1 .1 .1 .1 .1 .1 .1 .1 .1 .1 ) answer 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01" puts " [ quantity_into_ratios 0.10 .1 .1 .1 .1 .1 .1 .1 .1 .1 .1 ] " #puts " quantity_into_ratios ( 10 1 ) answer returns error " #puts " [ quantity_into_ratios 10 1 ] " #puts " [ quantity_into_ratios 84 1 ] for (quantity_into_ratios 84 0) returns error " #puts " [ quantity_into_ratios 84 ] for (quantity_into_ratios 84 0) returns error " #puts " [ quantity_into_ratios -2 1 2 3 ] for (quantity_into_ratios -2 1 2 3) returns error "