How to use expr on float? - bash

I know it's really stupid question, but I don't know how to do this in bash:
20 / 30 * 100
It should be 66.67 but expr is saying 0, because it doesn't support float.
What command in Linux can replace expr and do this equalation?

bc will do this for you, but the order is important.
> echo "scale = 2; 20 * 100 / 30" | bc
66.66
> echo "scale = 2; 20 / 30 * 100" | bc
66.00
or, for your specific case:
> export ach_gs=2
> export ach_gs_max=3
> x=$(echo "scale = 2; $ach_gs * 100 / $ach_gs_max" | bc)
> echo $x
66.66
Whatever method you choose, this is ripe for inclusion as a function to make your life easier:
#!/bin/bash
function pct () {
echo "scale = $3; $1 * 100 / $2" | bc
}
x=$(pct 2 3 2) ; echo $x # gives 66.66
x=$(pct 1 6 0) ; echo $x # gives 16

just do it in awk
# awk 'BEGIN{print 20 / 30 * 100}'
66.6667
save it to variable
# result=$(awk 'BEGIN{print 20 / 30 * 100}')
# echo $result
66.6667

I generally use perl:
perl -e 'print 10 / 3'

As reported in the bash man page:
The shell allows arithmetic expressions to be evaluated, under certain circumstances...Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error.
You can multiply by 100 earlier to get a better, partial result:
let j=20*100/30
echo $j
66
Or by a higher multiple of 10, and imagine the decimal place where it belongs:
let j=20*10000/30
echo $j
66666

> echo "20 / 30 * 100" | bc -l
66.66666666666666666600
This is a simplification of the answer by paxdiablo. The -l sets the scale (number of digits after the decimal) to 20. It also loads a math library with trig functions and other things.

Another obvious option:
python -c "print(20 / 30 * 100)"
assuming you are using Python 3. Otherwise, use python3.

Related

how to divide and multiply variables inside quote marks

