5

I was asked this questino on a programming test. The question was, I was passed in a float as a parameter, and asked when would the following code be false.

bool result  = (floatValue == floatValue);

I couldn't think of a valid reason or a situation of when this would be false and still can't. In the end, I answered that there will never be a case when this would be false. Was wondering if anyone can give me some example(s) of when this would be false

10
  • 1
    stackoverflow.com/a/570694/3093378 Commented Oct 2, 2015 at 3:51
  • //posted from phone. Will edit Commented Oct 2, 2015 at 3:51
  • stackoverflow.com/questions/18661635/… Commented Oct 2, 2015 at 3:54
  • 1
    @M.M That's true when comparing two different float values, but in this case I think you're comparing one variable with itself. Commented Oct 2, 2015 at 4:37
  • 1
    @M.M. not sure I understand. floatValue here refers to the same lvalue. It's stored only once. Commented Oct 2, 2015 at 4:38

1 Answer 1

12

floatValue == NaN

If floatValue is the result of a computation that is Not a Number, like 0/0.

Sign up to request clarification or add additional context in comments.

2 Comments

OH. I was totally forgetting NaN. Does this also apply if floatValue is INF? Or is this just for when floatValue is NaN?
Only for NaNs. Infinities compare normally (-infinity < any real number < +infinity; +infinity == +infinity; etc.).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.