From 2531eee8753c35b603408fb7246dc5dc5441833b Mon Sep 17 00:00:00 2001 From: Mia Date: Tue, 22 Feb 2022 14:10:10 -0500 Subject: [PATCH] fix bug in addition --- addition/add | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/addition/add b/addition/add index a804392..6b0ba96 100755 --- a/addition/add +++ b/addition/add @@ -1,7 +1,19 @@ #!/bin/sh # add $2 to $1 and return [ -z "${1##*.*}" -o -z "${2##*.*}" ] && { # if float # dylanaraps/pure-sh-bible#check-if-a-number-is-a-float -# modified to check $1 & $2 + # modified to check $1 & $2 + # make sure $1 & $2 are the same length + if [ "${#1}" -gt "${#2}" ]; then + f2="$2"; until [ "${#f2}" -eq "${#1}" ]; do + f2="${f2}0" + done + set -- "$1" "$f2" + elif [ "${#1}" -lt "${#2}" ]; then + f1="$1"; until [ "${#f1}" -eq "${#2}" ]; do + f1="${f1}0" + done + set -- "$f1" "$2" + fi f1="${1##*.}"; f2="${2##*.}" # whole is ${1%.$f1} # dot required # eq is $1+$2 -- $f1 + $f2 -- then add $1 & $2 wo="$((${1%.$f1}+${2%.$f2}))" # whole digit, this is preset and not redeclared