I have 2 numbers
xmin = 0.369000018
xmax = 0.569000006
and want to calculate
(xmax- xmin) / 5.0
I have tried using $(( )) but is always giving an error
I have 2 numbers
xmin = 0.369000018
xmax = 0.569000006
and want to calculate
(xmax- xmin) / 5.0
I have tried using $(( )) but is always giving an error
Here is an approach using bc
#!/bin/bash
xmin=0.369000018
xmax=0.569000006
echo "scale=9; ( $xmax - $xmin ) / 5.0" | bc
The bash shell doesn't perform non-integer arithmetic.
You can do it with a recent (newer than 1988) Korn shell:
xmin=0.369000018
xmax=0.569000006
printf "%.10f\n" $(( (xmax - xmin) / 5.0))
which produces:
0.0399999976
Bugger. I'm stuck with bash.
---------- Post updated at 07:53 PM ---------- Previous update was at 07:49 PM ----------
I it possible to write a function to do a calculation using bc?
---------- Post updated at 08:02 PM ---------- Previous update was at 07:53 PM ----------
Seems a solution would be in awk
---------- Post updated at 08:04 PM ---------- Previous update was at 08:02 PM ----------
This did not work
compute() {
echo "(0.569000006-0.369000018)/5.0" | awk '{ print $1 }'
}
awk ' BEGIN { printf "%f\n", (0.569000006-0.369000018) / 5.0 } '
Aney idea what's the problem with this?
awk -v a=0.569000006 -v b=0.369000018 -v c=5.0 '{res= (a-b)/c; print res;}'
You need a BEGIN
block which is executed once before any input is read. In this case BEGIN
block runs and program exits:
awk -v a=0.569000006 -v b=0.369000018 -v c=5.0 ' BEGIN {res= (a-b)/c; print res;}'
This line of code will be run once for every line of input given to awk. Since there are no input lines, the code isn't run. However:
awk -v a=0.569000006 -v b=0.369000018 -v c=5.0 'BEGIN{res= (a-b)/c; print res;}'
or
awk -v a=0.569000006 -v b=0.369000018 -v c=5.0 'END{res= (a-b)/c; print res;}'
should work for you.
And you can do the calculation directly in the print
awk -v a=0.569000006 -v b=0.369000018 -v c=5.0 'BEGIN {print (a-b)/c}'