正弦、余弦和正切

基本类型 / 数字 / 数学运算

#bash has no fractional numbers; in bc -l s() is the sine,
#c() the cosine and a() the arctangent, there is no tan() and no pi
pi=$(bc -l <<< "4 * a(1)")
#pi is 3.14159265358979323844

#the functions take radians
sin90=$(bc -l <<< "s($pi / 2)")
#sin90 is 1.00000000000000000000

cos180=$(bc -l <<< "c($pi)")
#cos180 is -1.00000000000000000000

#tangent is the sine divided by the cosine
tan45=$(bc -l <<< "s($pi / 4) / c($pi / 4)")
#tan45 is .99999999999999999998

#awk has sin(), cos() and atan2(); atan2(0, -1) is pi
#degrees to radians
sin30=$(awk -v degrees=30 \
    'BEGIN { pi = atan2(0, -1); print sin(degrees * pi / 180) }')
#sin30 is 0.5

echo "sin90 is $sin90"
echo "cos180 is $cos180"
echo "tan45 is $tan45"
echo "sin30 is $sin30"