This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Bye.'s profile picture

Error in if check

Started by Bye., 14 October 2015 - 01:24 PM
Bye. #1
Posted 14 October 2015 - 03:24 PM
I have a problem with this if:


if not fs.getFreeSpace("/") >= 100 then

the return is:


startup:103: attempt to compare boolean with number

Can you please help me?
Thanks
Lyqyd #2
Posted 14 October 2015 - 03:31 PM
The not happens before the comparison. You should remove the not and use < instead of >=.
Bye. #3
Posted 14 October 2015 - 03:57 PM
Thanks a lot but I don't fully understand why its happened
Lyqyd #4
Posted 14 October 2015 - 06:26 PM
Order of operations. As you can see in the operator precedence list, the `not` operator has a higher precedence than `>=` does. So, first, the result of the fs.getFreeSpace call is Boolean NOT'd, which would turn any number to a Boolean false value. Then it tries to compare `false >= 100`, which is where the error is thrown. So instead, you should change the comparison operator to make the comparison you're actually interested in, rather than trying to use the opposite comparison and inverting the result (which would work if you used parentheses to force the comparison to happen before the inversion, but there is no good reason to do that).