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

API Error. math.sqrt()

Started by Connor_13_, 08 February 2013 - 10:02 AM
Connor_13_ #1
Posted 08 February 2013 - 11:02 AM
I made a program that tests the square roots of some fuel values and i get this error…

test:74: attempt to compare __le on boolean and number

my code on line 74 is…


sticks = 5
woodenTools = 10

if math.sqrt(sticks) <= x <= math.sqrt(woodenTools) then
  recomendedFuel = "Wooden Tools"
  fuel = woodenTools
end
Lyqyd #2
Posted 08 February 2013 - 11:07 AM
You cannot use the conditionals like that. It would evaluate one part first, resulting in a boolean, then attempt to compare the boolean with the other term. Try this instead:

if smallValue <= x and x <= largeValue then
OmegaVest #3
Posted 08 February 2013 - 11:08 AM
What, exactly, is x, though?

Also, I'm not entirely sure, but I think you have to break that three-part statement into two two-part statements.


if (math.sqrt(sticks) <= x) and (math.sqrt(woodentools) >=x) then
   recomendedFuel = "Wooden Tools"
   fuel = woodenTools
end
shiphorns #4
Posted 08 February 2013 - 11:10 AM
Due to left-to-right association, it's comparing (math.sqrt(sticks) <= x) against (math.sqrt(woodenTools))

The first expression evaluates as a boolean (true or false) and the second is a number (what math.sqrt() returns)

You need to write it out as:

if (math.sqrt(sticks) <= x) and (x <= math.sqrt(woodenTools)) then

end
Lyqyd #5
Posted 08 February 2013 - 11:18 AM
Interesting that you both used parentheses, despite them not being necessary. They're fine either way, of course.
Connor_13_ #6
Posted 08 February 2013 - 11:42 AM
thanks i got it to work
ChunLing #7
Posted 08 February 2013 - 11:51 AM
For purposes of clarifying what the program is attempting to do (and why it doesn't work) they are convenient, at least. But for purposes of simply writing clean code they are unnecessary and a bit distracting. Anyway, all's well that ends well.
shiphorns #8
Posted 08 February 2013 - 07:48 PM
Interesting that you both used parentheses, despite them not being necessary. They're fine either way, of course.

Since lack of understanding of how the expressions were being grouped was the very essence of the OP's problem, it made sense to show it explicitly. Personally, even though Lua lets you omit them, I'd rather see more parentheses than are necessary, than fewer. Grouping with parentheses makes larger, more complex expressions easier to parse visually and spot mistakes.
ChunLing #9
Posted 08 February 2013 - 08:13 PM
Eh…it depends. For most purposes, the intention and operator precedence is clear enough without them, and they really are a tiny bit distracting, which can accumulate fast.

When there is any real question of operator precedence, they are very helpful. And people's level of familiarity with operator precedence does vary. But people trying to read much code should at least be able to distinguish the precedence of arithmetic, equivalence tests, and logic.
Kingdaro #10
Posted 09 February 2013 - 03:23 AM
For me, putting the comparisons on separate lines is better than using parentheses. It keeps all the code on the screen, and it doesn't look all cluttered.


if var1 > var2
and var1 < var2
and var1 == var2 then
  -- something that would literally never happen
  -- but example is still valid :3
end