Is casting of infinity to integer undefined?

前端 未结 3 1756
日久生厌
日久生厌 2021-01-17 07:18

Is the casting of infinity (represented by float) to an integer an undefined behavior?

The standard says:

4.10 Floating-integral conversions<

3条回答
  •  遥遥无期
    2021-01-17 08:22

    You said

    I can't tell whether "truncated value cannot be represented" covers infinity

    but it all boils down to

    What is the result of truncating infinity.

    The C standard (incorporated into C++ via 26.9) answers that quite plainly:

    Since truncation of infinity is still infinity, and infinity cannot be represented in int (I hope there's no question about this part), the behavior is undefined.

提交回复
热议问题