Bc1

From CleanPosts

Jump to: navigation, search

Contents

Calculate factorials on the double

seq 52 | paste -sd* | bc

Convert a floating point number to binary

echo 'obase = 2; scale = 15; 3.141592653589793' | bc -l

Convert between numerical bases

#!/bin/bash
printf "hex2dec=";echo "ibase=16;$1"|bc
printf "dec2hex=";echo "obase=16;$1"|bc
printf "oct2dec=";echo "ibase=8;$1"|bc
printf "dec2oct=";echo "obase=8;$1"|bc

Convert temperatures

#!/bin/bash
c=$(echo "scale=1;($1-32)*(5/9)" | bc -l)
f=$(echo "scale=1;($1*(9/5))+32" | bc -l)
echo -e "\n\e[1;30m=$c C & $f F\e[0m\n"

Factorial function implemented by recursion

define fact(n) { if (n <= 1) return (n); return (n * fact(n-1)); }

Use the arctangent function in bc to obtain pi to 1000 digits.

bc 1.07.1

Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.

This is free software with ABSOLUTELY NO WARRANTY.

For details type `warranty'. 

scale=1000

4*a(1)

3.141592653589793238462643383279502884197169399375105820974944592307\

81640628620899862803482534211706798214808651328230664709384460955058\

22317253594081284811174502841027019385211055596446229489549303819644\

28810975665933446128475648233786783165271201909145648566923460348610\

45432664821339360726024914127372458700660631558817488152092096282925\

40917153643678925903600113305305488204665213841469519415116094330572\

70365759591953092186117381932611793105118548074462379962749567351885\

75272489122793818301194912983367336244065664308602139494639522473719\

07021798609437027705392171762931767523846748184676694051320005681271\

45263560827785771342757789609173637178721468440901224953430146549585\

37105079227968925892354201995611212902196086403441815981362977477130\

99605187072113499999983729780499510597317328160963185950244594553469\

08302642522308253344685035261931188171010003137838752886587533208381\

42061717766914730359825349042875546873115956286388235378759375195778\

18577805321712268066130019278766111959092164201988

quit

Recursive C function for the Fibonacci series, which calls itself twice in the same function

define fibonacci(n) {if((n==1)||(n==0)) {return(n);} else {return(fibonacci(n-1)+fibonacci(n-2));}} 

scale=10

fibonacci(20) / fibonacci(19)

1.6180339631

Return pi to the number of digits specified on the command line (Answer grows more accurate the longer the fractional part)

#!/bin/bash 
echo "scale=$1;a(1)*4" | bc -l

Power function (a ^ b)

define pow(a, b) {
   if (scale(b) == 0) {
       return a ^ b;
   }
   return e(b*l(a));
}

Trig functions

scale = 50
p = 4 * a(1)  /* pi */
d = p / 180   /* one degree in radians */
 
Using radians:

"  sin(-pi / 6) = "; s(-p / 6)
"  cos(3 * pi / 4) = "; c(3 * p / 4)
"  tan(pi / 3) = "; t(p / 3)
"  asin(-1 / 2) = "; y(-1 / 2)
"  acos(-sqrt(2) / 2) = "; x(-sqrt(2) / 2)
"  atan(sqrt(3)) = "; a(sqrt(3))
 
"Using degrees:
"
"  sin(-30) = "; s(-30 * d)
"  cos(135) = "; c(135 * d)
"  tan(60) = "; t(60 * d)
"  asin(-1 / 2) = "; y(-1 / 2) / d
"  acos(-sqrt(2) / 2) = "; x(-sqrt(2) / 2) / d
"  atan(sqrt(3)) = "; a(sqrt(3)) / d
Personal tools
Strangers In Paradise