c++ division by 0

前端 未结 5 1982
-上瘾入骨i
-上瘾入骨i 2020-11-29 08:28

I am running long simulations. I record the results into a vector to compute statistics about the data. I realized that, in theory, those samples could be the result of a di

5条回答
  •  Happy的楠姐
    2020-11-29 09:14

    Note that C standard says (6.5.5):

    The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.

    So something/0 is undefined (by the standard) both for integral types and Floating points. Nevertheless most implementations have fore mentioned behavior (+-INF or NAN).

提交回复
热议问题