Using the float variable

Hello everyone, can someone help me with this question?

Why is the result 3 and not 3.333?
What am I doing wrong?
I have tried both ways

      int x = 10;
      int y = 3;
      float respaldo = (x / y);
      Serial.println(respaldo, 3);
      int x = 10;
      int y = 3;
      float respaldo = (float)(x / y);
      Serial.println(respaldo, 3);

image

You are instructing the computer to do an integer divide and convert the result to float. The answer is correct.

Try this

float respaldo = (float) x / y;

float respaldo = (float) x / y;
This works fine, thanks!

Ok.

1. Do you know the meaning/value of (float) x? (Given: int x = 10;)
2. Without uploading the following codes in the Arduino UNO, can you tell what will be the value of y --?

float x = 10.93;
int y =  (int) x;

"y" will be 10, because "y" is a integer

2 Likes

y will be 10 because x was casted to int....

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.