Pages: [1]   Go Down
Author Topic: variable equal one but don't equal one  (Read 443 times)
0 Members and 1 Guest are viewing this topic.
jordan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it's weird, the variable cursf1 equal one, but in (if) statement don't equal one, what's wrong?


Code:
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
)
« Last Edit: June 27, 2013, 04:21:37 pm by TTU.ABD » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The image didn't show up. Perhaps you can copy and paste some text that demonstrates the problem.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

jordan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I added the code and the results.
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

jordan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 3974
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Examples can be found in your IDE.

jordan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Montreal
Offline Offline
Full Member
***
Karma: 4
Posts: 179
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1872
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: