[ale] real numbers in bash scripts?

Mark R. Lindsey 30ae+fdd4.e6e4__ at mark.datasys.net
Thu Dec 28 23:57:42 EST 2000


: I was trying to find a way to handle real numbers (percentages)
: in BASH.  It appears bash handles integers natively, but I'm not
: sure how to do real numbers.  Do you have to create a function
: that calls some external tool, like bc or something?

You're right -- bash provides a few operations integers, represented as 
decimal ("50"), octal ("062"), and hexidecimal ("0x32"), and those
operations throw away the fractional part ("echo $[0x100 / 0xABC]"). 
To use perlspeak, when you evaluate the strings that represent 
those numbers, the parser chokes on decimal points.

And, yes, bc is one way to do it. I occasionally get awk mixed up
in the whole thing. 
	wheel_circumference=`echo "scale=500 ; ${wheel_radius} * 8 * a(1)" | bc -l`

Note that when using a big scale like that, you'll have to accomodate 
bc's oh-so-un-unixy formatting:
	mark at vielle ~$ echo "scale=500 ; 37.5 * 8 * a(1)" | bc -l
	235.6194490192344928846982537459627163147877049531329365731208444230\
	86230471465674897102611900658780098661106488496172998532038345716293\
	66737940195560963608380877130770264538908291697334672117161977864733\
	21608231749450084596356736175340087373953401431859236425192595261457\
	84074498616004520544518685595529344025495473669113116114069072212194\
	05687865232759194427700084978979116153498910381102139561337070747929\
	52774319693964819139588036449458844828838911055846784972062175513914\
	3145436684209536372589618400 
this is left as an exercise to the reader.

We'd probably all be frightened about how much of this goes on in the
bowels of the data center of your friendly multinational bank.


--
To unsubscribe: mail majordomo at ale.org with "unsubscribe ale" in message body.





More information about the Ale mailing list