0
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 1 year has passed since last update.

[Swift] Double の min(), max() で Double.nan を扱うときは順番に注意せよ

Posted at

結論

以下のように、min()max() で評価するときに先頭に Double.nan があると、必ず Double.nan が抽出されてしまいます。

let hoge: Double = min(Double.nan, 0.0, -1.0)
print("hoge: \(hoge)") // hoge: nan

let moge: Double = min(0.0, Double.nan, -1.0)
print("moge: \(moge)") // moge: -1.0

let huga: Double = max(Double.nan, 0.0, 1.0)
print("huga: \(huga)") // huga: nan

let piyo: Double = max(0.0, Double.nan, 1.0)
print("piyo: \(piyo)") // piyo: 1.0

なので、先頭に Double.nan をとりうる計算結果などは、入れないように注意しましょう。

0
0
0

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
0
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?