Score:0

invalid arithmetic operator (error token is ".5") with modulo

cn flag

Put simply I would like to check if the following statement is truthy: 8 % 1.5 === 0.5

secondMatch=1.5
secondMatchResult=0.5

for element in "${array[@]}"
do
    COUNTER=$((COUNTER + 1))
    if [[ $(($COUNTER%3)) -eq 1 ]]
    then
       FILE_CONTENTS="$FILE_CONTENTSfile 'input.mp4'"
    elif [[ $(($COUNTER%$secondMatch)) -eq $secondMatchResult ]]
    then
       FILE_CONTENTS="$FILE_CONTENTS\ninpoint 3180"
    else
       FILE_CONTENTS="$FILE_CONTENTS\noutpoint 6000\n"
    fi
    echo "$element"
done

The failing line with error:

elif [[ $(($COUNTER%$secondMatch)) -eq $secondMatchResult ]]

Full Error:

 ./cut.sh: line 36: 2%1.5: syntax error: invalid arithmetic operator (error token is ".5")

n.b please ignore my bash tallents :(

tm flag
bash can only do integer maths, no floats.
cn flag
:O!!! My mind is blown :D How can you test modulo with the 2nd, 5th, 8th, 11th indexed item? :O
tm flag
Modulo is only defined for integers, `8 % 1.5` makes no sense even outside bash.
cn flag
Ye fair point..
waltinator avatar
it flag
Modulo is defined across the real numbers. You can feed your real expressions through `bc` using a "here" document with shell interpretation, and get an integer result. Read `man bc`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.