# "cannot convert float to boolean"

I was trying to make a DVD screensaver and I’ve come this message “cannot convert float to boolean”
I don’t know how to fix it but this is my code to all the geneses on this platform

``````float x = 240;   // x location of square
float y = 0;     // y location of square

float xspeed = 5;   // speed of square on x axis
float yspeed = 5;   // speed of square on y axis

void setup() {
size(1920, 1080);
frameRate(144);
/////
/////
/////
}

void draw() {
background(255);

// Display the square

fill(175);
stroke(0);
rectMode(CENTER);
rect(x, y, 100, 100);

// movement

y = y + yspeed;
x = x + xspeed;

// bounce

if(x = 1870){  //////// highlighted
xspeed = xspeed * -1;
}
if(y = 1030){
yspeed = yspeed * -1;
}
}
``````
1 Like

One of the things that might see kinda weird about java if you haven’t used it much, is you have to use double equals like `==` if you want to check if something is equals. If your goal is to check if x is equal to 1870 then try `if (x == 1870) {` and so on. You’ll have to do the same for the line 3 below that one too.

2 Likes

good find, but as

x +=5;

is used might need something like

``````if ( x >= 1870 )

``````
4 Likes

The exact problem is that `=` and `==` are 2 completely different operations.
Say, for example, `x` is a float value. Then the operation `x=5` sets `x` to `5` - and it also returns the value of `x`.

So, `if(x = 1870){...` sets `x` to `1870`, and the operation, as a result, returns `1870` too, so you are practically doing `if(1870){...`. And that’s why you get an error - `if` statement expects a boolean value, but gets a float - and there is no direct obvious way to convert a float value to a boolean, so it just errors at you instead.

That’s why doing something like `x = y = z = 12345;` works - it gets resolved as `x = (y = (z = 12345));`, then `x = (y = 12345);`, then `x = 12345;`, practically setting every value to the deepest one in the operation chain - which is the rightmost in this case.

This can be useful to crunch up code to make it smaller or look cool. For example `if(x = x+1 == 20) ...` sets x to itself plus 1, and then compares that to 20, and does the `if` statement based on the result. (although I personally don’t like doing stuff like that)

The `==` operation, however, does not set anything to anything, takes 2 arguments, and returns a boolean.
So, `x == 1870` gets resolved into either `true` or `false` - exactly what `if(...)` statement wants.

You could also combine both operations the other way around: `myBool = x == 1870;` - this compares `x` to `1870` and sets `myBool` to either `true` or `false` depending on the result.

As far as I know, this is also the case in many other programming languages that differentiate the two operations.

Make sure to not repeat this mistake you made when comparing boolean statements - it will give no error, would set the left boolean to the right one’s value, and can easily cause a “what in the world is going on???” moment!

3 Likes

Thank you but now ive got a different problem while trying to add a sprite and it says "dvd cannot be resolved to a variable

``````float x = random(200,300);   // x location of square
float y = random(200,300);     // y location of square
float xspeed = 1;   // speed of square
float yspeed = 1;   // speed of square

void setup() {
size(800, 600);
frameRate(240);
//surface.setResizable(true);
PImage dvd;          /// dvd

}

void draw() {
background(255);

fill(0,0,0);
rect(0,0,10000,-1);

// Display the square
fill(dvd); /// highighted line
stroke(0);
rect(x, y, -200, -150);

y = y + yspeed;
x = x + xspeed;
// bounce
if(x > width){
xspeed = xspeed * -1;
}
if(y > height){
yspeed = yspeed * -1;
}
if(x < 200){
xspeed = xspeed * -1;
}
if(y < 150){
yspeed = yspeed * -1;
}
if (keyPressed) {
if (key == 'b' || key == 'B') {
xspeed = xspeed - 0.1f;
yspeed = yspeed - 0.1f;
}
}
``````