F
F
forgoty2018-10-15 01:08:50
C++ / C#
forgoty, 2018-10-15 01:08:50

Why might the if ( a == a) condition fail for Embedded development?

I went to an interview for the position of Embedded C Developer. At the interview they asked:
Why, what and under what conditions will the if statement not be executed?

int a;
float b;

int main(){
    if (a == a){
        ...
        (some code)
        ...
    }

    if (b == b){
        ...
        (some code)
        ...
    }

This immediately surprised me. I replied that no one would write like that. However, they offered to think. I started talking about interrupts. That it could arise and change the variables during the comparison. But they never answered me why, but simply kept silent. Please tell me to answer this question at least for yourself. Just wondering.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jcmvbkbc, 2018-10-15
@forgoty

float b;
...
if (b == b)

Fails if and only if b is NaN.
The rationale is in the standard: C99 standard clause 6.2.6.1:4 says:
Two values (other
than NaNs) with the same object representation compare equal, but values that compare
equal may have different object representations.
.

C
CityCat4, 2018-10-15
@CityCat4

Well, I'd say (b == b) won't work because the float storage format is such that it's impossible to make an exact comparison. A floating point number is a mathematical abstraction, for the implementation of which in computing (which operates with specific zeros and ones) a number of assumptions are applied;)

S
SagePtr, 2018-10-15
@SagePtr

In general, the compiler will cut all this to hell, because the volatile keyword was not set

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question