Go Down

Topic: variable equal one but don't equal one (Read 685 times) previous topic - next topic

TTU.ABD

Jun 27, 2013, 10:51 pm Last Edit: Jun 27, 2013, 11:21 pm by TTU.ABD Reason: 1
it's weird, the variable cursf1 equal one, but in (if) statement don't equal one, what's wrong?


Code: [Select]
double cursf;
void setup(){
  Serial.begin(9600);
  delay(1000);
  Serial.println("go");
  cursf=0.01;
  Serial.println(cursf);
  cursf=cursf*10;
  cursf=cursf*10;
  Serial.println(cursf);
  Serial.println(int(cursf));
  if(cursf==1)
  {Serial.println("hello");}
}

void loop(){}


the results on serial monitor:
(
go
0.01
1.00
0
)

johnwasser

The image didn't show up. Perhaps you can copy and paste some text that demonstrates the problem.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

KeithRB

I can't see your code, but if it is a floating point variable, it is probably close to one, not exact.

Either use integers or code like this:
http://c-faq.com/fp/fpequal.html

TTU.ABD


KeithRB

Welcome to floating point!

As I have quoted before from Kernigham and Plauger _Elements of Programming Style_, "Floating point numbers are like sandpiles. Everytime you move one you lose a little sand and pick up a little dirt."

Also in your case, you are not comparing a floating point number to a floating point number, you are comparing a floating point number with the integer 1. You should use:
cursf == 1.0

Though it probably won't make a difference in this case, since the compiler will probably make the exact same code either way.

TTU.ABD


Also in your case, you are not comparing a floating point number to a floating point number, you are comparing a floating point number with the integer 1. You should use:
cursf == 1.0

I tried it [ if(cursf==1.0) ], but the results are the same.

GoForSmoke

Floating point is not exact. The more math you perform with it, the less exact it gets.

And btw, Arduino float and double are the same. 32-bit IEEE float.

As useless as it has been before I will mention to use fixed-point or choose smaller units and use integers. If I work in micro-meters (the unit, not the measuring tool) to achieve results in fractional meters then I will have 6 places accuracy and only need to add the decimal point for final output. A 32-bit signed integer (long) is good for 9 places.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

TTU.ABD


Floating point is not exact. The more math you perform with it, the less exact it gets.

I tried [ if(round(cursf)==1) ] , the result was right.
THANX.

JarkkoL

floats don't have infinite precision, thus when you write float x=0.01f; it's not exactly 0.01 but more like 0.0099999998f. It's generally not good idea to compare equality of floats (there are exception, but not in this case).

holmes4

instead of if(cursf==1) use if( (int)(cursf+0.5)==1)

Welcome to the world of floats ints and casts. Here's how it should work cursf is a float so the value will not be equal to 1 ever!. You should never ask if a float is equal to any thing ever. Greater or less than but never equal to.

Convert (casting) a float to an int all ways rounds DOWN never up or off so adding the 0.5 and then casting to an int will give you an int of 1 if cursf is between 0.5 and 1.4999.

Mark  

KeithRB

Somehow, I don't think we got through to him.

Go Up