From 6088d3f1a4541c1ac7058d85b4fddfd82f7d7c8e Mon Sep 17 00:00:00 2001 From: olli Date: Thu, 26 Oct 2023 17:00:41 +0200 Subject: [PATCH] new math functions --- dabo/functions/g_num_is_approx.sh | 23 +++++++++++++ dabo/functions/g_num_is_between.sh | 54 ++++++++++++++++++++++++++++++ dabo/functions/g_num_is_higher.sh | 37 ++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 dabo/functions/g_num_is_approx.sh create mode 100644 dabo/functions/g_num_is_between.sh create mode 100644 dabo/functions/g_num_is_higher.sh diff --git a/dabo/functions/g_num_is_approx.sh b/dabo/functions/g_num_is_approx.sh new file mode 100644 index 0000000..7e842b7 --- /dev/null +++ b/dabo/functions/g_num_is_approx.sh @@ -0,0 +1,23 @@ +function g_num_is_approx { + + # check if $1 is in percentage range ($3 and $4) to $2 + local f_num=$1 + local f_base=$2 + local f_percentage_up=$3 + local f_percentage_down=$4 + + # Check for valid decimal number + for f_numtest in "${f_num}" "${f_base}" "${f_percentage_up}" "${f_percentage_down}" + do + if ! [[ ${f_numtest} =~ ^(-)?[0-9]+(\.[0-9]+)?$ ]] + then + g_echo "\"${f_numtest}\"Not a valid number" + return 1 + fi + done + + local f_from=$(echo "${f_base} - (${f_base} / 100 * ${f_percentage_down})" | bc -l) + local f_to=$(echo "${f_base} + (${f_base} / 100 * ${f_percentage_up})" | bc -l) + + g_num_is_between ${f_num} ${f_from} ${f_to} +} diff --git a/dabo/functions/g_num_is_between.sh b/dabo/functions/g_num_is_between.sh new file mode 100644 index 0000000..18df763 --- /dev/null +++ b/dabo/functions/g_num_is_between.sh @@ -0,0 +1,54 @@ +function g_num_is_between { + + local f_num=$1 + local f_between1=$2 + local f_between2=$3 + + # Check for integer (can be done with bash itself) + if [[ ${f_num} =~ ^[0-9]+$ ]] && [[ ${f_between1} =~ ^[0-9]+$ ]] && [[ ${f_between2} =~ ^[0-9]+$ ]] + then + # Check which is the low (from) and the high (to) number + if [ "${f_between1}" -lt "${f_between2}" ] + then + local f_from=${f_between1} + local f_to=${f_between2} + else + local f_from=${f_between2} + local f_to=${f_between1} + fi + # Check if given number is in or out range + if [ ${f_num} -lt ${f_from} ] || [ ${f_num} -gt ${f_to} ] + then + return 1 + else + return 0 + fi + fi + + # Check for valid decimal number + for f_numtest in "$f_num" "$f_between1" "$f_between2" + do + if ! [[ ${f_numtest} =~ ^(-)?[0-9]+(\.[0-9]+)?$ ]] + then + g_echo "\"${f_numtest}\"Not a valid number" + return 1 + fi + done + + # Check which is the low (from) and the high (to) number + if [ $(echo "${f_between1} < ${f_between2}" | bc -l) -ne 0 ] + then + local f_from=${f_between1} + local f_to=${f_between2} + else + local f_from=${f_between2} + local f_to=${f_between1} + fi + # Check if given number is in or out range + if [ $(echo "${f_num} < ${f_from}" | bc -l) -ne 0 ] || [ $(echo "${f_num} > ${f_to}" | bc -l) -ne 0 ] + then + return 1 + else + return 0 + fi +} diff --git a/dabo/functions/g_num_is_higher.sh b/dabo/functions/g_num_is_higher.sh new file mode 100644 index 0000000..53c4816 --- /dev/null +++ b/dabo/functions/g_num_is_higher.sh @@ -0,0 +1,37 @@ +function g_num_is_higher { + + local f_num=$1 + local f_checkhigher=$2 + + # Check for integer (can be done with bash itself) + if [[ ${f_num} =~ ^[0-9]+$ ]] && [[ ${f_checkhigher} =~ ^[0-9]+$ ]] + then + # Check which is the low (from) and the high (to) number + if [ "${f_num}" -gt "${f_checkhigher}" ] + then + return 0 + else + return 1 + fi + fi + + # Check for valid decimal number + for f_numtest in "$f_num" "$f_checkhigher" + do + if ! [[ ${f_numtest} =~ ^(-)?[0-9]+(\.[0-9]+)?$ ]] + then + g_echo "\"${f_numtest}\"Not a valid number" + return 1 + fi + done + + # Check which is the low (from) and the high (to) number + # Check if given number is in or out range + if [ $(echo "${f_num} > ${f_checkhigher}" | bc) -ne 0 ] + then + return 0 + else + return 1 + fi +} +