0
1

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 3 years have passed since last update.

C++ での NaN のメモ

Posted at

NaN のビット定義

exponential bit が全部 1 で,

qNaN : mantissa 最上位(sign ビットの方向を上位とする)のビットが 1
sNaN : mantissa 最上位のビットが 0 で, それ以外どれかの mantissa bit が立っている
(inf: mantissa ビットが全部ゼロ)

したがって sign bit も入れて 2**24 - 1 通りの何かを NaN 値に埋め込むことができる.

NaN boxing などで使われている.

NaN の生成

std::numeric_limits<float>::quet_NaN()
std::numeric_limits<float>::signaling_NaN()

で生成できる.

例外

qNaN は例外ビットを立てない(はず).

std::isnan

std::isnan では sNaN か qNaN かの判定まではできない(std::fpclassify でも不可).
やるなら自前でビット演算でチェック?

min/max 演算

_mm_min_ps/_mm_max_ps は IEEE-754-2008 の振る舞いをする

C++11 では fmin/fmax

min/max あたりは, NaN の扱いが少し特殊なので注意しましょう.

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?