Comprehensive Reference Guide — Using bc, awk, and other tools for advanced mathematics
Bash doesn't have built-in trigonometry, but bc -l and awk provide complete math libraries. This guide covers sine, cosine, tangent, inverse functions, logarithms, exponentials, and practical applications for system administration, data analysis, and scientific computing.
bc -l: The -l flag loads the math library with sin, cos, atan, log, exp, and more. All trig functions use radians.
awk: Built-in math functions including sin, cos, atan2, log, exp, sqrt. Also uses radians.
| Function | Description | Example | Result |
|---|---|---|---|
s(x) | Sine (radians) | s(1.5708) | ≈ 1 |
c(x) | Cosine (radians) | c(0) | 1 |
a(x) | Arctangent (radians) | a(1) | ≈ 0.7854 (π/4) |
l(x) | Natural logarithm (ln) | l(2.718) | ≈ 1 |
e(x) | Exponential (e^x) | e(1) | ≈ 2.718 |
sqrt(x) | Square root | sqrt(16) | 4 |
j(n,x) | Bessel function | j(0,1) | ≈ 0.7652 |
radians = degrees × π / 1804 * a(1) in bc
| Function | Description | Example |
|---|---|---|
sin(x) | Sine (radians) | sin(3.14159/2) |
cos(x) | Cosine (radians) | cos(0) |
atan2(y,x) | Arctangent of y/x | atan2(1,1) |
exp(x) | Exponential (e^x) | exp(1) |
log(x) | Natural logarithm | log(2.718) |
sqrt(x) | Square root | sqrt(2) |
int(x) | Integer part | int(3.7) |
rand() | Random 0-1 | rand() |
These functions aren't built-in but can be derived:
| Function | bc Formula | awk Formula |
|---|---|---|
| Tangent | s(x)/c(x) | sin(x)/cos(x) |
| Arcsine | a(x/sqrt(1-x*x)) | atan2(x,sqrt(1-x*x)) |
| Arccosine | a(sqrt(1-x*x)/x) | atan2(sqrt(1-x*x),x) |
| Log base 10 | l(x)/l(10) | log(x)/log(10) |
| Log base n | l(x)/l(n) | log(x)/log(n) |
| Power (x^y) | e(y*l(x)) | x^y or exp(y*log(x)) |
π = 4*a(1) in bc = 3.14159265...
e = e(1) in bc = 2.71828182...
π = atan2(0,-1) in awk
Fundamental trig operations using bc's math library, including sine, cosine, and tangent.
#!/bin/bash
# Basic trig functions with bc -l
echo "=== Constants ==="
pi=$(echo "scale=10; 4*a(1)" | bc -l)
e=$(echo "scale=10; e(1)" | bc -l)
echo "π = $pi"
echo "e = $e"
echo ""
echo "=== Sine Function ==="
# sin(0) = 0, sin(π/2) = 1, sin(π) = 0
echo "sin(0) = $(echo "scale=6; s(0)" | bc -l)"
echo "sin(π/6) = $(echo "scale=6; s($pi/6)" | bc -l)" # 0.5
echo "sin(π/4) = $(echo "scale=6; s($pi/4)" | bc -l)" # √2/2 ≈ 0.707
echo "sin(π/2) = $(echo "scale=6; s($pi/2)" | bc -l)" # 1
echo "sin(π) = $(echo "scale=6; s($pi)" | bc -l)" # 0
echo ""
echo "=== Cosine Function ==="
echo "cos(0) = $(echo "scale=6; c(0)" | bc -l)" # 1
echo "cos(π/3) = $(echo "scale=6; c($pi/3)" | bc -l)" # 0.5
echo "cos(π/2) = $(echo "scale=6; c($pi/2)" | bc -l)" # 0
echo "cos(π) = $(echo "scale=6; c($pi)" | bc -l)" # -1
echo ""
echo "=== Tangent Function ==="
# tan(x) = sin(x)/cos(x)
tan() {
echo "scale=6; s($1)/c($1)" | bc -l
}
echo "tan(0) = $(tan 0)"
echo "tan(π/4) = $(tan "$pi/4")" # 1
echo "tan(π/3) = $(tan "$pi/3")" # √3 ≈ 1.732
echo ""
echo "=== Pythagorean Identity ==="
# sin²(x) + cos²(x) = 1
angle="$pi/6"
sin_sq=$(echo "scale=10; s($angle)^2" | bc -l)
cos_sq=$(echo "scale=10; c($angle)^2" | bc -l)
sum=$(echo "scale=10; $sin_sq + $cos_sq" | bc -l)
echo "sin²(π/6) + cos²(π/6) = $sum"
Converting between degrees and radians for practical trig calculations.
#!/bin/bash
# Degree and radian conversions
pi=$(echo "scale=10; 4*a(1)" | bc -l)
# Conversion functions
deg_to_rad() {
echo "scale=10; $1 * $pi / 180" | bc -l
}
rad_to_deg() {
echo "scale=10; $1 * 180 / $pi" | bc -l
}
echo "=== Degrees to Radians ==="
for deg in 0 30 45 60 90 180 270 360; do
rad=$(deg_to_rad $deg)
printf "%3d° = %s rad\n" $deg "$rad"
done
echo ""
echo "=== Radians to Degrees ==="
echo "π/6 rad = $(rad_to_deg "$pi/6")°"
echo "π/4 rad = $(rad_to_deg "$pi/4")°"
echo "π/2 rad = $(rad_to_deg "$pi/2")°"
echo "π rad = $(rad_to_deg "$pi")°"
echo "2π rad = $(rad_to_deg "2*$pi")°"
echo ""
echo "=== Trig with Degrees ==="
# sin(30°) = 0.5
sin_deg() {
local rad=$(deg_to_rad $1)
echo "scale=6; s($rad)" | bc -l
}
cos_deg() {
local rad=$(deg_to_rad $1)
echo "scale=6; c($rad)" | bc -l
}
echo "sin(30°) = $(sin_deg 30)"
echo "sin(45°) = $(sin_deg 45)"
echo "sin(90°) = $(sin_deg 90)"
echo "cos(60°) = $(cos_deg 60)"
echo "cos(90°) = $(cos_deg 90)"
echo ""
echo "=== Common Angle Table ==="
printf "%-6s %-10s %-10s %-10s\n" "Deg" "Rad" "Sin" "Cos"
printf "%-6s %-10s %-10s %-10s\n" "---" "---" "---" "---"
for deg in 0 30 45 60 90; do
rad=$(deg_to_rad $deg)
sin=$(sin_deg $deg)
cos=$(cos_deg $deg)
printf "%-6s %-10.4f %-10s %-10s\n" "${deg}°" "$rad" "$sin" "$cos"
done
Arcsine, arccosine, and arctangent for finding angles from ratios.
#!/bin/bash
# Inverse trig functions
pi=$(echo "scale=10; 4*a(1)" | bc -l)
# bc only has a(x) for arctangent
# We derive arcsin and arccos
# arcsin(x) = atan(x / sqrt(1 - x²))
arcsin() {
echo "scale=10; a($1/sqrt(1-$1*$1))" | bc -l
}
# arccos(x) = atan(sqrt(1 - x²) / x)
arccos() {
if (( $(echo "$1 > 0" | bc -l) )); then
echo "scale=10; a(sqrt(1-$1*$1)/$1)" | bc -l
else
echo "scale=10; $pi + a(sqrt(1-$1*$1)/$1)" | bc -l
fi
}
# arctan is built-in as a(x)
arctan() {
echo "scale=10; a($1)" | bc -l
}
rad_to_deg() {
echo "scale=4; $1 * 180 / $pi" | bc -l
}
echo "=== Arctangent ==="
echo "atan(0) = $(arctan 0) rad = $(rad_to_deg $(arctan 0))°"
echo "atan(1) = $(arctan 1) rad = $(rad_to_deg $(arctan 1))°"
echo "atan(√3) = $(arctan "sqrt(3)") rad = $(rad_to_deg $(arctan "sqrt(3)"))°"
echo ""
echo "=== Arcsine ==="
echo "asin(0) = $(arcsin 0) rad = $(rad_to_deg $(arcsin 0))°"
echo "asin(0.5) = $(arcsin 0.5) rad = $(rad_to_deg $(arcsin 0.5))°"
echo "asin(√2/2) = $(arcsin "sqrt(2)/2") rad = $(rad_to_deg $(arcsin "sqrt(2)/2"))°"
echo "asin(1) = $(arcsin 0.9999999) rad ≈ 90°"
echo ""
echo "=== Arccosine ==="
echo "acos(1) = $(arccos 1) rad = $(rad_to_deg $(arccos 1))°"
echo "acos(0.5) = $(arccos 0.5) rad = $(rad_to_deg $(arccos 0.5))°"
echo "acos(0) = $(echo "scale=10; $pi/2" | bc -l) rad = 90°"
echo ""
echo "=== Practical: Finding Angles ==="
# Given opposite=3, adjacent=4, find angle
opp=3
adj=4
angle_rad=$(arctan "$opp/$adj")
angle_deg=$(rad_to_deg $angle_rad)
echo "Triangle: opposite=$opp, adjacent=$adj"
echo "Angle = atan($opp/$adj) = $angle_deg°"
# Verify with hypotenuse
hyp=$(echo "scale=6; sqrt($opp^2 + $adj^2)" | bc -l)
echo "Hypotenuse = $hyp"
echo "sin(angle) = $opp/$hyp = $(echo "scale=6; $opp/$hyp" | bc -l)"
Natural logarithm, common logarithm (base 10), and exponential functions for scientific calculations.
#!/bin/bash
# Logarithms and exponentials
echo "=== Natural Logarithm (ln) ==="
echo "ln(1) = $(echo "scale=6; l(1)" | bc -l)"
echo "ln(e) = $(echo "scale=6; l(e(1))" | bc -l)"
echo "ln(2) = $(echo "scale=6; l(2)" | bc -l)"
echo "ln(10) = $(echo "scale=6; l(10)" | bc -l)"
echo ""
echo "=== Exponential (e^x) ==="
echo "e^0 = $(echo "scale=6; e(0)" | bc -l)"
echo "e^1 = $(echo "scale=6; e(1)" | bc -l)"
echo "e^2 = $(echo "scale=6; e(2)" | bc -l)"
echo "e^-1 = $(echo "scale=6; e(-1)" | bc -l)"
echo ""
echo "=== Log Base 10 ==="
# log₁₀(x) = ln(x) / ln(10)
log10() {
echo "scale=6; l($1)/l(10)" | bc -l
}
echo "log₁₀(1) = $(log10 1)"
echo "log₁₀(10) = $(log10 10)"
echo "log₁₀(100) = $(log10 100)"
echo "log₁₀(1000) = $(log10 1000)"
echo ""
echo "=== Log Base 2 ==="
log2() {
echo "scale=6; l($1)/l(2)" | bc -l
}
echo "log₂(1) = $(log2 1)"
echo "log₂(2) = $(log2 2)"
echo "log₂(8) = $(log2 8)"
echo "log₂(1024) = $(log2 1024)"
echo ""
echo "=== Arbitrary Base Power ==="
# x^y = e^(y * ln(x))
power() {
echo "scale=6; e($2 * l($1))" | bc -l
}
echo "2^10 = $(power 2 10)"
echo "10^3 = $(power 10 3)"
echo "2^0.5 = $(power 2 0.5)" # √2
echo "e^π = $(power "e(1)" "4*a(1)")"
echo ""
echo "=== Practical: Decibel Calculations ==="
# dB = 10 * log₁₀(P₂/P₁)
p1=1
p2=100
db=$(echo "scale=2; 10 * $(log10 "$p2/$p1")" | bc -l)
echo "Power ratio $p2:$p1 = ${db} dB"
# Reverse: ratio from dB
db_val=20
ratio=$(echo "scale=4; e($db_val/10 * l(10))" | bc -l)
echo "${db_val} dB = ratio of $ratio"
Using awk's built-in math functions for trig calculations in data processing.
#!/bin/bash
# Trigonometry with awk
echo "=== Basic awk Trig ==="
awk 'BEGIN {
pi = atan2(0, -1)
print "π =", pi
print ""
print "sin(0) =", sin(0)
print "sin(π/2) =", sin(pi/2)
print "cos(0) =", cos(0)
print "cos(π) =", cos(pi)
}'
echo ""
echo "=== awk Angle Table ==="
awk 'BEGIN {
pi = atan2(0, -1)
printf "%-8s %-10s %-10s %-10s\n", "Degrees", "Radians", "Sin", "Cos"
printf "%-8s %-10s %-10s %-10s\n", "-------", "-------", "---", "---"
for (deg = 0; deg <= 90; deg += 15) {
rad = deg * pi / 180
printf "%-8d %-10.4f %-10.4f %-10.4f\n", deg, rad, sin(rad), cos(rad)
}
}'
echo ""
echo "=== atan2 Function ==="
# atan2(y, x) handles all quadrants correctly
awk 'BEGIN {
pi = atan2(0, -1)
print "atan2(1, 1) =", atan2(1, 1) * 180/pi, "degrees" # 45°
print "atan2(1, -1) =", atan2(1, -1) * 180/pi, "degrees" # 135°
print "atan2(-1, -1) =", atan2(-1, -1) * 180/pi, "degrees" # -135°
print "atan2(-1, 1) =", atan2(-1, 1) * 180/pi, "degrees" # -45°
}'
echo ""
echo "=== Process Data with Trig ==="
# Convert polar to cartesian
echo "5 30
10 45
15 60
20 90" | awk '
BEGIN {
pi = atan2(0, -1)
printf "%-6s %-8s %-10s %-10s\n", "r", "θ(deg)", "x", "y"
}
{
r = $1
theta_deg = $2
theta_rad = theta_deg * pi / 180
x = r * cos(theta_rad)
y = r * sin(theta_rad)
printf "%-6.1f %-8.1f %-10.4f %-10.4f\n", r, theta_deg, x, y
}'
atan2(y, x) correctly handles all four quadrants and avoids division by zero. Always prefer it over simple arctangent.
Hyperbolic sine, cosine, and tangent functions for engineering and physics applications.
#!/bin/bash
# Hyperbolic functions
# sinh(x) = (e^x - e^-x) / 2
sinh() {
echo "scale=6; (e($1) - e(-$1)) / 2" | bc -l
}
# cosh(x) = (e^x + e^-x) / 2
cosh() {
echo "scale=6; (e($1) + e(-$1)) / 2" | bc -l
}
# tanh(x) = sinh(x) / cosh(x)
tanh() {
echo "scale=6; (e($1) - e(-$1)) / (e($1) + e(-$1))" | bc -l
}
echo "=== Hyperbolic Sine ==="
echo "sinh(0) = $(sinh 0)"
echo "sinh(1) = $(sinh 1)"
echo "sinh(2) = $(sinh 2)"
echo ""
echo "=== Hyperbolic Cosine ==="
echo "cosh(0) = $(cosh 0)"
echo "cosh(1) = $(cosh 1)"
echo "cosh(2) = $(cosh 2)"
echo ""
echo "=== Hyperbolic Tangent ==="
echo "tanh(0) = $(tanh 0)"
echo "tanh(1) = $(tanh 1)"
echo "tanh(2) = $(tanh 2)"
echo "tanh(10) = $(tanh 10)" # Approaches 1
echo ""
echo "=== Hyperbolic Identity ==="
# cosh²(x) - sinh²(x) = 1
x=1.5
cosh_sq=$(echo "scale=10; ($(cosh $x))^2" | bc -l)
sinh_sq=$(echo "scale=10; ($(sinh $x))^2" | bc -l)
diff=$(echo "scale=10; $cosh_sq - $sinh_sq" | bc -l)
echo "cosh²($x) - sinh²($x) = $diff"
echo ""
echo "=== Inverse Hyperbolic (arcsinh) ==="
# arcsinh(x) = ln(x + sqrt(x² + 1))
arcsinh() {
echo "scale=6; l($1 + sqrt($1^2 + 1))" | bc -l
}
echo "arcsinh(0) = $(arcsinh 0)"
echo "arcsinh(1) = $(arcsinh 1)"
echo "arcsinh(sinh(2)) = $(arcsinh $(sinh 2))"
echo ""
echo "=== Practical: Catenary Curve ==="
# y = a * cosh(x/a) - cable hanging under gravity
a=10 # Parameter
echo "Catenary (a=$a):"
for x in -20 -10 0 10 20; do
y=$(echo "scale=4; $a * ($(cosh "$x/$a"))" | bc -l)
printf " x=%3d: y=%s\n" $x "$y"
done
Practical geometry calculations including distance, midpoint, and angle between points.
#!/bin/bash
# Distance and coordinate calculations
pi=$(echo "scale=10; 4*a(1)" | bc -l)
echo "=== Euclidean Distance ==="
# d = sqrt((x₂-x₁)² + (y₂-y₁)²)
distance() {
local x1=$1 y1=$2 x2=$3 y2=$4
echo "scale=6; sqrt(($x2-$x1)^2 + ($y2-$y1)^2)" | bc -l
}
echo "Distance (0,0) to (3,4) = $(distance 0 0 3 4)"
echo "Distance (1,2) to (4,6) = $(distance 1 2 4 6)"
echo "Distance (-1,-1) to (2,3) = $(distance -1 -1 2 3)"
echo ""
echo "=== 3D Distance ==="
distance3d() {
local x1=$1 y1=$2 z1=$3 x2=$4 y2=$5 z2=$6
echo "scale=6; sqrt(($x2-$x1)^2 + ($y2-$y1)^2 + ($z2-$z1)^2)" | bc -l
}
echo "Distance (0,0,0) to (1,2,2) = $(distance3d 0 0 0 1 2 2)"
echo ""
echo "=== Angle Between Points ==="
# θ = atan2(y₂-y₁, x₂-x₁)
angle_between() {
local x1=$1 y1=$2 x2=$3 y2=$4
awk -v x1="$x1" -v y1="$y1" -v x2="$x2" -v y2="$y2" '
BEGIN {
angle = atan2(y2-y1, x2-x1)
print angle * 180 / atan2(0,-1)
}'
}
echo "Angle from (0,0) to (1,1) = $(angle_between 0 0 1 1)°"
echo "Angle from (0,0) to (1,0) = $(angle_between 0 0 1 0)°"
echo "Angle from (0,0) to (0,1) = $(angle_between 0 0 0 1)°"
echo ""
echo "=== Polar to Cartesian ==="
polar_to_cart() {
local r=$1 theta_deg=$2
local theta_rad=$(echo "scale=10; $theta_deg * $pi / 180" | bc -l)
local x=$(echo "scale=6; $r * c($theta_rad)" | bc -l)
local y=$(echo "scale=6; $r * s($theta_rad)" | bc -l)
echo "x=$x, y=$y"
}
echo "r=10, θ=30°: $(polar_to_cart 10 30)"
echo "r=5, θ=45°: $(polar_to_cart 5 45)"
echo "r=8, θ=60°: $(polar_to_cart 8 60)"
echo ""
echo "=== Cartesian to Polar ==="
cart_to_polar() {
local x=$1 y=$2
local r=$(echo "scale=6; sqrt($x^2 + $y^2)" | bc -l)
local theta=$(awk -v x="$x" -v y="$y" 'BEGIN { print atan2(y,x) * 180 / atan2(0,-1) }')
echo "r=$r, θ=${theta}°"
}
echo "(3, 4): $(cart_to_polar 3 4)"
echo "(5, 5): $(cart_to_polar 5 5)"
echo "(0, 10): $(cart_to_polar 0 10)"
Calculate the great-circle distance between two points on Earth using latitude and longitude.
#!/bin/bash
# Haversine formula for geographic distance
# Earth's radius in kilometers
EARTH_RADIUS=6371
haversine() {
local lat1=$1 lon1=$2 lat2=$3 lon2=$4
awk -v lat1="$lat1" -v lon1="$lon1" -v lat2="$lat2" -v lon2="$lon2" -v R="$EARTH_RADIUS" '
BEGIN {
pi = atan2(0, -1)
# Convert to radians
lat1_r = lat1 * pi / 180
lat2_r = lat2 * pi / 180
dlat = (lat2 - lat1) * pi / 180
dlon = (lon2 - lon1) * pi / 180
# Haversine formula
a = sin(dlat/2)^2 + cos(lat1_r) * cos(lat2_r) * sin(dlon/2)^2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = R * c
printf "%.2f", distance
}'
}
echo "=== Great Circle Distances ==="
echo ""
# New York to London
ny_lat=40.7128; ny_lon=-74.0060
london_lat=51.5074; london_lon=-0.1278
dist=$(haversine $ny_lat $ny_lon $london_lat $london_lon)
echo "New York to London: $dist km"
# Los Angeles to Tokyo
la_lat=34.0522; la_lon=-118.2437
tokyo_lat=35.6762; tokyo_lon=139.6503
dist=$(haversine $la_lat $la_lon $tokyo_lat $tokyo_lon)
echo "Los Angeles to Tokyo: $dist km"
# Sydney to Cape Town
sydney_lat=-33.8688; sydney_lon=151.2093
cape_lat=-33.9249; cape_lon=18.4241
dist=$(haversine $sydney_lat $sydney_lon $cape_lat $cape_lon)
echo "Sydney to Cape Town: $dist km"
# Short distance: Binghamton to NYC
bing_lat=42.0987; bing_lon=-75.9180
nyc_lat=40.7128; nyc_lon=-74.0060
dist=$(haversine $bing_lat $bing_lon $nyc_lat $nyc_lon)
echo "Binghamton to NYC: $dist km ($(echo "scale=1; $dist * 0.621371" | bc) miles)"
echo ""
echo "=== Bearing Calculation ==="
# Initial bearing from point 1 to point 2
bearing() {
local lat1=$1 lon1=$2 lat2=$3 lon2=$4
awk -v lat1="$lat1" -v lon1="$lon1" -v lat2="$lat2" -v lon2="$lon2" '
BEGIN {
pi = atan2(0, -1)
lat1_r = lat1 * pi / 180
lat2_r = lat2 * pi / 180
dlon = (lon2 - lon1) * pi / 180
x = sin(dlon) * cos(lat2_r)
y = cos(lat1_r) * sin(lat2_r) - sin(lat1_r) * cos(lat2_r) * cos(dlon)
bearing = atan2(x, y) * 180 / pi
# Normalize to 0-360
bearing = (bearing + 360) % 360
printf "%.1f", bearing
}'
}
bear=$(bearing $ny_lat $ny_lon $london_lat $london_lon)
echo "Bearing NY → London: ${bear}°"
Generating sine waves, combining waves, and calculating oscillation parameters.
#!/bin/bash
# Wave functions and oscillations
pi=$(echo "scale=10; 4*a(1)" | bc -l)
echo "=== Simple Sine Wave ==="
# y = A * sin(2π * f * t + φ)
amplitude=5
frequency=2
phase=0
echo "A=$amplitude, f=$frequency Hz, φ=$phase"
echo "Time Value"
for t in $(seq 0 0.1 1); do
y=$(echo "scale=4; $amplitude * s(2 * $pi * $frequency * $t + $phase)" | bc -l)
printf "%.1f %7.4f\n" $t $y
done
echo ""
echo "=== ASCII Wave Plot ==="
awk 'BEGIN {
pi = atan2(0, -1)
amplitude = 10
for (t = 0; t <= 2*pi; t += 0.2) {
y = amplitude * sin(t)
# Scale to 0-20 for display
pos = int(y + amplitude + 0.5)
printf "%6.2f |", t
for (i = 0; i <= 20; i++) {
if (i == 10) printf "|"
else if (i == pos) printf "*"
else printf " "
}
printf "\n"
}
}'
echo ""
echo "=== Wave Superposition ==="
# Two waves with different frequencies
awk 'BEGIN {
pi = atan2(0, -1)
print "Combining f=1 Hz and f=3 Hz:"
print " t y1 y2 sum"
for (t = 0; t <= 1; t += 0.1) {
y1 = sin(2 * pi * 1 * t)
y2 = 0.5 * sin(2 * pi * 3 * t)
sum = y1 + y2
printf "%.1f %6.3f %6.3f %6.3f\n", t, y1, y2, sum
}
}'
echo ""
echo "=== Damped Oscillation ==="
# y = A * e^(-bt) * sin(ωt)
awk 'BEGIN {
pi = atan2(0, -1)
A = 10
b = 0.5 # Damping factor
omega = 2*pi # Angular frequency
print "Damped oscillation (A=10, b=0.5):"
for (t = 0; t <= 5; t += 0.5) {
y = A * exp(-b * t) * sin(omega * t)
printf "t=%.1f: y=%7.3f\n", t, y
}
}'
A comprehensive bash math library with all trig and advanced functions.
#!/bin/bash
# Comprehensive math library
# Initialize constants
PI=$(echo "scale=15; 4*a(1)" | bc -l)
E=$(echo "scale=15; e(1)" | bc -l)
# ===== Trigonometric Functions =====
sin_deg() { echo "scale=${2:-6}; s($1 * $PI / 180)" | bc -l; }
cos_deg() { echo "scale=${2:-6}; c($1 * $PI / 180)" | bc -l; }
tan_deg() { echo "scale=${2:-6}; s($1*$PI/180)/c($1*$PI/180)" | bc -l; }
sin_rad() { echo "scale=${2:-6}; s($1)" | bc -l; }
cos_rad() { echo "scale=${2:-6}; c($1)" | bc -l; }
tan_rad() { echo "scale=${2:-6}; s($1)/c($1)" | bc -l; }
# ===== Inverse Trig =====
asin() { echo "scale=${2:-6}; a($1/sqrt(1-$1*$1))" | bc -l; }
acos() {
if (( $(echo "$1 >= 0" | bc -l) )); then
echo "scale=${2:-6}; a(sqrt(1-$1*$1)/$1)" | bc -l
else
echo "scale=${2:-6}; $PI + a(sqrt(1-$1*$1)/$1)" | bc -l
fi
}
atan() { echo "scale=${2:-6}; a($1)" | bc -l; }
# ===== Logarithms =====
ln() { echo "scale=${2:-6}; l($1)" | bc -l; }
log10() { echo "scale=${2:-6}; l($1)/l(10)" | bc -l; }
log2() { echo "scale=${2:-6}; l($1)/l(2)" | bc -l; }
logn() { echo "scale=${3:-6}; l($1)/l($2)" | bc -l; } # log base n
# ===== Exponential =====
exp() { echo "scale=${2:-6}; e($1)" | bc -l; }
pow() { echo "scale=${3:-6}; e($2 * l($1))" | bc -l; }
# ===== Hyperbolic =====
sinh() { echo "scale=${2:-6}; (e($1) - e(-$1)) / 2" | bc -l; }
cosh() { echo "scale=${2:-6}; (e($1) + e(-$1)) / 2" | bc -l; }
tanh() { echo "scale=${2:-6}; (e($1) - e(-$1)) / (e($1) + e(-$1))" | bc -l; }
# ===== Utility =====
sqrt() { echo "scale=${2:-6}; sqrt($1)" | bc -l; }
abs() { echo "scale=${2:-6}; if ($1 < 0) -($1) else $1" | bc -l; }
deg_to_rad() { echo "scale=${2:-10}; $1 * $PI / 180" | bc -l; }
rad_to_deg() { echo "scale=${2:-6}; $1 * 180 / $PI" | bc -l; }
# ===== Demo =====
echo "===== Math Library Demo ====="
echo "Constants:"
echo " π = $PI"
echo " e = $E"
echo ""
echo "Trigonometry (degrees):"
echo " sin(30°) = $(sin_deg 30)"
echo " cos(60°) = $(cos_deg 60)"
echo " tan(45°) = $(tan_deg 45)"
echo ""
echo "Inverse trig:"
echo " asin(0.5) = $(rad_to_deg $(asin 0.5))°"
echo " acos(0.5) = $(rad_to_deg $(acos 0.5))°"
echo " atan(1) = $(rad_to_deg $(atan 1))°"
echo ""
echo "Logarithms:"
echo " ln(e) = $(ln $E)"
echo " log₁₀(100) = $(log10 100)"
echo " log₂(1024) = $(log2 1024)"
echo ""
echo "Powers & Roots:"
echo " 2^10 = $(pow 2 10)"
echo " √2 = $(sqrt 2)"
echo " e^2 = $(exp 2)"
echo ""
echo "Hyperbolic:"
echo " sinh(1) = $(sinh 1)"
echo " cosh(1) = $(cosh 1)"
echo " tanh(1) = $(tanh 1)"
echo ""
echo "===== End Demo ====="
bc -l for trig (loads math library)awk for processing data with trigscale for precisionatan2(y,x) instead of atan(y/x)# Constants
pi=$(echo "scale=10; 4*a(1)" | bc -l)
e=$(echo "scale=10; e(1)" | bc -l)
# Degree/Radian conversion
rad=$(echo "scale=10; $deg * $pi / 180" | bc -l)
deg=$(echo "scale=10; $rad * 180 / $pi" | bc -l)
# Basic trig (radians)
sin=$(echo "scale=6; s($x)" | bc -l)
cos=$(echo "scale=6; c($x)" | bc -l)
tan=$(echo "scale=6; s($x)/c($x)" | bc -l)
# Inverse trig
atan=$(echo "scale=6; a($x)" | bc -l)
asin=$(echo "scale=6; a($x/sqrt(1-$x*$x))" | bc -l)
# Logarithms
ln=$(echo "scale=6; l($x)" | bc -l)
log10=$(echo "scale=6; l($x)/l(10)" | bc -l)
# Power
pow=$(echo "scale=6; e($y * l($x))" | bc -l)
# Distance
dist=$(echo "scale=6; sqrt(($x2-$x1)^2 + ($y2-$y1)^2)" | bc -l)
| Identity | Formula |
|---|---|
| Pythagorean | sin²(x) + cos²(x) = 1 |
| Double angle | sin(2x) = 2·sin(x)·cos(x) |
| Half angle | sin²(x/2) = (1 - cos(x))/2 |
| Sum | sin(a+b) = sin(a)cos(b) + cos(a)sin(b) |
| Hyperbolic | cosh²(x) - sinh²(x) = 1 |
| Euler's | e^(ix) = cos(x) + i·sin(x) |