Does NumberBox.Value NaN not equal to double.NaN?

鲤鱼 秀 65 Reputation points
2024-05-11T07:35:25.51+00:00

When I use NumberBox (WinUI2), I have trouble as follows:

if (NumberBox.Value == double.NaN) // It keeps false.
{
     // Some action codes.
}

Is there any bug about this equation?

I would appreciate it if anyone could solve this problem.

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,401 questions
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,486 Reputation points Microsoft Vendor
    2024-05-13T02:16:02.3033333+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The reason for this behavior is the Double.NaN itself. Based on the document- Double.NaN Field, it is mentioned that: The Equality operator considers two NaN values to be unequal to one another. In general, Double operators cannot be used to compare Double.NaN with other Double values. And it is suggested to use the IsNaN method.

    Like:

    if (double.IsNaN(NumberBox.Value)) 
    {
        // Some action codes.
    }
    
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2024-05-11T07:35:25.5233333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The equality check between NumberBox.Value and double.NaN will always return false. This is because double.NaN is not equal to any value, including itself. Instead, you can use the double.IsNaN() method to check if a value is NaN. Here's an example:

    if (double.IsNaN(NumberBox.Value))
    {
        // Some action codes.
    }
    

    This should solve the problem you're experiencing.


    References:

    1 person found this answer helpful.
    0 comments No comments