mirror of https://dicksdeathabove.xyz/~mia/psh-fractional synced 2024-09-19 16:40:44 +00:00

basically done

This commit is contained in:
Mia T. Rain 2022-04-05 12:24:44 -04:00
parent f9f0623956
commit 3e683a9f73
Signed by: Mia
GPG key ID: 5F40D66F94DECFE8
5 changed files with 50 additions and 38 deletions

14
README
View file

@ -11,13 +11,13 @@ Dev Status
--- ---
Current Status Current Status
-- addition 100% working -- addition 100% working
-- subtraction ~80% working -- subtraction ~90% working
---- has major issues with decimal placement ---- decimal placement working now
---- I have no fix for this other than flat out long subtraction ---- some numbers produce the wrong results... no idea why
-- multiplication ~50% working -- multiplication 100% working
---- has a lot of issues I need to look into -- division ~90% working
-- division ~80% working ---- may sometimes produce to many decimals
---- issues with decimal placement ---- rarely places decimals in wrong places
--- ---
Tester Tester
-- a tester is present at ./tester; it is ofc not pure sh as it compares with bc -- a tester is present at ./tester; it is ofc not pure sh as it compares with bc

View file

@ -116,4 +116,4 @@ ld() { # $1/$2
echo "$o" # | $1/$2 | $dp | $do || $fd $1" echo "$o" # | $1/$2 | $dp | $do || $fd $1"
} }
# 19 decimal points unless $3 is passed # bc uses 19 # 19 decimal points unless $3 is passed # bc uses 19
[ "$1" -a "$2" ] && ld "$1" "$2" "${3:-19}" [ "$1" -a "$2" ] && ld "$1" "$2" "${3:-20}"

View file

@ -19,7 +19,7 @@ ad(){ # add decimal to $1 based on $2
e="${m#$next}${e}" e="${m#$next}${e}"
m="$next"; : $((n+=1)) m="$next"; : $((n+=1))
done done
w="${1%$e}"; echo "${w:-0}.$e" w="${1%$e}"; echo "${w:-0}.${e%0}"
} }
[ -z "${1##*.*}" -o -z "${2##*.*}" ] || { # if not floats [ -z "${1##*.*}" -o -z "${2##*.*}" ] || { # if not floats
echo "$(($1*$2))" echo "$(($1*$2))"
@ -27,16 +27,8 @@ ad(){ # add decimal to $1 based on $2
} }
if [ ! -z "${1##*.*}" ]; then if [ ! -z "${1##*.*}" ]; then
set -- "${1}.0" "$2"; d="${1#*.}"; d2="${2#*.}" set -- "${1}.0" "$2"; d="${1#*.}"; d2="${2#*.}"
until [ "${#d}" -eq "${#d2}" ]; do
set -- "${1}0" "$2"
d="${1#*.}"
done
elif [ ! -z "${2##*.*}" ]; then elif [ ! -z "${2##*.*}" ]; then
set -- "$1" "${2}.0"; d="${2#*.}"; d2="${1#*.}" set -- "$1" "${2}.0"; d="${2#*.}"; d2="${1#*.}"
until [ "${#d}" -eq "${#d2}" ]; do
set -- "$1" "${2}0"
d="${2#*.}"
done
fi # if $1/$2 is not a float; convert to float fi # if $1/$2 is not a float; convert to float
dn="$(countd $2 $(countd $1))"; m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}" dn="$(countd $2 $(countd $1))"; m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}"
until [ "${m1#0}" = "$m1" ]; do until [ "${m1#0}" = "$m1" ]; do
@ -45,6 +37,8 @@ done
until [ "${m2#0}" = "$m2" ]; do until [ "${m2#0}" = "$m2" ]; do
m2="${m2#0}" m2="${m2#0}"
done # dash has issues when numbers have leading 0's done # dash has issues when numbers have leading 0's
# zsh,bash,etc don't need this at all # zsh,bash,etc don't need this at all
# and really dash shouldn't either but /shrug # and really dash shouldn't either but /shrug
#printf '%s\n%s\n' "${m1}*${m2}" "9223372036854775807"
ad "$((m1*m2))" "$dn" ad "$((m1*m2))" "$dn"

View file

