This commit is contained in:
Mia T. Rain 2022-04-19 14:22:06 -04:00
parent b4b9f36448
commit fd8f2fe231
Signed by: Mia
GPG Key ID: 5F40D66F94DECFE8
2 changed files with 94 additions and 4 deletions

26
README
View File

@ -6,5 +6,29 @@ Usage
-- $1 = number
-- $2 = operator
-- $3 = number
-- eg: 1.89 ^~ 2
-- eg:
---- 1.89 ^~ 2
---- 1.5 >/-gt 1.04
---- 1 </-lt 3.14122
---- 1.0051 -ne 1
---- 1.231201 -eq 1.231201
---- ...
---
Operator support:
-- -eq -ne -gt -ge -lt -le -cl -fl
---- -eq/==/=: equal to
---- -ne/!==/=: not equal to
---- -gt/>: greator than
---- -ge/=>/>=: greator than or equal to
---- -lt/<: less than
---- -le/<=/=<: less than or equal to
---- -cl/^~/~^/^=: equal after ceiling
---- -fl/~/≈/≅: equal after floor
---
Caveats
-- note that while no math is used here
-- sh still has a maximum of 9223372036854775807
-- if either whole or decimal is above this
-- the number will overflow when interacted with by $(())
-- ( to -922337203685477580... )
---

72
com
View File

@ -27,12 +27,78 @@ floor() {
} || return 1
} # floor
gt() {
:
}
w1="${1%.*}"; w2="${2%.*}"
d1="${1#*.}"; d2="${2#*.}"
[ "$w1" -gt "$w2" ] && exit 0 # if whole number is greator no need to compare decimals
# now compare $d1 to $d2
# 0.10 > 0.01
# 100 > 1
until [ "${d1#0}" = "${d1}" ]; do
d1="${d1#0}"; d2="${d2}0"
done
until [ "${d2#0}" = "${d2}" ]; do
d2="${d2#0}"; d1="${d1}0"
done
[ "$3" ] && {
[ "$d1" -gt "$d2" -o "$d1" -eq "$d2" -a "$w1" -eq "$w2" ] && {
exit 0
} || return 1
} || {
[ "$d1" -gt "$d2" ] && {
exit 0
} || return 1
} # or equal to (-ge)
} # test if $1 is > $2
lt() {
w1="${1%.*}"; w2="${2%.*}"
d1="${1#*.}"; d2="${2#*.}"
[ "$w1" -lt "$w2" ] && exit 0
until [ "${d1#0}" = "${d1}" ]; do
d1="${d1#0}"; d2="${d2}0"
done
until [ "${d2#0}" = "${d2}" ]; do
d2="${d2#0}"; d1="${d1}0"
done
[ "$3" ] && {
[ "$d1" -lt "$d2" -o "$d1" -eq "$d2" -a "$w1" -eq "$w2" ] && {
exit 0
} || return 1
} || {
[ "$d1" -lt "$d2" ] && {
exit 0
} || return 1
}
} # test if $1 is < $2
eq() { # use $3 for -ne
w1="${1%.*}"; w2="${2%.*}"
d1="${1#*.}"; d2="${2#*.}"
until [ "${d1#0}" = "${d1}" ]; do
d1="${d1#0}"; d2="${d2}0"
done
until [ "${d2#0}" = "${d2}" ]; do
d2="${d2#0}"; d1="${d1}0"
done
[ "$3" ] && {
[ "$w1" -ne "$w2" -a "$d1" -ne "$d2" ] && {
exit 0
} || return 1
} || {
[ "$w1" -eq "$w2" -a "$d1" -eq "$d2" ] && {
exit 0
} || return 1
}
}
n1="$1"; op="$2"; n2="$3"
case "$op" in
'^~'|'~^'|'^='|'-cl') ceil "$n1" "$n2" || exit 1 ;;
'~'|'≈'|'≅'|'-fl') floor "$n1" "$n2" || exit 1 ;;
# '>'|'-gt') gt "$n1" "$n2" || exit 1 # wip
'>'|'-gt') gt "$n1" "$n2" || exit 1 ;;
'<'|'-lt') lt "$n1" "$n2" || exit 1 ;;
'-ge'|'>='|'=>') gt "$n1" "$n2" "-" || exit 1 ;;
'-le'|'<='|'=<') lt "$n1" "$n2" "-" || exit 1 ;;
# or equal to is set via $3
'-eq'|'=='|'=') eq "$n1" "$n2" || exit 1 ;;
'-ne'|'!=='|'!=') eq "$n1" "$n2" "-" || exit 1
# -ne uses $3 with -eq
esac
exit 0 # exit 0 if done