new math functions

This commit is contained in:
olli 2023-10-26 17:00:41 +02:00
parent 075ec6f674
commit 6088d3f1a4
3 changed files with 114 additions and 0 deletions

View File

@ -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}
}

View File

@ -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
}

View File

@ -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
}