Contrived and a touch pretentious

Story: bc - The shell maestro’s calculatorTotal Replies: 4
Author Content
richo123

Apr 06, 2007
5:50 PM EDT
Instead of using a fancy shell tool like bc why not just write a regular c or fortran program? It doesn't take long to both write and compile and you have the program for future reference. Easy and no need to remember yet more convoluted shell syntax.
jezuch

Apr 07, 2007
1:39 AM EDT
What if you want to calculate something in a shell script?

echo 'int main() { /* whatever */ }' >> /tmp/calculatesomething.c gcc /tmp/calculatesomething.c result=$(/tmp/calculatesomething)

I'd say it's contrived ;)
DarrenR114

Apr 07, 2007
7:38 AM EDT
create a simple 'eval.pl' script: #!/usr/bin/perl # make sure that the above path to perl is correct use strict; my $expr = shift; my $result = eval($expr); print $expr . "=" . $result . "\n";

make eval.pl executable: $> chmod +x eval.pl

run the script: $>./eval.pl 1+1

result: 1+1=2

To get an approximation of pi: $> ./eval.pl 22/7

It's not as accurate the actual pi, but then you can do './eval.pl 22/7*\(3^2\)' to find a reasonable approximation of the area of a cirle with a radius of '3'.
richo123

Apr 07, 2007
8:22 AM EDT
Darren: How about

program pi double precision xpi xpi=4.*atan(1.0) print *,'pi=',xpi end

Accurate to 8 places. Edit: Dammit it is only accurate to 6 places
DarrenR114

Apr 07, 2007
9:44 AM EDT
@richo123

using my PERL script, as is, with your technique, instead of 22/7, to find pi, I would use: $> ./eval.pl \(atan2\(1,5\)*4-atan2\(1,239\)\)*4 (from a BASH command line.)

To find the area of a circle with radius 3 then: $> ./eval.pl \(atan2\(1,5\)*4-atan2\(1,239\)\)*4*3**2 giving this result: (atan2(1,5)*4-atan2(1,239))*4*3**2=28.2743338823081

Posting in this forum is limited to members of the group: [ForumMods, SITEADMINS, MEMBERS.]

Becoming a member of LXer is easy and free. Join Us!