mirror of https://dicksdeathabove.xyz/~mia/psh-fractional synced 2024-07-27 03:59:08 +00:00

-- subtraction:

---- had issues due to an edge case before:
------ eg: 49518.81665 - 22399.4358
------ produces: 47278.87307
------ correct:  27119.38085
---- this happened when ${#1} > ${#2}
---- if $2 contains decimals
-- tester:
---- now exits `1` if answer is "false"
-- division
---- debugging on going
This commit is contained in:
Mia T. Rain 2022-04-22 14:36:58 -04:00
parent e0cf732500
commit 0207f421f4
Signed by: Mia
GPG key ID: 5F40D66F94DECFE8
5 changed files with 41 additions and 14 deletions

33
README
View file

@ -2,22 +2,39 @@
psh-fractional
-- fractional math in pure sh, successor to psh-float-micro
---
Dev Status
-- this is on pause for now; I'm really tired
---
Current Status
-- addition 100% working
-- addition ~85% working
---- has some odd issues with decimals
---- has issues with "Illegal number"
------ eg: 18471.81672 + 27540.27237
---- will debug at later date
-- subtraction ~90% working
---- decimal placement working now
---- some numbers produce the wrong results... no idea why
---- had issues due to an edge case before: see last commit
---- has an issue where $e in rad() somehow becomes malformed
------ eg: 23019.18605 - 1139
------ produces: 11629.18605
------ correct: 21880.18605
---- current method uses a system of removing the decimal from $1 & $2
---- and then subtracting them; (m1-m2)
---- the issue is likely in rad() as $((m1-m2)) is correct before
---- being handed to rad()
-- multiplication 100% working
---- length issues when comparing to bc (produces additional decimals)
------ I am not going to attempt to fix this
------ it's really not an issue
-- division ~90% working
---- may sometimes produce to many decimals
------ this will not be fixed
---- rarely places decimals in wrong places
------ eg: 324.81478 / 21715
------ produces: 1.49580833525212986414
------ correct: 0.01495808335252129864
---- Some odd edge case with $dp is causing this...
---- debugging ongoing
---
Outer Usage
-- it is recommended to use this library with the upcomming
-- psh-fc if you wish to compare numbers that it produces
-- it is recommended to use this library with psh-fc
-- if you wish to compare numbers that it produces
-- as decimal numbers are not considered numbers by posix sh
---- https://dicksdeathabove.xyz/~mia/psh-fc
---

View file

@ -63,7 +63,7 @@ ld() { # $1/$2
esac
#echo "$1 $2"
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"
o="$((${di%.*}/${div%.*}))"; [ "${#o}" -eq 0 ] && : $((count+=2)); : $((count+=${#o}))
sd="${di%.*}"; : $((count+=${#sd}));
until [ "${#di}" -eq "$((count+1))" ]; do
@ -81,14 +81,17 @@ ld() { # $1/$2
fd="${sd}${fd}"
#echo "$fd"
}
sd="$((fd/div))"; [ "$sd" -eq 0 ] && : $((count+=1))
sd="$((fd/div))"
#[ "$sd" -eq 0 ] && : $((count+=1))
o="${o}${sd}"; sd="$((sd*div))"
#printf " %s\n- %s\n" "$fd" "$sd"
sd="$((fd-sd))"
#echo "| $sd -- $i"
#echo "$o $dp"
di="${di#?}" # $fd$sd
: $((n+=1))
done
#echo "$o"
n=0; sd="$o"; while [ "$sd" ]; do
next="${sd#?}"
[ "$n" -eq "$dp" ] && {
@ -98,10 +101,10 @@ ld() { # $1/$2
sd="$next"
: $((n+=1))
done
#echo "-- $o"
#until [ "${o#0}" = "$o" ]; do
# o="${o#0}"
#done
#echo "$o"
if [ ! -z "${o##*.*}" -a "$dp" -gt 0 ]; then # missing decimal place
do="$o"; [ "${#do}" -gt "$dp" ] && until [ "${#do}" -eq "$dp" ]; do
do="${do%?}"

View file

@ -37,8 +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"

View file

@ -37,7 +37,9 @@ elif [ ! -z "${2##*.*}" ]; then
done
fi # if $1/$2 is not a float; convert to float
m1="${1%.*}${1#*.}"; m2="${2%.*}${2#*.}"
dn=$((${1%.*}-${2%.*})); dn="${dn#-}"; dn="${#dn}"
dn=$((${1%.*}-${2%.*})); # dn == dp
# dn can also be checked to see if the wrong whole number result produced is correct
dn="${dn#-}"; dn="${#dn}"
# decimal place is determined by the length of $1-$2
# without decimals
## SWAP IF $1 > $2; negative handling
@ -54,4 +56,9 @@ until [ "${m2#0}" = "$m2" ]; do
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
##
# if $m1 is longer than $m2 it may cause it to produce wrong results
[ -z "${2##*.*}" ] && until [ "${#m2}" -eq "${#m1}" ]; do
m2="${m2}0"
done
rad "$((m1-m2))" "$dn" "${nr}"

2
tester
View file

@ -60,7 +60,7 @@ IFS=" "; for i in ${types}; do
} || status=1
case "$status" in
0) printf 'Correct!\n\n' ;;
1) printf 'False..\n\n'
1) printf 'False..\n\n' && exit 1
esac
done
done