Я пытаюсь реализовать алгоритм последовательного деления в Bash
и я сталкиваюсь с проблемой, когда один из модулей в цикле возвращает ложный результат
Я пробовал несколько способов вычисления в основном по модулю ((a% b)), expr и bc, но у всех одинаковые проблемы
dec=$1
echo ----Dividing----
echo "dividing $dec by $2"
div=$((dec/$2))
echo "the result is $div"
rem=$((dec%$2))
res="$rem"
echo " the remainder is $rem"
while [ $div != 0 ]
do
div_old=$div
echo "dividing $div by $2"
div=$((div/$2))
echo "the result is $div"
rem=$(echo "$div % $2" | bc)
echo " the remainder is $rem"
if [ $rem != 0 ]
then
res="$rem$res"
else
res="$div_old$res"
fi
echo "for now the result is $res"
done
$ 1 = 2371 и $ 2 = 5
ожидаемый результат - 33441, но мой скрипт возвращает 33341
как видно из этого вывода
----Dividing----
dividing 2371 by 5
the result is 474
the remainder is 1
dividing 474 by 5
the result is 94
the remainder is 4
for now the result is 41
dividing 94 by 5
the result is 18
the remainder is 3
for now the result is 341
dividing 18 by 5
the result is 3
the remainder is 3
for now the result is 3341
dividing 3 by 5
the result is 0
the remainder is 0
for now the result is 33341
33341
но когда я пытаюсь выполнить ту же операцию вне скрипта, что и
echo $(echo "94 % 5" | bc)
результат 4, что хорошо,
Любая идея, почему есть такая разница между внутри / снаружи цикла?