0

Hey I am trying to return a list of values based on a certain criteria but the bash output is unexpected.

oneIter=80.50
halfIter=40.25
twoIter=45.08
threeIter=36.22

numArr=()
posArr=()

#Creating an array of numbers starting from 0 to 81 with a step size of 0.01
for x in $(seq 0 0.01 81); do numArr+=(${x}); done

#Iterating through numArr and adding values to posArr if they satisfy condition
for x in ${numArr[@]}; do
  if [[ ${x} < ${oneIter} && ${x} > ${halfIter} && ${x} < ${twoIter} /
    && ${x} > ${threeIter} ]]; then
      posArr+=(${x})
  fi
done

for x in ${possArr[@]}; do echo $x; done

The last line code returns the values in the possArr array. But the values for some reason include starting from 4.03, 4.04, 4.05, ..., 4.50 and then after 4.50 the actual values that satisfy the condition are listed 40.26,40.27,...45.07. My question is why does the possArr include the 4.03 - 4.50 values when they clearly break the condition? Thank you.

  • Bash doesn't support floating-point math natively. Consider multiplying all your numbers by 100 so you can operate on them as integers (`(( x < oneIter ))` is an example of the syntax for numeric comparison). – Charles Duffy Aug 10 '21 at 16:46
  • `[[ $string < $otherString ]]` is **string** comparison, not numeric comparison. It goes character-by-character, so it thinks 100 is smaller than 2, because it sees `[[ 1 < 2 ]]` and then stops there. – Charles Duffy Aug 10 '21 at 16:47
  • ${numArr[@] should be ${numArr[@]} – Aval Sarri Aug 10 '21 at 16:50

0 Answers0