Re[5]: Глюк или фича?
От: 7C  
Дата: 14.06.07 04:04
Оценка:
7C>Здравствуйте, Bell, Вы писали:

B>>Это неверно. Тип выражения определяется типом "максимального" операнда, и при этом совершенно неважно, на каком месте в выражении он находится.


B>>

B>>5/9
B>>Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield
B>>result types in a similar way. The purpose is to yield a common type, which is also the type of the result.
B>>This pattern is called the usual arithmetic conversions, which are defined as follows:
B>>— If either operand is of type long double, the other shall be converted to long double.
B>>— Otherwise, if either operand is double, the other shall be converted to double.
B>>— Otherwise, if either operand is float, the other shall be converted to float.
B>>— Otherwise, the integral promotions (4.5) shall be performed on both operands.54)
B>>— Then, if either operand is unsigned long the other shall be converted to unsigned long.
B>>— Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent
B>>all the values of an unsigned int, the unsigned int shall be converted to a long int;
B>>otherwise both operands shall be converted to unsigned long int.
B>>— Otherwise, if either operand is long, the other shall be converted to long.
B>>— Otherwise, if either operand is unsigned, the other shall be converted to unsigned.
B>>[Note: otherwise, the only remaining case is that both operands are int ]


Нет, все равно не понимаю!
Тут должно работать следующее правило (выделенное):
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield
result types in a similar way. The purpose is to yield a common type, which is also the type of the result.
This pattern is called the usual arithmetic conversions, which are defined as follows:
— If either operand is of type long double, the other shall be converted to long double.
— Otherwise, if either operand is double, the other shall be converted to double.
— Otherwise, if either operand is float, the other shall be converted to float.
— Otherwise, the integral promotions (4.5) shall be performed on both operands.54)
— Then, if either operand is unsigned long the other shall be converted to unsigned long.
— Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent
all the values of an unsigned int, the unsigned int shall be converted to a long int;
otherwise both operands shall be converted to unsigned long int.
— Otherwise, if either operand is long, the other shall be converted to long.
— Otherwise, if either operand is unsigned, the other shall be converted to unsigned.
[Note: otherwise, the only remaining case is that both operands are int ]

Честно говоря, это правило непонятно для меня (точнее непонятна его двойственность).
Ну да бог с ним.
Но это правило мне не объясняет почему по-разному обрабатываются строки:
long b = a*z/1000; //b=4294261
long c = a*z; // c=-705600


P.S. Явное приведение типов [(int)x или int(x)] меня мало интересует (это всё я знаю). Меня смущает неожиданное и неопределенное поведение компилятора.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.