#!/bin/bash # https://stackoverflow.com/a/246128 SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) . $SCRIPT_DIR/utils.sh # https://unix.stackexchange.com/questions/66766/float-operation-with-bc # https://unix.stackexchange.com/questions/89712/how-to-convert-floating-point-number-to-integer # xargs: https://stackoverflow.com/questions/19148370/piping-seq-to-printf-for-number-formatting divideint () { if [ -z $3 ]; then local scale=2 else local scale=$3 fi bc <<< "scale=$scale; $1/$2" | xargs printf "%.0f\n" } multiint () { bc <<< "scale=2; $1*$2" | xargs printf "%.0f\n" } divide () { if [ -z $3 ]; then local scale=2 else local scale=$3 fi bc <<< "scale=$scale; $1/$2" } multiply () { if [ -z $3 ]; then local scale=2 else local scale=$3 fi bc <<< "scale=$scale; $1*$2" } avgoftwo () { local sum=$(bc <<< "$1+$2") divide $sum 2 } max () { declare -a allnumbers for arg in "$@"; do if isarray $arg; then for elem in ${arg[@]}; do [[ $elem -eq $elem ]] && allnumbers+=($elem) done else [[ $arg -eq $arg ]] && allnumbers+=($arg) fi done max=${allnumbers[0]} for n in "${allnumbers[@]}"; do ((n > max)) && max=$n done echo $max } min () { declare -a allnumbers for arg in "$@"; do if isarray $arg; then for elem in ${arg[@]}; do [[ $elem -eq $elem ]] && allnumbers+=($elem) done else [[ $arg -eq $arg ]] && allnumbers+=($arg) fi done min=${allnumbers[0]} for n in "${allnumbers[@]}"; do ((n < min)) && min=$n done echo $min }