3

Вопрос приведения типов.

double var = -inf; // or +inf
auto integer = static_cast<uint32_t>(var);

Какое значение будет в переменной integer, это описано в стандарте?

0

1 ответ 1

9

Согласно Стандарту, для C это приведет к UB

6.3.1.4 Real floating and integer

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.50)

Для C++ аналогично:

7.3.11

A prvalue of a floating-point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type. [Note: If the destination type is bool, see [conv.bool]. — end note]

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.