@ -36,15 +36,11 @@ elif [ ! -z "${2##*.*}" ]; then
d="${2#*.}" d="${2#*.}"
done done
fi # if $1/$2 is not a float; convert to float fi # if $1/$2 is not a float; convert to float
m1="${1%.*}"; m2="${2%.*}"; if [ "${#m2}" -gt "${#m1}" ]; then
dn="${#m2}"
elif [ "${#m2}" -eq "${#m1}" ]; then
dn="${#m1}"
else
dn="${#m1}"; : $((dn+=1))
fi
## SWAP IF $1 > $2; negative handling
m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}" m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}"
dn=$((${1%.*}-${2%.*})); dn="${dn#-}"; dn="${#dn}"
# decimal place is determined by the length of $1-$2
# without decimals
## SWAP IF $1 > $2; negative handling
[ "$m1" -lt "$m2" ] && { [ "$m1" -lt "$m2" ] && {
set -- "$2" "$1" set -- "$2" "$1"
m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}"; nr="-" m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}"; nr="-"

48
tester
View file

@ -13,32 +13,54 @@ fd() {
: $((n+=1)); m="$next" : $((n+=1)); m="$next"
done done
} }
for i in addition/add subtraction/sub multiplication/mul division/div; do ran() {
ran="$(${RPATH})"
ran="${ran#0.}"
until [ "${#ran}" -le 5 ]; do
ran="${ran%?}"
done
echo "${ran}"
}
case "$1" in
"") types="addition/add subtraction/sub multiplication/mul division/div" ;;
*) types="$1"
esac
IFS=" "; for i in ${types}; do
for n in 1 2; do for n in 1 2; do
ran="$(fd $(${RPATH}))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do ran="$(fd $(ran))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do
ran="$(fd $(${RPATH}))"; : $((ran-=1)) ran="$(fd $(ran))"; : $((ran-=1))
done done
case $ran in case $ran in
0) ran=""; tn="$(${RPATH}).$(${RPATH}) $(${RPATH})";; 0) ran=""; tn="$(ran).$(ran) $(ran)";;
1) ran=""; tn="$(${RPATH}) $(${RPATH}).$(${RPATH})" 1) ran=""; tn="$(ran) $(ran).$(ran)"
ran="$(fd $(${RPATH}))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do ran="$(fd $(ran))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do
ran="$(fd $(${RPATH}))"; : $((ran-=1)) ran="$(fd $(ran))"; : $((ran-=1))
done done
case $ran in case $ran in
1) ran=""; tn="${tn% *}.$(${RPATH}) ${tn#* }" 1) ran=""; tn="${tn% *}.$(ran) ${tn#* }"
esac esac
esac esac
# use random number to determine if number will have decimal place # use random number to determine if number will have decimal place
case $i in case $i in
addition/add) op="+";; *addition/add) op="+";;
subtraction/sub) op="-";; *subtraction/sub) op="-";;
multiplication/mul) op="*";; *multiplication/mul) op="*";;
division/div) op="/" *division/div) op="/"
esac esac
aws="$(echo ${tn% *}${op}${tn#* } | bc -l)"; case "$(fd $aws)" in aws="$(echo ${tn% *}${op}${tn#* } | bc -l)"; case "$(fd $aws)" in
'.') aws="0${aws}" '.') aws="0${aws}"
esac esac
printf ' %s\n%s %s\n_____\nbc: %s\n' "${tn% *}" "$op" "${tn#* }" "$aws" printf ' %s\n%s %s\n_____\nbc: %s\n' "${tn% *}" "$op" "${tn#* }" "$aws"
printf 'psh: %s\n\n' "$(${i} ${tn% *} ${tn#* })" oaws="$aws"; aws="${aws%.*}${aws#*.}"
[ "$aws" -ge 9223372036854775807 ] 2>/dev/null && {
printf 'psh: too large...\n\n'
} || printf 'psh: %s\n' "$(${i} ${tn% *} ${tn#* })"
[ "$oaws" = "$(${i} ${tn% *} ${tn#* })" ] && {
status=0
} || status=1
case "$status" in
0) printf 'Correct!\n\n' ;;
1) printf 'False..\n\n'
esac
done done
done done