mirror of https://dicksdeathabove.xyz/~mia/psh-fractional synced 2024-11-10 00:04:35 +00:00

greatly refine div

This commit is contained in:
Mia T. Rain 2022-02-24 11:57:55 -05:00
parent b6fb49a0b4
commit 819aa72f79
Signed by: Mia
GPG key ID: 5F40D66F94DECFE8

View file

@ -23,6 +23,7 @@ countd() { # count decimal places
echo "$n" echo "$n"
} }
ld() { # $1/$2 ld() { # $1/$2
if [ ! -z "${1##*.*}" -a -z "${2##*.*}" ]; then if [ ! -z "${1##*.*}" -a -z "${2##*.*}" ]; then
di="${1}."; until [ "${#di}" -eq "$((${#2}+1))" ]; do di="${1}."; until [ "${#di}" -eq "$((${#2}+1))" ]; do
di="${di}0" di="${di}0"
@ -34,58 +35,48 @@ ld() { # $1/$2
done done
set -- "$1" "$div" "$3" set -- "$1" "$div" "$3"
fi fi
[ -z "${1##*.*}" -o -z "${2##*.*}" ] || { [ -z "${1##*.*}" -o -z "${2##*.*}" ] || {
set -- "$1.00" "$2.00" "$3" set -- "$1.00" "$2.00" "$3"
} }
di="${1##*.}"; div="${2##*.}" di="${1##*.}"; div="${2##*.}"
until [ "${#di}" -ge 2 ]; do until [ "${#di}" -ge "${#div}" ]; do
di="${di}0" di="${di}0"
done done
until [ "${#div}" -ge 2 ]; do until [ "${#div}" -ge "${#di}" ]; do
div="${div}0" div="${div}0"
done done
set -- "${1%%.*}.$di" "${2%%.*}.$div" "$3" set -- "${1%%.*}.$di" "${2%%.*}.$div" "$3"
# handle decimal less numbers # handle decimal less numbers
#echo "$1/$2" #echo "$1/$2"
di="$1"; div="$2"; count="$3" di="$1"; div="$2"; count="$3"
case "$div" in case "$div" in
*'.'*) *'.'*) # if $2 has a decimal we must shift
sp=0; sdiv="${div%%.*}"; IFS="."; set -- ${div}; div="${1}${2}"; unset IFS set -- "${1%.*}${1#*.}" "${2%.*}${2#*.}" "$3" # shift $2 # simply remove the decimal point
sp="$((${#div}-${#sdiv}))" until [ "${#1}" -ge "${#2}" ]; do
esac set -- "${1}0" "$2" "$3"
# determine where the decimal point is -- if present
case "$di" in
*'.'*)
dp=1; t="$di"; while :; do # use while true + case/break
t="${t#?}"; : $((dp+=1))
case "$t" in
'.'*) break
esac
done done
;; set -- "$1.00" "$2" "$3" # add decimals back in for $1 only
*) dp=0
esac esac
[ "$dp" -gt 0 ] && { #echo "$1 $2"
IFS="."; set -- ${di}; di="${1}${2}"; unset IFS di="$1"; div="$2"; count="$3" # redeclare
}
#echo "$sp $div ${di}/${#di} | $1/$2 | $di/$div" #echo "$sp $div ${di}/${#di} | $1/$2 | $di/$div"
[ "$sp" ] && { o="$((${di%.*}/${div%.*}))"; [ "${#o}" -eq 0 ] && : $((count+=2)); : $((count+=${#o}))
ss="${#di}"; : $((ss-=dp)); : $((ss+=sp)); : $((ss+=1)) sd="${di%.*}"; : $((count+=${#sd}));
until [ "${#div}" -eq "$ss" ]; do until [ "${#di}" -eq "$((count+1))" ]; do
div="${div}0"
done
}
#echo "-"
o="$((di/div))"; dp="${#o}"
[ "${#o}" -eq 0 ] && : $((count+=2))
: $((count+=${#o}))
until [ "${#di}" -eq "$count" ]; do
di="${di}0" di="${di}0"
done done
set -- "$1" "$2" "${3:-9}" "$di" "$sd" : $((count-=1)) && di="${di%0}"
set -- "$1" "$2" "${3:-9}" "$di"
unset IFS o; n=0; for i in $(s 1 $count); do unset IFS o; n=0; for i in $(s 1 $count); do
fd="$(fd $di)" # fd is needed later fd="$(fd $di)" # fd is needed later
#echo "$fd"; [ "$fd" = '.' ] && {
dp="$n"
di="${di#?}"; fd="$(fd $di)"
}
[ "$i" -gt 1 ] && { [ "$i" -gt 1 ] && {
fd="${sd}${fd}" fd="${sd}${fd}"
#echo "$fd" #echo "$fd"
@ -94,72 +85,31 @@ ld() { # $1/$2
o="${o}${sd}"; sd="$((sd*div))" o="${o}${sd}"; sd="$((sd*div))"
#printf " %s\n- %s\n" "$fd" "$sd" #printf " %s\n- %s\n" "$fd" "$sd"
sd="$((fd-sd))" sd="$((fd-sd))"
#echo "| $sd" #echo "| $sd -- $i"
[ "$sd" -eq 0 ] && {
n=0
break
}
# now pull down # redefine $di
di="${di#?}" # $fd$sd di="${di#?}" # $fd$sd
: $((n+=1)) : $((n+=1))
done done
[ "$n" -gt 0 ] && { n=0; sd="$o"; while [ "$sd" ]; do
count=$((count+n)) next="${sd#?}"
} || count=0 [ "$n" -eq "$dp" ] && {
#echo "$fd $di/$4 - $div / $dp -- $sp | $count" o="${o%$sd}.$sd"
# do again if $((count-n)) is > 0 break
[ "$count" -gt 0 ] && {
di="$4"; sd="$5"; o=""; until [ "${#di}" -eq "$count" ]; do
di="${di}0"
done
for i in $(s 1 $count); do
fd="$(fd $di)" # fd is needed later
[ "$i" -gt 1 ] && fd="${sd}${fd}"
sd="$((fd/div))"; o="${o}${sd}"; sd="$((sd*div))"; sd="$((fd-sd))"
[ "$sd" -eq 0 ] && break
di="${di#?}"
done
} }
sd="$next"
: $((n+=1))
done
until [ "${o#0}" = "$o" ]; do until [ "${o#0}" = "$o" ]; do
o="${o#0}" o="${o#0}"
done done
[ "$dp" -gt 0 -a "$fd" -ne "$1" ] && { #echo "$o"
[ "$t" ] || : $((dp+=1))
[ "$t" ] && : $((dp-=1))
#[ "$sp" ] && : $((dp+=1))
[ "${sp:-0}" -gt 1 ] && : $((dp+=1))
#echo "$o $dp/$sp | ${#o}"
do="$o"; [ "${#do}" -gt "$dp" ] && until [ "${#do}" -eq "$dp" ]; do
do="${do%?}"
done
o="${do:-0}.${o#$do}"
}
[ "$1" -lt "$2" ] || until [ "${o#0}" = "$o" ]; do
o="${o#0}"
done
[ "$dp" -eq 0 ] && dp="$sp"
fd="$o"; until [ "${#fd}" -eq 1 ]; do
fd="${fd%?}"
done
[ "$fd" -ne "$(($1/${div%$2}))" ] && if [ "$fd" -ne "$(($1/${div%$2} +1))" -a "$fd" -ne "$(($1/${div%$2} -1))" ]; then
o="${o%.*}${o#*.}"; o="$(($1/${div%$2})).$o"
fi
if [ ! -z "${o##*.*}" -a "$dp" -gt 0 ]; then # missing decimal place if [ ! -z "${o##*.*}" -a "$dp" -gt 0 ]; then # missing decimal place
do="$o"; [ "${#do}" -gt "$dp" ] && until [ "${#do}" -eq "$dp" ]; do do="$o"; [ "${#do}" -gt "$dp" ] && until [ "${#do}" -eq "$dp" ]; do
do="${do%?}" do="${do%?}"
done done
o="${do:-0}.${o#$do}" o="${do:-0}.${o#$do}"
else # confirm that decimal is in right place
#cd="$(countd $o)"
cd="${o%%.*}"; [ "${#cd}" -ne "$dp" ] && {
[ -z "${o##*.*}" ] && o="${o%.*}${o#*.}"
do="$o"; [ "${#do}" -gt "$dp" ] && until [ "${#do}" -eq "$dp" ]; do
do="${do%?}"
done
o="${do:-0}.${o#$do}"
}
fi fi
echo "$o" #| $1/$2 | $dp/$sp -- $t | $do || $fd $1" [ "$(fd $o)" = '.' ] && o="0$o"
echo "$o" # | $1/$2 | $dp | $do || $fd $1"
} }
# 9 decimal points unless $3 is passed # 9 decimal points unless $3 is passed
[ "$1" -a "$2" ] && ld "$1" "$2" "${3:-9}" [ "$1" -a "$2" ] && ld "$1" "$2" "${3:-9}"