# "cannot convert float to boolean"

**URL:** <https://discourse.processing.org/t/cannot-convert-float-to-boolean/6822>\
**Category:** Beginners\
**Created:** [December 22, 2018, 10:43am UTC](https://discourse.processing.org/t/cannot-convert-float-to-boolean/6822 "2018-12-22T10:43:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![C0R4L](https://avatars.discourse-cdn.com/v4/letter/c/5e9695/32.png) [@C0R4L](https://discourse.processing.org/u/C0R4L)\
**Post date:** [December 22, 2018, 10:43am UTC](https://discourse.processing.org/t/cannot-convert-float-to-boolean/6822/1 "2018-12-22T10:43:00Z")

</div>

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

```auto
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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)\
**Post date:** [December 22, 2018, 11:40am UTC](https://discourse.processing.org/t/cannot-convert-float-to-boolean/6822/2 "2018-12-22T11:40:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 22, 2018, 11:54am UTC](https://discourse.processing.org/t/cannot-convert-float-to-boolean/6822/3 "2018-12-22T11:54:02Z")

</div>

good find, but as

> x +=5;

is used might need something like

```auto
if ( x >= 1870 )

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [December 22, 2018, 7:22pm UTC](https://discourse.processing.org/t/cannot-convert-float-to-boolean/6822/4 "2018-12-22T19:22:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![C0R4L](https://avatars.discourse-cdn.com/v4/letter/c/5e9695/32.png) [@C0R4L](https://discourse.processing.org/u/C0R4L)\
**Post date:** [December 24, 2018, 1:30pm UTC](https://discourse.processing.org/t/cannot-convert-float-to-boolean/6822/5 "2018-12-24T13:30:18Z")

</div>

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

```auto
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
dvd = loadImage("dvd.png"); /// 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);

  // Add speed to location.
  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;
    }
  }

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 24, 2018, 2:02pm UTC](https://discourse.processing.org/t/cannot-convert-float-to-boolean/6822/6 "2018-12-24T14:02:23Z")

</div>

pls. do not answer here!

> [@"dvd cannot be resolved to a variable"](https://discourse.processing.org/t/dvd-cannot-be-resolved-to-a-variable/6876):
>
> I’ve 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 dvd = loadImage("dvd.png"); /// dvd } void draw() { background(255); fill(0,0,0); rect(0,0…

---

<div class="post-metadata">

**Author:** ![C0R4L](https://avatars.discourse-cdn.com/v4/letter/c/5e9695/32.png) [@C0R4L](https://discourse.processing.org/u/C0R4L)\
**Post date:** [December 24, 2018, 2:30pm UTC](https://discourse.processing.org/t/cannot-convert-float-to-boolean/6822/7 "2018-12-24T14:30:53Z")

</div>

i fixed it…  
…  
…
