Something went wrong. Try again.
extentions for bash language
Something went wrong. Try again.
8.8 kB · 548 lines
Shell
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548#!/bin/bash
# Basic Math Module# Provides arithmetic operations and mathematical utilities
module_version math.basic 1.0.0
# Basic arithmetic operationsadd() { local a="$1" local b="$2" echo "$((a + b))"}
sub() { local a="$1" local b="$2" echo "$((a - b))"}
subtract() { sub "$@"}
mul() { local a="$1" local b="$2" echo "$((a * b))"}
multiply() { mul "$@"}
div() { local a="$1" local b="$2" if [[ "$b" -eq 0 ]]; then error "Division by zero" return ${EXIT_FAILURE:-1} fi echo "$((a / b))"}
divide() { div "$@"}
mod() { local a="$1" local b="$2" if [[ "$b" -eq 0 ]]; then error "Modulo by zero" return ${EXIT_FAILURE:-1} fi echo "$((a % b))"}
pow() { local base="$1" local exp="$2" local result=1 if [[ "$exp" -lt 0 ]]; then error "Negative exponent not supported" return ${EXIT_FAILURE:-1} fi for ((i=0; i<exp; i++)); do result=$((result * base)) done echo "$result"}
power() { pow "$@"}
# Comparison functionseq() { local a="$1" local b="$2" [[ "$a" -eq "$b" ]]}
equal() { eq "$@"}
ne() { local a="$1" local b="$2" [[ "$a" -ne "$b" ]]}
not_equal() { ne "$@"}
lt() { local a="$1" local b="$2" [[ "$a" -lt "$b" ]]}
less() { lt "$@"}
le() { local a="$1" local b="$2" [[ "$a" -le "$b" ]]}
less_equal() { le "$@"}
gt() { local a="$1" local b="$2" [[ "$a" -gt "$b" ]]}
greater() { gt "$@"}
ge() { local a="$1" local b="$2" [[ "$a" -ge "$b" ]]}
greater_equal() { ge "$@"}
# Min/Max functionsmin() { local min_val="$1" shift for val in "$@"; do if [[ "$val" -lt "$min_val" ]]; then min_val="$val" fi done echo "$min_val"}
max() { local max_val="$1" shift for val in "$@"; do if [[ "$val" -gt "$max_val" ]]; then max_val="$val" fi done echo "$max_val"}
# Find minimum in arraymin_array() { local min_val="$1" shift for val in "$@"; do if [[ "$val" -lt "$min_val" ]]; then min_val="$val" fi done echo "$min_val"}
# Find maximum in arraymax_array() { local max_val="$1" shift for val in "$@"; do if [[ "$val" -gt "$max_val" ]]; then max_val="$val" fi done echo "$max_val"}
# Sum of numberssum() { local total=0 for num in "$@"; do total=$((total + num)) done echo "$total"}
# Average of numbersavg() { local count=$# local total=$(sum "$@") if [[ "$count" -eq 0 ]]; then error "No numbers provided" return ${EXIT_FAILURE:-1} fi echo "$((total / count))"}
# Absolute valueabs() { local num="$1" if [[ "$num" -lt 0 ]]; then echo "$((0 - num))" else echo "$num" fi}
# Factorialfact() { local n="$1" local result=1 if [[ "$n" -lt 0 ]]; then error "Factorial of negative number" return ${EXIT_FAILURE:-1} fi for ((i=1; i<=n; i++)); do result=$((result * i)) done echo "$result"}
# Greatest Common Divisor (Euclidean algorithm)gcd() { local a="$1" local b="$2" local temp while [[ "$b" -ne 0 ]]; do temp="$b" b=$((a % b)) a="$temp" done echo "$a"}
# Least Common Multiplelcm() { local a="$1" local b="$2" if [[ "$a" -eq 0 || "$b" -eq 0 ]]; then echo "0" return fi local result=$((a * b / $(gcd "$a" "$b"))) echo "$result"}
# Check if number is evenis_even() { local num="$1" [[ $((num % 2)) -eq 0 ]]}
# Check if number is oddis_odd() { local num="$1" [[ $((num % 2)) -ne 0 ]]}
# Check if number is primeis_prime() { local num="$1" if [[ "$num" -le 1 ]]; then return 1 fi if [[ "$num" -le 3 ]]; then return 0 fi if [[ $((num % 2)) -eq 0 || $((num % 3)) -eq 0 ]]; then return 1 fi local i=5 while [[ $((i * i)) -le "$num" ]]; do if [[ $((num % i)) -eq 0 || $((num % (i + 2))) -eq 0 ]]; then return 1 fi i=$((i + 6)) done return 0}
# Generate Fibonacci sequence up to n termsfib() { local n="$1" local a=0 local b=1 if [[ "$n" -le 0 ]]; then return fi for ((i=0; i<n; i++)); do echo "$a" local temp="$a" a="$b" b=$((temp + b)) done}
# Get nth Fibonacci numberfib_n() { local n="$1" if [[ "$n" -le 0 ]]; then echo "0" return fi local a=0 local b=1 for ((i=1; i<n; i++)); do local temp="$a" a="$b" b=$((temp + b)) done echo "$a"}
# Square root (integer approximation)sqrt_int() { local num="$1" local low=0 local high="$num" local mid local result=0 if [[ "$num" -lt 0 ]]; then error "Square root of negative number" return ${EXIT_FAILURE:-1} fi while [[ "$low" -le "$high" ]]; do mid=$(((low + high) / 2)) local mid_sq=$((mid * mid)) if [[ "$mid_sq" -eq "$num" ]]; then echo "$mid" return elif [[ "$mid_sq" -lt "$num" ]]; then low=$((mid + 1)) result="$mid" else high=$((mid - 1)) fi done echo "$result"}
# Check if number is in rangein_range() { local num="$1" local min="$2" local max="$3" [[ "$num" -ge "$min" && "$num" -le "$max" ]]}
# Clamp number to rangeclamp() { local num="$1" local min="$2" local max="$3" if [[ "$num" -lt "$min" ]]; then echo "$min" elif [[ "$num" -gt "$max" ]]; then echo "$max" else echo "$num" fi}
# Number base conversiondec_to_bin() { local num="$1" local result="" local remainder if [[ "$num" -eq 0 ]]; then echo "0" return fi while [[ "$num" -gt 0 ]]; do remainder=$((num % 2)) result="${remainder}${result}" num=$((num / 2)) done echo "$result"}
dec_to_hex() { local num="$1" local result="" local remainder local hex_chars="0123456789ABCDEF" if [[ "$num" -eq 0 ]]; then echo "0" return fi while [[ "$num" -gt 0 ]]; do remainder=$((num % 16)) result="${hex_chars:$remainder:1}${result}" num=$((num / 16)) done echo "$result"}
# Parse number from string (validation)is_number() { local str="$1" [[ "$str" =~ ^-?[0-9]+$ ]]}
# Safe arithmetic with validationsafe_add() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Invalid numbers" return ${EXIT_FAILURE:-1} fi add "$a" "$b"}
safe_sub() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Invalid numbers" return ${EXIT_FAILURE:-1} fi sub "$a" "$b"}
safe_mul() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Invalid numbers" return ${EXIT_FAILURE:-1} fi mul "$a" "$b"}
safe_div() { local a="$1" local b="$2" if ! is_number "$a" || ! is_number "$b"; then error "Invalid numbers" return ${EXIT_FAILURE:-1} fi div "$a" "$b"}
# Random number utilitiesrandom_int() { local min="${1:-0}" local max="${2:-32767}" if [[ "$min" -gt "$max" ]]; then local temp="$min" min="$max" max="$temp" fi local range=$((max - min + 1)) echo $((min + RANDOM % range))}
random_float() { local min="${1:-0}" local max="${2:-1}" if command -v awk > /dev/null; then awk "BEGIN{srand(); print $min + ($max - $min) * rand()}" else # Fallback to integer random_int "$min" "$max" fi}
# Export module functionsexport_module math.basic \ add sub subtract mul multiply div divide mod pow power \ eq equal ne not_equal lt less le less_equal gt greater ge greater_equal \ min max min_array max_array sum avg \ abs fact gcd lcm \ is_even is_odd is_prime \ fib fib_n sqrt_int \ in_range clamp \ dec_to_bin dec_to_hex \ is_number \ safe_add safe_sub safe_mul safe_div \ random_int random_float