To be clear, here is a perfectly valid use of == with floats:No joke, any one testing floats with == would never be hired.
float f;
if (f==f)
printf("f is not NAN\n");
else
printf("f is NAN.\n");
It's more subtle than that. Many programmers think that just using <= instead of == solves the problems associated with floating point comparisons. It doesn't. What I always recommend, and I diligently try to follow my own advice, is that any time you are performing relational operation on floating point values, you need to carefully consider the implications of near-equality. What should happen, or at least what is acceptable, if the comparison barely misses equality on either side. If the answer isn't that both outcomes are tolerable, then you have quite a bit more work to do, and usually the best solution is to refactor the logic so that you are making decisions based on well-crafted integer expressions.When testing for limits, you should not test for equality. Use <= and >= instead of ==.
If you are using float or real data types, never use ==.
double v = 2.0;
while (v <= 4.0)
{
y = measure(v);
v += 0.2;
}
The fact that we are not checking for exact equality is sufficient to satisfy many programmers that life is good, but the real question that needs to be considered is what should happen if, due to accumulated roundoff error, a value of v is reached that is slightly less than 4.0 versus slight greater than 4.0? The final intended measurement may or may not get made.
This is NOT a C issue, this is a general logic issue when dealing with floating point computations. In this case, it's better to recraft the code so that it based on taking n data samples and computing the voltage value for each based on an integer counter.
I don't do floating point. Integers are good enough for me.
I am with you on that one. Wise choice.I don't do floating point. Integers are good enough for me.
That's awesome!I don't do floating point. Integers are good enough for me.
I did dewpoint calculation from temperature and humidity to 0.1 degree resolution in both °C and °F, all in integer arithmetic. Does that count?That's awesome!
When you get a chance, will you post code to perform a polynomial regression against a 2nd order equation for arbitrary input data using integer math?
Thanks.
No.I did dewpoint calculation from temperature and humidity to 0.1 degree resolution in both °C and °F, all in integer arithmetic. Does that count?
Whenever I next need one!That's awesome!
When you get a chance, will you post code to perform a polynomial regression against a 2nd order equation for arbitrary input data using integer math?
Thanks.
Another one of my peeves is the reluctance to use FP when it simplifies and makes X application software easier. This is double true when people are using 32-bit controller hardware with IEEE 754 FP hardware that's faster than integer math.
You'd be surprised how useful such a thing is, especially when automating the calibration of non-linear sensors.Whenever I next need one!
We understand the power and convenience of FP math.You'd be surprised how useful such a thing is, especially when automating calibration for non-linear sensors.
What's your reply got to do with 2nd Order Polynomial Regression Over Arbitrary Data Sets except that it's nearly impossible to do with integer arithmetic?We understand the power and convenience of FP math.
We also know that FP math occupies more MCU time and space.
We also know the pitfalls of relying on FP math when proper care is not applied.
https://wiki.sei.cmu.edu/confluence/display/c/FLP00-C.+Understand+the+limitations+of+floating-point+numbers#:~:text=As a result, they do,about either precision or range.
This is the one that might not be true today with 32-bit controllers. So , no, we don't know that automatically as a fact.We understand the power and convenience of FP math.
We also know that FP math occupies more MCU time and space.
We also know the pitfalls of relying on FP math when proper care is not applied.
https://wiki.sei.cmu.edu/confluence/display/c/FLP00-C.+Understand+the+limitations+of+floating-point+numbers#:~:text=As a result, they do,about either precision or range.
I was just wondering what benefits the == test for equality bring to the C language over the single = that other languages (Basic, for instance) use.
The question was why it needs to be different. There are plenty of languages that use the single equals sign for both purposes, where the compiler is perfectly capable of distinguishing from context whether the intention is assignment or comparison. Does C benefit from having two different symbols? Or does it detract from the writing of clear code? Or does it lead to errors?In summary, == is used for comparison, while = is used for assignment. It's essential to distinguish between these two operators to avoid logical errors in your programs
In C:There are plenty of languages that use the single equals sign for both purposes
butUsing = for assignment was a poor decision really, right there is where stuff started go down the wrong path.
A statement like
a = a + 1
is nonsensical, mathematically false.
I can't think of an application in C where using = deliberately in an if statement is in any way consistent with writing clear and easy-to-follow software.In C:
a = b;
and
a == b;
Are both valid syntax with different meanings. You may say the second statement is extraneous, but if either a or b is a volatile hardware register, it will have consequences.
It gets worse:
In the middle of an expression,
(a = b)
and
(a==b)
Are also both valid with different meanings.
So no, they are not different because it is “easier to parse”, they are different so it is possible to parse. Without the difference, C becomes ambiguous.