Simple Arithmetic Program (help pls)
I have a simple mathematical process i would like to automate with a program of some sort. Maybe even a basic shell script. I would like to have the user specify (or be prompted for) a few variables "A" "B" "C" and "M". Where A,B, and C are values in units of time aka HMS (hh.mmss). And M is in units of MiB.
A+B+C=TT (in HMS, whats the syntax for adding HMS units in the shell?) A HMS-> / TT HMS-> = AX (unitless factor) Where "HMS->" (aka HMS OUT) means to convert HMS to a decimal number (unitless). And repeat for B and C, resulting in AX,BX, and CX. Then simply: AX*M=AS (in MiB) BX*M=BS (in MiB) CX*M=CS (in MiB) The desired output would be the numerical values of AS,BS, and CS. I know theres probably many ways to do this, any suggestions would be helpful. To keep it simple i would prefer a simple bash script if possible. |
For a start, reduce/cvt all time units to the lowest ie seconds.
You should read/bookmark these http://rute.2038bug.com/index.html.gz http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/ Note that shell only works in integers, so you'll get rounding or truncation when doing division. If you want floating pt, use the bc tool http://linux.die.net/man/1/bc |
Quote:
Actually, nor do most programming languages have an h:m:s datatype inherently, but in some you could happily define an object to have one, and that may make things easier/cleaner. Also the problem that chrism01 mentions: Quote:
|
I've seen some posts lately about floating-point math with AWK (I'm not an "AWK person" myself, no first-hand knowledge....)
|
FWIW,
Code:
echo $(($(echo "((1:20:5" |sed 's,:,)*60+,g'))) A+B+C=TT To do decimals for A HMS-> / TT HMS-> = AXyou must append a 0 (* 10^N) for each decimal place you want. If you want to display the resulting factors, you will need to clean up your output by inserting a decimal point in the correct spot. You should leave the factors as is for the next step AX*M=AS (in MiB)& divide your results by 10^N. To me this seems like an easy bash script. I think qualifying the inputs may be more difficult than the arithmetic. Edit: Changed "1^N" to "10^N" |
Quote:
I guess my first step before trying to write a scrip is figuring how to even do it manually in the shell first. So now i can convert HMS to seconds and get TT in seconds. Now i just need to divide each A,B, and C by TT and get a factor to 12 decimal places (AX,BX,CX). Then just multiply AX,BX, and CX by "M", to the nearest whole number is fine. PS: Is there a source for that "echo $(($(echo "((1:20:5" |sed 's,:,)*60+,g')))" command i could read over, just curious what each piece means exactly (how it works). |
Quote:
Quote:
Quote:
Quote:
Quote:
Code:
echo $(($(echo "((1:20:5" | sed 's,:,)*60+,g'))) Code:
echo $(($(echo "(($A" | sed 's,:,)*60+,g'))) (hh*60+mm)*60+ssor, adding a set of mathematically unnecessary, but programatically useful parentheses ((hh)*60+mm)*60+sswill convert hh:mm:ss into seconds, so the innermost (purple hues) echo adds the 2 necessary leading parentheses to the hh:mm:ss & pipes it into a sed which turns the colons into the right math. Run Code:
echo "((1:20:5" | sed 's,:,)*60+,g' The Command Substitution $( ... ) ormakes the algebra available to the Arithmetic Expansion $(( ... )) orThe green (1st) echo displays the result of the Arithmetic Expansion, you may not need it in your script. It's time for you to start working on the commands. Post them & we'll critique them. |
Thanks for the detailed response. I think going with a bash scrip might not be as "simple" as i expected. Perhaps going with a programmable (preferably RPN) calculator program (either CLI or GUI) would be an easier way. I planned to automate some other tasks that i may have more trouble with using a bash script. My background in programming calculators began with HP50g, which uses a HP-Basic language. Where a program is basically stored as a global variable and executed as such. A simple one would look like this:
<< A B C + + D 'STO' D "The Answer Is" ->Tag >> In RPN mode, it would sum A,B and C, store it as a global variable "D" and display the answer on the stack tagged with "The Answer Is". The above program could be saved as global variable "ABCD", and executed by running ABCD. This is the simplicity i was after :). I've since opened another thread regarding suggestions for RPN programmable calculators here. |
<OT>
I started w/ a 35 w/ no model # on the case, went on to the 65, the 97, & a 41 -- which languishes needing a battery because there is no room for both it & my phone in my shirt pocket. So why doesn't HP, alone or w/ Nokia, make the "perfect" engineer/geek phone? Start w/ the shape (from the Wikipedia® article referenced above): Quote:
Quote:
On a pure keystoke RPN calculator the conversion sequence is: HÎ60*M+60*S+ ('Î' is the "Enter" key). |
Quote:
|
Quote:
http://perldoc.perl.org/ http://www.perlmonks.org/?node=Tutorials |
All times are GMT -5. The time now is 12:31 AM. |