6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

なんでしょう?(小ネタ)

Last updated at Posted at 2018-01-07

いきなりコード

問題
static void Main(string[] args)
{
    var a = /* ここに何かが入ります */;
    var b = a;
    Console.WriteLine(b == a);
}

で、実行結果。
image.png

さて、aには何が代入されたでしょうか?








(これぐらいスクロールすれば大丈夫かな?






正解
static void Main(string[] args)
{
    var a = double.NaN;
    var b = a;
    Console.WriteLine(b == a);
}

というわけで、正解はNaN(なん)でした。
(NaNでしょう?NaNでした。ダジャレかよ。

それはさておき、NaNも含めて等しいかを判定するにはEqualsを使いましょう。

static void Main(string[] args)
{
    var a = double.NaN;
    var b = a;
    Console.WriteLine(b.Equals(a));
}

image.png

正しく判定できました。

6
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?