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.

浮動小数点数の負値を unsigned int に代入すると未定義動作

Last updated at Posted at 2023-07-26

C99 §6.3.1.5 第2段落及び注 50.
C11 も引用したほうがいいのかもしれないが手元にない。
実際には x86 Linux gcc では、あたかも符号付き整数型の負値を unsigned int に代入したかのような動作、つまり UINT_MAX+1 の剰余がとられて、たとえば -1.0 は -1 と同様に 0xFFFFFFFF になるのだが、x86 でさえそれをあてにしてはいけない(未定義だから違う値になっても落ちても文句はいえない)。[追記:コメントに実例あり]
さらに x86 Linux でなければ違う値になることがある。なのだが未定義動作なのでコンパイラのドキュメントには書かれない。なので引用して違うとお伝えすることはできない。

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?