Operating System - HP-UX
1832275 Members
2086 Online
110041 Solutions
New Discussion

script that can handle exponentials

 
SOLVED
Go to solution
Marc Ahrendt
Super Advisor

script that can handle exponentials

any tricks in perl or ksh to take an input value, for example, 7.00E-5, and multiply it by a similar value?

basically i am writing a very simple calculator for some engineers here but have no idea on how to add, subtract, multiply, etc.. values in the format 8.03e-3

in the past i have used "bc" but not sure if there was an elegant way of working with exponents like this
hola
5 REPLIES 5
Chris Vail
Honored Contributor

Re: script that can handle exponentials

bc is the catch-all executable for all of these kinds of things. I find it quite elegant, actually. The korn shell itself has very limited math capability. Its limited to the "let" statement--denoted with double parens: (( and )).

I don't do perl, but suspect that it too uses bc.


Chris
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: script that can handle exponentials

Perl natively understands exponential notation and can easily output in the same or in the floating point output format of your choice. Normally Perl does all math in floating-point and you have to tell it explicitly (via "use integer;") to do integer math.

#!/usr/bin/perl

my $a = 7.00e-5;
my $b = 2000.0
my $c = $a * $b;
printf("Fixed point: %10.2f Exponential: %e\n",$c,$c);
If it ain't broke, I can fix that.
Biswajit Tripathy
Honored Contributor

Re: script that can handle exponentials

If you want to multiply, say 7.00e-5 with 5.00e2,
then you do something like this:

----
#!/usr/bin/ksh
a="7.00*e(-5)"
b="5.00*e(2)"
echo "a=($a)*($b)" | bc -l
----

Basically, you need to take the user input and
format it to a string like 'a' and 'b' as shown
above. See bc(1) manpages for all the cool maths
things you can do.

- Biswajit
:-)
Biswajit Tripathy
Honored Contributor

Re: script that can handle exponentials

Sorry for the typo in my last post. Change the
script to:

---
#!/usr/bin/ksh
a="7.00*e(-5)"
b="5.00*e(2)"
echo "($a)*($b)" | bc -l
---

- Biswajit
:-)
Marc Ahrendt
Super Advisor

Re: script that can handle exponentials

thx ...the quick feedback was very helpful
hola