Hi, one short question, because I couldn't find an answer and I think its fast to answer, if you know the clue:
So I want to divide to numbers and want to get a float number like 0.2.
What do I do wrong?
I cant do it either with int, because it is rounding to zero
Here's my code:
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
}
void loop() {
float a = 1/5;
// put your main code here, to run repeatedly:
Serial.println(a);
delay(1000);
}
Edit:
Ok, sorry - after several tries I got it -> you have to do flaot a = float(1)/5
So I assume, that the number of the nominator has to be float. Is that right?
float a = 1/5;
The two operands of the division are integers, so according to the C language specification the division is done with integer division. This means that the result is the quotient and the remainder or fraction is thrown away.
So, 1/5 equals zero.
If you want the division done as floating point you can either force one or both of the operands to be floating point. Either with cast e.g.:
float a = (float)1/5;
or explicitly making one or both float.
float a = 1./5;
Pete
P.S. the forum didn’t notify me that you had modified your post.
You can change the numerator and/or the denominator to force the division to be float.
Ok, thank you, this explains my assumptions and the last hint was also new for me new.