Search code examples
m4

m4 preprocessor BC macro


Relative to this question, I'd like to define a BC m4 macro of that function:

syscmd(bc <<< "scale=6; print ARITHMETIC_OPERATION_STRING")

NB: It just process an arithmetic operation with bc, for ex: syscmd(bc <<< "scale=6; print 1.2*2") => 2.4

I guess something like:

define(`BC', syscmd(bc <<< "scale=6; print $1"))

but how to expand other variables in BC argument, eg:

define(`ONEPOINTTWO', `1.2')
define(`TWO', `2')

BC(ONEPOINTTWO*TWO)
=> 2.4

Thank you


Solution

  • define(`BC', `syscmd(echo "scale=6; print $1" | bc)')
    

    then:

    BC(ONEPOINTTWO*TWO)
    => 2.4