m4 preprocessor BC macro

2019-08-10 11:00发布

问题:

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

回答1:

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

then:

BC(ONEPOINTTWO*TWO)
=> 2.4


标签: m4