Плавающе-точечное
Nov. 18th, 2013 12:51 amВ какой-то мере навеяно постом
Что произойдет в вашей системе и что, по-вашему, должно произойти при попытке компиляции (и, если компиляция была/должна быть успешной, то запуска) следующей программы на Си:
#include <stdio.h>
main() {
printf("%d %d %d\n", (int)(-1.0/0.0), (int)(0.0/0.0), (int)(1.0/0.0));
}
no subject
Date: 2013-11-18 09:11 am (UTC)Потому что -inf, nan, inf.
no subject
Date: 2013-11-18 04:22 pm (UTC)#include <stdio.h>
#include <stdlib.h>
int main(int c, char **v) {
printf("%d %d %d\n", (int)(-1.0/strtod(v[1], 0)), (int)(0.0/strtod(v[1], 0)), (int)(1.0/strtod(v[1], 0)));}
Что выходит с аргументом 0?
no subject
Date: 2013-11-18 06:20 pm (UTC)no subject
Date: 2013-11-18 09:18 am (UTC)Microsoft (R) C/C++ Optimizing Compiler Version 17.00.60610.1
no subject
Date: 2013-11-18 04:23 pm (UTC)no subject
Date: 2013-11-18 04:28 pm (UTC)no subject
Date: 2013-11-18 04:58 pm (UTC)no subject
Date: 2013-11-18 05:01 pm (UTC)no subject
Date: 2013-11-18 05:08 pm (UTC)no subject
Date: 2013-11-19 06:25 am (UTC)no subject
Date: 2013-11-18 09:21 am (UTC)void main() {double z = 0.0;
printf("%d %d %d\n", (int)(-1.0/z), (int)(0.0/z), (int)(1.0/z));
}
-2147483648 -2147483648 -2147483648
no subject
Date: 2013-11-18 09:22 am (UTC)no subject
Date: 2013-11-18 04:28 pm (UTC)no subject
Date: 2013-11-19 06:14 am (UTC)C99:
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.
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.
no subject
Date: 2013-11-19 06:44 am (UTC)no subject
Date: 2013-11-19 06:50 am (UTC)no subject
Date: 2013-11-19 06:52 am (UTC)no subject
Date: 2013-11-19 06:56 am (UTC)no subject
Date: 2013-11-19 07:23 am (UTC)no subject
Date: 2013-11-19 07:26 am (UTC)no subject
Date: 2013-11-19 07:31 am (UTC)int i = -1;unsigned u;
printf("%d\n", (i%2)^(u%1));
потому что правила распространения беззнаковости по дереву выражения нечеткие.
no subject
Date: 2013-11-19 07:33 am (UTC)no subject
Date: 2013-11-19 06:52 am (UTC)А потом обновляешь компилятор или запускаешь другой компилятор на том же процессоре с той же ОС, и результат другой.
no subject
Date: 2013-11-19 06:57 am (UTC)no subject
Date: 2013-11-19 07:07 am (UTC)no subject
Date: 2013-11-19 07:27 am (UTC)no subject
Date: 2013-11-18 09:31 am (UTC)Computer says −80000000.
no subject
Date: 2013-11-18 04:59 pm (UTC)no subject
Date: 2013-11-18 09:46 am (UTC)-2147483648 -2147483648 -2147483648
clang version 3.3 (tags/RELEASE_33/rc3)
-2147483648 0 2147483647
gcc (Debian 4.6.3-14+rpi1) 4.6.3
-2147483648 0 2147483647
no subject
Date: 2013-11-18 04:29 pm (UTC)no subject
Date: 2013-11-18 06:04 pm (UTC)no subject
Date: 2013-11-18 06:16 pm (UTC)no subject
Date: 2013-11-19 02:38 am (UTC)no subject
Date: 2013-11-19 05:03 am (UTC)no subject
Date: 2013-11-19 06:24 am (UTC)no subject
Date: 2013-11-19 06:23 am (UTC)Если ты хочешь такого, но не видишь, то это ошибка в компиляторе:
The semantic rules for the evaluation of a constant expression are the same as for nonconstant expressions. 97)
97) Thus, in the following initialization,
static int i = 2 || 1 / 0;
the expression is a valid integer constant expression with value one.
no subject
Date: 2013-11-19 06:48 am (UTC)