From: tbrannon@pluto.CSEE.Lehigh.Edu (tbrannon)
To: libes@NIST.GOV
Date: Wed, 24 Nov 1993 05:38:29 -0500 (EST)

[This code is useful for evaluating arbitrary precision expressions.
To initialize, just call "set bc [bc_init]".  To use from then on, call:

	bc_eval $bc $expr

where $bc is the result from bc_init, and $expr is the expression to
evaluate.  See code for more info. - DEL]

----------------------------------------------------------------------
# Terrence Brannon
# tb06@pl122a.eecs.lehigh.edu

set timeout -1

# bc_init:
# Call with a string to be sent to bc upon creation of the bc process.
# If no string is supplied, a default initialization string will be
# used.
# Returns a spawn_id which must be used when sending expressions to
# the bc process
#
proc bc_init { {init_string "scale=6\r"} } {
    spawn bc
    set I $spawn_id
    send -i $I $init_string
    expect -re "(.*)\r\n" 
    return $I
}

# bc_eval:
# Call with the value of the spawn_id and a mathematical expression
# Returns the result of expression evaluation
#
proc bc_eval {spawn_id math_expr} {
    send -i $spawn_id "$math_expr\r"
    expect -re "(.*)\r\n(.*)\r\n" {return $expect_out(2,string)}
}

proc bc_test {} {
    set math_string {2.3 * 4.6666 * 1.1 + 500}
    set j [bc_init]
    set result [bc_eval $j $math_string]
    puts "The evaluation of $math_string is: $result"
}

