Sample Header Ad - 728x90

bc scale: How to avoid rounding? (Calculate small binomial probability)

5 votes
5 answers
6940 views
Following code calculates the Binomial Probability of a success event k out of n trials: n=144 prob=$(echo "0.0139" | bc) echo -e "Enter no.:" read passedno k=$passedno nCk2() { num=1 den=1 for((i = 1; i <= $2; ++i)); do ((num *= $1 + 1 - i)) && ((den *= i)) done echo $((num / den)) } binomcoef=$(nCk2 $n $k) binprobab=$(echo "scale=8; $binomcoef*($prob^$k)*((1-$prob)^($n-$k))" | bc) echo $binprobab When for $passedno (=k) "5" is entered, then the result is shown as 0 (instead of "0.03566482") whereas with "4" passed I get ".07261898". **How can I print the output with given precision of 8 decimal digits without getting the rounded value of the output?**
Asked by Aliakbar Ahmadi (203 rep)
Jul 29, 2015, 10:58 AM
Last activity: Dec 15, 2019, 01:37 AM