Tcl Library Source Code

Attachment Details
Login
Overview

Artifact ID: 3b2a3dc6388364d90b8029c057d6d1943991cd99987e48dadbc559e83de11156
Ticket: 67b7bafb20656b607f2d861e334c82567518a0bb
Date: 2018-09-24 18:07:24
User: anonymous
Artifact Attached: 9e38e1b054e1fdfff64aac6e8ccd8d255e03227fb2b1d1c8cad1299be56a9d09
Filename:proc_t_combined_market_rate.txt
Description:added foreach filter against irregular zeros into combined_market_proc.gold on tcl wiki
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
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
        # adapted from tcl Stats 2011-05-22, arithmetic mean  [RLE]
        # ::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  
        #  filter foreach drops irregular zero elements from argument
        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 }
            set res {};set counter2 0;
            # filter foreach drops irregular zero elements  
            foreach item $args {if {$item != 0 } {incr counter2 1; lappend res $item } }
            set counter 0
            foreach val $res {
              set sum [ expr { $sum + 1./$val } ] 
              incr counter 1
            }
            set combined_market_rate1 [ expr { 1./(($sum*1.)/$counter2) } ]
            return $combined_market_rate1
        }
        # various testcases on combined_market_rate
        # 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, correct
        # operator math formula follows
        # check [/ 1. [/ [+ [/ 1. -.1] [/ 1. -.2] [/ 1. -0.3 ] [/ 1. -0.4] ] 4. ] ]
        # returns  -0.192, correct 
        # puts " [ ::math::combined_market_rate .1 ] "
        # :math::combined_market_rate -.1 -.2  .3 .4
        # answer -0.4363636363636364
        # operator math formula follows
        # 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.
        # addition dated 24sep2018
        # added filter foreach to remove zero's
        # irregular zeros,
        # test on zero's 0.1 0.0 0.0 0.2 0.3 0.4
        # returns 0.192 , correct
        # test on zero's   0.1 0.0 0.0 0.2 0.3 0.4
        # returns 0.192 , correct