Reputation:
I would like to do the following operation in my script:
1 - ((m - 20) / 34)
I would like to assign the result of this operation to another variable. I want my script use floating point math. For example, for m = 34:
results = 1 - ((34 - 20) / 34) == 0.588
Upvotes: 50
Views: 90791
Reputation: 2786
Just to note that many times you actually don’t need floating point arithmetic. For example, instead of
results = 1 - ((34 - 20) / 34) == 0.588
you can always write
results = 1000 - (((34 - 20) * 1000) / 34) == 588
which is the equivalent.
Upvotes: 0
Reputation: 88674
Teach bash e.g. integer division with floating point results:
#!/bin/bash
div () # Arguments: dividend and divisor
{
if [ $2 -eq 0 ]; then echo division by 0; return 1; fi
local p=12 # precision
local c=${c:-0} # precision counter
local d=. # decimal separator
local r=$(($1/$2)); echo -n $r # result of division
local m=$(($r*$2))
[ $c -eq 0 ] && [ $m -ne $1 ] && echo -n $d
[ $1 -eq $m ] || [ $c -eq $p ] && echo && return
local e=$(($1-$m))
c=$(($c+1))
div $(($e*10)) $2
}
result=$(div 1080 633) # write to variable
echo $result
result=$(div 7 34)
echo $result
result=$(div 8 32)
echo $result
result=$(div 246891510 2)
echo $result
result=$(div 5000000 177)
echo $result
Output:
1.706161137440
0.205882352941
0.25
123445755
28248.587570621468
Upvotes: 20
Reputation: 110
I know this is an old thread, but this seemed like a fun project to tackle without using bc
or invoking recursion. I'm sure it can be improved, but this maxed out my skill.
numerator=5
denominator=7 # - 0 -> returns "undef"
decimal_places=4 # - 0 -> same as echo $(( $numerator / $denominator ))
_result_sign=""
let _dp_exp=10**decimal_places
if [ $denominator -eq 0 ]; then _div_result_int_large=0; else let _div_result_int_large=$((numerator * _dp_exp / denominator)); fi
if [ $_div_result_int_large -lt 0 ]; then let _div_result_int_large=$(( _div_result_int_large * -1 )); _result_sign="-"; fi
let _div_result_int=$((_div_result_int_large / _dp_exp))
let _div_result_mant=$((_div_result_int_large - _div_result_int * _dp_exp))
let _dp_lzeros=$((decimal_places - ${#_div_result_mant}))
printf -v _div_result_mant_padded "%.${_dp_lzeros}d$_div_result_mant"
div_result="$_result_sign$_div_result_int"
if [ $decimal_places -gt 0 ]; then div_result="$_result_sign$_div_result_int.$_div_result_mant_padded"; fi
if [ $denominator -eq 0 ]; then div_result="undef"; fi
echo $div_result
Example output:
numerator=5
denominator=7
decimal_places=5
-> 0.71428
numerator=250
denominator=13
decimal_places=0
-> 19
numerator=-5
denominator=6
decimal_places=2
-> -0.83
numerator=3
denominator=0 # - uh-oh
decimal_places=2 # - can be anything, in this case
-> undef
Upvotes: 1
Reputation: 361739
You could use the bc
calculator. It will do arbitrary precision math using decimals (not binary floating point) if you set increease scale
from its default of 0:
$ m=34
$ bc <<< "scale = 10; 1 - (($m - 20) / 34)"
.5882352942
The -l
option will load the standard math library and default the scale to 20:
$ bc -l <<< "1 - (($m - 20) / 34)"
.58823529411764705883
You can then use printf to format the output, if you so choose:
printf "%.3f\n" "$(bc -l ...)"
Upvotes: 61
Reputation: 101
echo $a/$b|bc -l
gives the result.
Example:
read a b
echo $a/$b|bc -l
Enter a & b value as 10 3, you get 3.3333333333
If you want to store the value in another variable then use the code
read a b
c=`echo $a/$b|bc -l`
echo $c
It also gives the same result as above. Try it...
Upvotes: 10
Reputation: 435
Use this script open this file with favorite editor like:
$ sudo vim /usr/bin/div
Then paste this code:
#!/bin/bash
# Author: Danial Rikhteh Garan ([email protected])
if [[ -z "$1" ]] || [[ -z "$2" ]]; then
echo "Please input two number"
echo "for 100/50 use: div 10 50"
exit 1;
fi
div=$(echo "$1/$2" | bc -l);
echo 0$div | sed 's/[0]*$//g'
Now chmod it to 755:
$ sudo chmod 755 /usr/bin/div
Now use it:
$ div 5 100
0.05
In your script you can use this:
var=$(div 5 100);
echo "$var"
Upvotes: -1
Reputation: 34964
Bash does not do floating point math. You can use awk or bc to handle this. Here is an awk example:
$ m=34; awk -v m=$m 'BEGIN { print 1 - ((m - 20) / 34) }'
0.588235
To assign the output to a variable:
var=$(awk -v m=$m 'BEGIN { print 1 - ((m - 20) / 34) }')
Upvotes: 27