diff --git a/README b/README index d8a2dc2..a7adfdd 100644 --- a/README +++ b/README @@ -11,13 +11,13 @@ Dev Status --- Current Status -- addition 100% working --- subtraction ~80% working ----- has major issues with decimal placement ----- I have no fix for this other than flat out long subtraction --- multiplication ~50% working ----- has a lot of issues I need to look into --- division ~80% working ----- issues with decimal placement +-- subtraction ~90% working +---- decimal placement working now +---- some numbers produce the wrong results... no idea why +-- multiplication 100% working +-- division ~90% working +---- may sometimes produce to many decimals +---- rarely places decimals in wrong places --- Tester -- a tester is present at ./tester; it is ofc not pure sh as it compares with bc diff --git a/division/div b/division/div index 7b6dc1e..edc184a 100755 --- a/division/div +++ b/division/div @@ -116,4 +116,4 @@ ld() { # $1/$2 echo "$o" # | $1/$2 | $dp | $do || $fd $1" } # 19 decimal points unless $3 is passed # bc uses 19 -[ "$1" -a "$2" ] && ld "$1" "$2" "${3:-19}" \ No newline at end of file +[ "$1" -a "$2" ] && ld "$1" "$2" "${3:-20}" \ No newline at end of file diff --git a/multiplication/mul b/multiplication/mul index 3beb2cc..ed85405 100755 --- a/multiplication/mul +++ b/multiplication/mul @@ -19,7 +19,7 @@ ad(){ # add decimal to $1 based on $2 e="${m#$next}${e}" m="$next"; : $((n+=1)) done - w="${1%$e}"; echo "${w:-0}.$e" + w="${1%$e}"; echo "${w:-0}.${e%0}" } [ -z "${1##*.*}" -o -z "${2##*.*}" ] || { # if not floats echo "$(($1*$2))" @@ -27,16 +27,8 @@ ad(){ # add decimal to $1 based on $2 } if [ ! -z "${1##*.*}" ]; then set -- "${1}.0" "$2"; d="${1#*.}"; d2="${2#*.}" - until [ "${#d}" -eq "${#d2}" ]; do - set -- "${1}0" "$2" - d="${1#*.}" - done elif [ ! -z "${2##*.*}" ]; then 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 dn="$(countd $2 $(countd $1))"; m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}" until [ "${m1#0}" = "$m1" ]; do @@ -45,6 +37,8 @@ done until [ "${m2#0}" = "$m2" ]; do m2="${m2#0}" done # dash has issues when numbers have leading 0's + # zsh,bash,etc don't need this at all # and really dash shouldn't either but /shrug +#printf '%s\n%s\n' "${m1}*${m2}" "9223372036854775807" ad "$((m1*m2))" "$dn" diff --git a/subtraction/sub b/subtraction/sub index 86c9bad..1e3f609 100755 --- a/subtraction/sub +++ b/subtraction/sub @@ -36,15 +36,11 @@ elif [ ! -z "${2##*.*}" ]; then d="${2#*.}" done 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#*.}" +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" ] && { set -- "$2" "$1" m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}"; nr="-" diff --git a/tester b/tester index 2e27eaa..5bb09a8 100755 --- a/tester +++ b/tester @@ -13,32 +13,54 @@ fd() { : $((n+=1)); m="$next" 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 - ran="$(fd $(${RPATH}))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do - ran="$(fd $(${RPATH}))"; : $((ran-=1)) + ran="$(fd $(ran))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do + ran="$(fd $(ran))"; : $((ran-=1)) done case $ran in - 0) ran=""; tn="$(${RPATH}).$(${RPATH}) $(${RPATH})";; - 1) ran=""; tn="$(${RPATH}) $(${RPATH}).$(${RPATH})" - ran="$(fd $(${RPATH}))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do - ran="$(fd $(${RPATH}))"; : $((ran-=1)) + 0) ran=""; tn="$(ran).$(ran) $(ran)";; + 1) ran=""; tn="$(ran) $(ran).$(ran)" + ran="$(fd $(ran))"; : $((ran-=1)); until [ "$ran" -eq 1 -o "$ran" -eq 0 ]; do + ran="$(fd $(ran))"; : $((ran-=1)) done case $ran in - 1) ran=""; tn="${tn% *}.$(${RPATH}) ${tn#* }" + 1) ran=""; tn="${tn% *}.$(ran) ${tn#* }" esac esac # use random number to determine if number will have decimal place case $i in - addition/add) op="+";; - subtraction/sub) op="-";; - multiplication/mul) op="*";; - division/div) op="/" + *addition/add) op="+";; + *subtraction/sub) op="-";; + *multiplication/mul) op="*";; + *division/div) op="/" esac aws="$(echo ${tn% *}${op}${tn#* } | bc -l)"; case "$(fd $aws)" in '.') aws="0${aws}" esac 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