Problem in Float variable

Hello all,

I have this very simple program as below:

float x;

void setup()
{
Serial.begin(57600);
}

void loop()
{
delay(100);
x = (1/2);
Serial.println(x);
}

but there is a problem :confused: and I really don't know why this not work??
the problem is when I make x equal to this (1/2) the result will be zero (0.00) but when makes x equal to (0.5) it is OK.
So why the compiler did not solve this simple operation and put the result in x as (0.5)??
So where is the problem here?

Thanks

The compiler is doing the arithmetic (the division of 1 by 2) using integer math, then converting the answer to a float. Integers cannot have decimal places, so 1/2 = 0. If you make one of the numbers a float, such as 1.0/2 or 1/2.0 the answer should be correct, or you can cast one of the numbers to a float such as (float)1/2

Thanks my dear for fast reply. in fact my problem is not in this program I just simplified it with this program it is bigger or more complex and I would like to illustrate it here:

see this piece of code:

Frequency = (500000 / HighTime);
FlowRate = Frequency / 7.5; // Flow rate in L/min
LitersCount = LitersCount + (FlowRate/60);

OK, what I am working on is taking a PWM from flow meter sensor and read the high pulse width in microsecond time and put it in HighTime variable, all variables are defined as float type variable, now when I using Serial.print to monitor variables all are OK and as expected except LitersCount it is showing "inf" :o when I changed HighTime or make it equal to any number like (HighTime = 2500 that is as read in real time from high pulse) it's works properly or replacing FlowRate in this equation LitersCount = LitersCount + (FlowRate/60) to LitersCount = LitersCount + (0.45/60) its work properly too :confused: and 0.45 is as read in real time from FlowRate.

I hope to understand my illustration and sorry if my English not good.
Thanks

That should not be, so it is likely that the problem is in the part of the program that you did not show to us. Please autoformat your program and post the ENTIRE program in code tags. Include the printing.

Thanks for replay

Sure this is all program below:

// *********************************************************************
// Macros settings
// *********************************************************************

#define _INPUT_SEN_SIG 2 //square wave input

// *********************************************************************
// Global variables
// *********************************************************************

unsigned char LockTEMP = 0;
float FlowRate, Frequency, Time = 1, LitersCount;
unsigned long NextValueMilli, NextValueMilli_1;
unsigned long FirstValueMicro, SecoundValueMicro, TherdValueMicro;
float HighTime, LowTime;

void setup()
{
// Initialize IO pins
pinMode(_INPUT_SEN_SIG, INPUT);
Serial.begin(57600);
}

void loop()
{
if ((digitalRead(_INPUT_SEN_SIG) == HIGH) && (LockTEMP == 0))
{
FirstValueMicro = micros();
LockTEMP = 1;
//LowTime = FirstValueMicro - SecoundValueMicro;
}
if ((digitalRead(_INPUT_SEN_SIG) == LOW) && (LockTEMP == 1))
{
SecoundValueMicro = micros();
LockTEMP = 0;
HighTime = SecoundValueMicro - FirstValueMicro;
}

//Time = HighTime + LowTime;

Frequency = 500000 / HighTime;
FlowRate = Frequency / 7.5; // Flow rate in L/min
//LitersCount = LitersCount + (FlowRate/60);

if (NextValueMilli <= millis())
{
NextValueMilli = millis() + 1000;
LitersCount = LitersCount + (FlowRate / 60);
}

if (NextValueMilli_1 <= millis())
{
NextValueMilli_1 = millis() + 100;
Serial.print(Frequency);
Serial.print(" ");
Serial.print(HighTime);
Serial.print(" ");
Serial.print(LitersCount);
Serial.print(" ");
Serial.println(FlowRate);
}
}

and this is what I got:

199.36 2508.00 inf 26.58
198.10 2524.00 inf 26.41
196.23 2548.00 inf 26.16
203.25 2460.00 inf 27.10
196.85 2540.00 inf 26.25
203.25 2460.00 inf 27.10
199.36 2508.00 inf 26.58
197.16 2536.00 inf 26.29
203.25 2460.00 inf 27.10
198.10 2524.00 inf 26.41
203.92 2452.00 inf 27.19

Thanks in advance

Several things:

  1. Place the cursor in the IDE's source code window and press Ctrl-T. This will reformat our code to a common C style and will help us read your code.

  2. Any floating point constant should have a ".0" at the end if it doesn't already have one, like:

Frequency = 500000.0 / HighTime;

Doing this insures that the two expression have the same data type when the expression is resolved.
It also documents your intention.

  1. You need to read up on "cast" in C. In the expression above, you are processing float expressions on
    the right side of the assignment, but then assigning it into a long. This forces the compiler to do a
    "silent cast". Most of the time this works because the compiler is smart enough to do the cast
    behind your back, but it's the lazy way to do it. It reads better if you do:

Frequency = (long) (500000.0 / HighTime);

This allows the math in the right-most expression to be done in floating point, but then the result is
then cast to a long for assignment into Frequency. Again, if nothing else, this documents your intent.

Not casting on some compilers can lead to obscure bugs. For example:

myInt = YourLong / MyLong;

For an Arduino Nano, long's are 4 bytes, int's are 2 bytes. So, once the two long's are divided, the
4 byte result is assigned into the int. This is like taking 4 gallons of water and trying to pour it into
a 2 gallon bucket...2 bytes of data might get slopped on the floor. (Not all compilers are equal.)
Sticking a cast in front of the expression:

myInt = (int) (YourLong / MyLong);

has the effect of putting the right side expression after division into a 2 gallon bucket and then
pouring the data into myInt.

My dear econjack,

OMG, "Cast" really thank you so much it solve the problem. :kissing:

Thanks