I'm confused about how I know what to make an int or float

I got this message "The operator || is undefined for the argument type(s) int, float" on the bolded and italicized line.

This code in general probably is error-ridden in many other ways but for now I'm just trying to know why that line got that error message. Any thoughts?

int circleX;
int circleY;
int circleWidth;
int circleHeight;

void setup() {
  size(200,200);
  circleX = 100;
  circleY = 100;
  circleWidth = 10;
  circleHeight = 10;
}

void draw() {
  ellipse(circleX,circleY,circleWidth,circleHeight);
  circleX = circleX + 1;
  circleY = circleY + 1;

  if ((circleX = 0 + circleWidth * 0.5) 
    || (200 - circleWidth * 0.5) 
    || (circleY = 0 + circleHeight * 2) 
    || (circleY = 200 - circleHeight * 2));
    circleX = circleX + random(-1,1);
    circleY = circleY + random(-1,1);
  }

Thanks!

Answers

  • edited April 2018

    https://Forum.Processing.org/two/discussion/15473/how-to-format-code-and-text

    if ((circleX = 0 + circleWidth * 0.5) || (200 - circleWidth * 0.5) || (circleY = 0 + circleHeight * 2) || (circleY = 200 - circleHeight * 2));

    Each expression above evaluates as a float value! :-SS

    In order to use the operator ||, only boolean values are permitted! #-o

    In order to get a boolean outta those expressions, use the relational operators, such as ==, !=, >, <, >= & <=. :-B

    BtW, I'm suspicious you should replace the logical operator || w/ && instead. :-?

    Maybe you should take a look at collision detection for circles: *-:)
    http://JeffreyThompson.org/collision-detection/circle-circle.php

  • I've tried formatting the code but there are syntax errors. The condition that starts on line 19 above ends with a ; so isn't going to work. Should be a {

    You are also missing a final closing } on the draw

    And the = signs in the comparison should be ==

  • edited April 2018

    Regarding if() in general, keep in mind that "if" relates to statements that are true or false.

    if(true){ print("true"); }
    if(false){ print("false"); }
    if(false || false || false || true){ print("etc."); }
    if(x==3 || x==4){ print("etc."); }
    

    But what should happen if you use an int?:

    if(3){ print("???"); }
    if(3 || 4){ print("???"); }
    

    ...? "If x equals 3" makes sense, "If 3" doesn't make a lot of sense....

  • The answer is there in the last 3 comments but you have to dig a bit.

    = is the assignment operator and it means evaluate the expression on the right hand side and store the result in the variable on the left hand side.

    == is the equality operator and compares the result of evaluating both sides of the operator.

    Therefore in line 19 (circleX = 0 + circleWidth * 0.5) should be (circleX == 0 + circleWidth * 0.5) but now you have another problem.

    Although circleX and circleWidth are both integers, '0.5' is a floating point number (FPN). When you perform mixed integer and FPN calculations the integers are converted to FPN. This means that in the statement
    (circleX == 0 + circleWidth * 0.5)
    the expression on the right evaluates to a FPN and the left hand side will be treated as a float to perform the comparison. Comparing floating point numbers for equality if fraught with problems due to limitations of storing floats in memory.

Sign In or Register to comment.