I am trying to get a result of percentage with the following;
for FNAME in *_1.fq
do
FILE=${FNAME##*/}
SAMPLE=${FILE%_1.fq}
echo "processing ${SAMPLE}"
let "Raw_reads = $((`cat ${SAMPLE}_1.fq | wc -l` / 4)) + $((`cat ${SAMPLE}_2.fq | wc -l` / 4))"
let "Error_corrected = $((`cat unfixrm_${SAMPLE}_1.cor.fq | wc -l` / 4)) + $((`cat unfixrm_${SAMPLE}_2.cor.fq | wc -l` / 4))"
let "Error_corrected_per = ($Error_corrected / $Raw_reads) * 100"
echo -e "$SAMPLE\t$Raw_reads\t$Error_corrected($Error_corrected_per%)"
done
I get this result; how do I get (44%) and (20%)
processing FB_0d
FB_0d 100 44(0%)
processing FB_2d
FB_2d 200 40(0%)
Since, i.e., 44/200 equals 0 in integer arithmetic, and multiplying 0 with anything also results in 0, I suggest the following alternatives:
Either do, in your formula, multiplication by 100 first, and division last, i.e.
((Error_corrected_per = (Error_corrected * 100) / Raw_reads))
Another possibility (if this is an option for you) would be to rewrite your program in, for instance, zsh instead of bash, where you have floating point arithmetic.

How do I perform this calculation and output it to standard out?

I am trying to do this in Bash:
read n
echo int(math.ceil((math.sqrt(1 + 8 * n) - 1) / 2))
Of course this isn't working syntax but I am just putting it there so you can tell what I am trying to do.
Is there an easy way to actually make this into valid Bash?
Although you ask to do this in Bash, there's no native support for functions like square root or ceiling. It would be simpler to delegate to Perl:
perl -wmPOSIX -e "print POSIX::ceil((sqrt(1 + 8 * $n) - 1) / 2)"
Alternatively, you could use bc to calculate the square root, and some Bash to calculate the ceiling.
Let's define a function that prints the result of the formula with sqrt of bc:
formula() {
local n=$1
bc -l <<< "(sqrt(1 + 8 * $n) - 1) / 2"
}
The -l flag changes the scale from the default 0 to 20.
This affects the scale in the display of floating point results.
For example, with the default zero, 10 / 3 would print just 3.
We need the floating point details in the next step to compute the ceiling.
ceil() {
local n=$1
local intpart=${n%%.*}
if [[ $n =~ \.00*$ ]]; then
echo $intpart
else
echo $((intpart + 1))
fi
}
The idea here is to extract the integer part,
and if the decimal part is all zeros, then we print simply the integer part,
otherwise the integer part + 1, as that is the ceiling.
And a final simple function that combines the above functions to get the result that you want:
compute() {
local n=$1
ceil $(formula $n)
}
And a checker function to test it:
check() {
local n num
for n; do
num=$(formula $n)
echo $n $num $(compute $n)
done
}
Let's try it:
check 1 2 3 4 7 11 12 16 17
It produces:
1 1.00000000000000000000 1
2 1.56155281280883027491 2
3 2.00000000000000000000 2
4 2.37228132326901432992 3
7 3.27491721763537484861 4
11 4.21699056602830190566 5
12 4.42442890089805236087 5
16 5.17890834580027361089 6
17 5.35234995535981255455 6
You can use bc's sqrt function.
echo "(sqrt(1 + 8 * 3) - 1) / 2" | bc
Ceil function can be implemented using any of the methods described in this answer.
Getting Ceil integer
For eg:
ceiling_divide() {
ceiling_result=`echo "($1 + $2 - 1)/$2" | bc`
}
You can use bc for all the job
$>cat filebc
print "Enter a number\n";
scale=20
a=read()
b=((sqrt(1 + 8 * a) - 1) / 2)
scale=0
print "ceil = ";
((b/1)+((b%1)>0))
quit
Call it like that
bc -q filebc

How to calculate percentage in shell script

I used the below line of script in my shell code
Percent=echo "scale=2; $DP*100/$SDC" | bc
it returns .16 as output but i need it as 0.16
Posix-compliant solution using bc:
#!/bin/sh
Percent="$(echo "
scale=2;
a = $DP * 100 / $SDC;
if (a > -1 && a < 0) { print "'"-0"'"; a*=-1; }
else if (a < 1 && a > 0) print 0;
a" | bc)"
That's kind of ugly with all of those special checks for cases when the answer is between -1 and 1 (exclusive) but not zero.
Let's use this Posix-compliant solution using awk instead:
#!/bin/sh
Percent="$(echo "$DP" "$SDC" |awk '{printf "%.2f", $1 * 100 / $2}')"
Z shell can do this natively:
#!/bin/zsh
Percent="$(printf %.2f $(( DP * 100. / SDC )) )"
(The dot is necessary to instruct zsh to use floating point math.)
Native Posix solution using string manipulation (assumes integer inputs):
#!/bin/sh
# # e.g. round down e.g. round up
# # DP=1 SDC=3 DP=2 SDC=3
Percent=$(( DP * 100000 / SDC + 5)) # Percent=33338 Percent=66671
Whole=${Percent%???} # Whole=33 Whole=66
Percent=${Percent#$Whole} # Percent=338 Percent=671
Percent=$Whole.${Percent%?} # Percent=33.33 Percent=66.67
This calculates 1,000 times the desired answer so that we have all the data we need for the final resolution to the hundredths. It adds five so we can properly truncate the thousandths digit. We then define temporary variable $Whole to be just the truncated integer value, we temporarily strip that from $Percent, then we append a dot and the decimals, excluding the thousandths (which we made wrong so we could get the hundredths rounded properly).

Correct usage of bc in a shell script?

I'm simply trying to multiplying some float variables using bc:
#!/bin/bash
a=2.77 | bc
b=2.0 | bc
for cc in $(seq 0. 0.001 0.02)
do
c=${cc} | bc
d=$((a * b * c)) | bc
echo "$d" | bc
done
And this does not give me an output. I know it's a silly one but I've tried a number of combinations of bc (piping it in different places etc.) to no avail.
Any help would be greatly appreciated!
bc is a command-line utility, not some obscure part of shell syntax. The utility reads mathematical expressions from its standard input and prints values to its standard output. Since it is not part of the shell, it has no access to shell variables.
The shell pipe operator (|) connects the standard output of one shell command to the standard input of another shell command. For example, you could send an expression to bc by using the echo utility on the left-hand side of a pipe:
echo 2+2 | bc
This will print 4, since there is no more here than meets the eye.
So I suppose you wanted to do this:
a=2.77
b=2.0
for c in $(seq 0. 0.001 0.02); do
echo "$a * $b * $c" | bc
done
Note: The expansion of the shell variables is happening when the shell processes the argument to echo, as you could verify by leaving off the bc:
a=2.77
b=2.0
for c in $(seq 0. 0.001 0.02); do
echo -n "$a * $b * $c" =
echo "$a * $b * $c" | bc
done
So bc just sees numbers.
If you wanted to save the output of bc in a variable instead of sending it to standard output (i.e. the console), you could do so with normal command substitution syntax:
a=2.77
b=2.0
for c in $(seq 0. 0.001 0.02); do
d=$(echo "$a * $b * $c" | bc)
echo "$d"
done
To multiply two numbers directly, you would do something like:
echo 2.77 * 2.0 | bc
It will produce a result to 2 places - the largest number of places of the factors. To get it to a larger number of places, like 5, would require:
echo "scale = 5; 2.77 * 2.0" | bc
This becomes more important if you're multiplying numerals that each have a large number of decimal places.
As stated in other replies, bc is not a part of bash, but is a command run by bash. So, you're actually sending input directly to the command - which is why you need echo. If you put it in a file (named, say, "a") then you'd run "bc < a". Or, you can put the input directly in the shell script and have a command run the designated segment as its input; like this:
cat <<EOF
Input
EOF
... with qualifiers (e.g. you need to write "" as "\", for instance).
Control flow constructs may be more problematic to run in BC off the command line. I tried the following
echo "scale = 6; a = 2.77; b = 2.0; define f(cc) { auto c, d; c = cc; d = a*b*c; return d; } f(0); f(0.001); f(0.02)" | bc
and got a syntax error (I have a version of GNU-BC installed). On the other hand, it will run fine with C-BC
echo "scale = 6; a = 2.77; b = 2.0; define f(cc) { auto c, d; c = cc; d = a * b * c; return d; } f(0); f(0.001); f(0.02)" | cbc
and give you the expected result - matching the example you cited ... listing numbers to 6 places.
C-BC is here (it's operationally a large superset of GNU-BC and UNIX BC, but not 100% POSIX compliant):
https://github.com/RockBrentwood/CBC
The syntax is closer to C, so you could also write it as
echo "scale = 6, a = 2.77, b = 2.0; define f(cc) { return a * b * cc; } f(0); f(0.001); f(0.02)" | cbc
to get the same result. So, as another example, this
echo "scale = 100; for (x = 0, y = 1; x < 50; y *= ++x); y" | cbc
will give you 50 factorial. However, comma-expressions, like (x = 0, y = 1) are not mandated for bc by POSIX, so it will not run in other bc dialects, like GNU BC.

How do I get bc(1) to print the leading zero?

I do something like the following in a Makefile:
echo "0.1 + 0.1" | bc
(in the real file the numbers are dynamic, of course)
It prints .2 but I want it to print 0.2.
I would like to do this without resorting to sed but I can't seem to find how to get bc to print the zero. Or is bc just not able to do this?
You can also resort to awk to format:
echo "0.1 + 0.1" | bc | awk '{printf "%f", $0}'
or with awk itself doing the math:
echo "0.1 0.1" | awk '{printf "%f", $1 + $2}'
This might work for you:
echo "x=0.1 + 0.1; if(x<1) print 0; x" | bc
After a quick look at the source (see bc_out_num(), line 1461), I don't see an obvious way to make the leading 0 get printed if the integer portion is 0. Unless I missed something, this behaviour is not dependent on a parameter which can be changed using command-line flag.
Short answer: no, I don't think there's a way to make bc print numbers the way you want.
I don't see anything wrong with using sed if you still want to use bc. The following doesn't look that ghastly, IMHO:
[me#home]$ echo "0.1 + 0.1" | bc | sed 's/^\./0./'
0.2
If you really want to avoid sed, both eljunior's and choroba's suggestions are pretty neat, but they require value-dependent tweaking to avoid trailing zeros. That may or may not be an issue for you.
I cannot find anything about output format in the documentation. Instead of sed, you can also reach for printf:
printf '%3.1f\n' $(bc<<<0.1+0.1)
echo "$a / $b" | bc -l | sed -e 's/^-\./-0./' -e 's/^\./0./'
This should work for all cases where the results are:
"-.123"
".123"
"-1.23"
"1.23"
Explanation:
For everything that only starts with -., replace -. with -0.
For everything that only starts with ., replace . with 0.
Building on potongs answer,
For fractional results:
echo "x=0.1 + 0.1; if(x<1 && x > 0) print 0; x" | bc -l
Note that negative results will not be displayed correctly. Aquarius Power has a solution for that.
$ bc -l <<< 'x=-1/2; if (length (x) == scale (x) && x != 0) { if (x < 0) print "-",0,-x else print 0,x } else print x'
This one is pure bc. It detects the leading zero by comparing the result of the length with the scale of the expression. It works on both positive and negative number.
This one will also handle negative numbers:
echo "0.1 - 0.3" | bc | sed -r 's/^(-?)\./\10./'
For positive numbers, it may be as simple as printing (an string) zero:
$ echo '"0";0.1+0.1' | bc
0.2
avoid the zero if the number is bigger (or equal) to 1:
$ echo 'x=0.1+0.1; if(x<1){"0"}; x' | bc
0.2
It gets a bit more complex if the number may be negative:
echo 'x= 0.3 - 0.5 ; s=1;if(x<0){s=-1};x*=s;if(s<0){"-"};if(x<1) {"0"};x' | bc
-0.2
You may define a function and add it to a library:
$ echo 'define leadzero(x){auto s;
s=1;if(x<0){s=-1};x*=s;if(s<0){"-"};if(x<1){"0"};
return(x)};
leadzero(2.1-12.4)' | bc
-10.3
$ echo 'define leadzero(x){auto s;
s=1;if(x<0){s=-1};x*=s;if(s<0){"-"};if(x<1){"0"};
return(x)};
leadzero(0.1-0.4)' | bc
-0.3
Probably, bc isn't really the best "bench calculator" for the modern age. Other languages will give you more control. Here are working examples that print values in the range (-1.0..+1.0) with a leading zero. These examples use bc, AWK, and Python 3, along with Here String syntax.
#!/bin/bash
echo "using bc"
time for (( i=-2; i<=+2; i++ ))
{
echo $(bc<<<"scale=1; x=$i/2; if (x==0||x<=-1||x>=1) { print x } else { if (x<0) { print \"-0\";-x } else { print \"0\";x } } ")
}
echo
echo "using awk"
time for (( i=-2; i<=+2; i++ ))
{
echo $(echo|awk "{printf \"%.1f\",$i/2}")
}
echo
echo "using Python"
time for (( i=-2; i<=+2; i++ ))
{
echo $(python3<<<"print($i/2)")
}
Note that the Python version is about 10x slower, if that matters (still very fast for most purposes).
Doing any non-trivial math with sh or bc is a fool's errand. There are much better bench calculators available nowadays. For example, you can embed and execute Python subroutines inside your Bash scripts using Here Documents.
function mathformatdemo {
python3<<SCRIPT
import sys
from math import *
x=${1} ## capture the parameter from the shell
if -1<=x<=+1:
#print("debug: "+str(x),file=sys.stderr)
y=2*asin(x)
print("2*asin({:2.0f})={:+6.2f}".format(x,y))
else: print("domain err")
SCRIPT
}
echo "using Python via Here-doc"
time for (( i=-2; i<=+2; i++ ))
{
echo $(mathformatdemo $i)
}
Output:
using Python via Here-doc
domain err
2*asin(-1)= -3.14
2*asin( 0)= +0.00
2*asin( 1)= +3.14
domain err
this only uses bc, and works with negative numbers:
bc <<< "x=-.1; if(x==0) print \"0.0\" else if(x>0 && x<1) print 0,x else if(x>-1 && x<0) print \"-0\",-x else print x";
try it with:
for y in "0" "0.1" "-0.1" "1.1" "-1.1"; do
bc <<< "x=$y; if(x==0) print \"0.0\" else if(x>0 && x<1) print 0,x else if(x>-1 && x<0) print \"-0\",-x else print x";
echo;
done
Another simple way, similar to one of the posts in this thread here:
echo 'x=0.1+0.1; print "0",x,"\n"' | bc
Print the list of variables, including the leading 0 and the newline.
Since you have the question tagged [bash] you can simply compute the answer and save it to a variable using command substitution (e.g. r="$(...)") and then using [[..]] with =~ to test if the first character in the result is [1-9] (e.g. [[ $r =~ ^[1-9].*$ ]]), and if the first character isn't, prepend '0' to the beginning of r, e.g.
r=$(echo "0.1 + 0.1" | bc) # compute / save result
[[ $r =~ ^[1-9].*$ ]] || r="0$r" # test 1st char [1-9] or prepend 0
echo "$r" # output result
Result
0.2
If the result r is 1.0 or greater, then no zero is prepended, e.g. (as a 1-liner)
$ r=$(echo "0.8 + 0.6" | bc); [[ $r =~ ^[1-9].*$ ]] || r="0$r"; echo "$r"
1.4

Resources