由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 0 < -1 ? A c++ question
相关主题
面试问题请教一个c++ reference问题
a question about bitwise operation题2
小问题member and friend
C++隐式类型转换的规则?请教for_each
c++环境入门问题请问关于overloading <<
C++命名空间和算子重载string operator +
C++菜问: 怎么这样也可以?conversion between const to nonconst
C++ 初学者请教一个 iostream 的问题问个函数指针指向操作符的问题
相关话题的讨论汇总
话题: unsigned话题: int话题: question话题: arithmetic话题: c++
进入Programming版参与讨论
1 (共1页)
t*****h
发帖数: 137
1
I have a following question about unsigned and int type, and the result
really surprised me.
ubuntu 8.04 and g++ 4.24
Here is the code
#include
using namespace std;
int main()
{
unsigned x = 1;
int k = 1;
if ( (x-k) >= -1 )
cout << "0 >= -1\n";
else
{
cout << "0 < -1\n";
}
return 0;
}
T*****9
发帖数: 2484
2
unsigned int 0最小

【在 t*****h 的大作中提到】
: I have a following question about unsigned and int type, and the result
: really surprised me.
: ubuntu 8.04 and g++ 4.24
: Here is the code
: #include
: using namespace std;
: int main()
: {
: unsigned x = 1;
: int k = 1;

k*****l
发帖数: 177
3
回答的真含蓄 :)
这种不同类型之间的运算定义在哪里?我也想看看。

【在 T*****9 的大作中提到】
: unsigned int 0最小
t****t
发帖数: 6806
4
9 Many binary operators that expect operands of arithmetic or enumera-
tion 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 con-
verted to long double.
--Otherwise, if either operand is double, the other shall be converted


【在 k*****l 的大作中提到】
: 回答的真含蓄 :)
: 这种不同类型之间的运算定义在哪里?我也想看看。

T*****9
发帖数: 2484
5
thrust大牛
你这个文档是啥?能告诉我么?

【在 t****t 的大作中提到】
: 9 Many binary operators that expect operands of arithmetic or enumera-
: tion 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 con-
: verted to long double.
: --Otherwise, if either operand is double, the other shall be converted
:

t****t
发帖数: 6806
6
this is the approximation of c++98 standard (97/11 draft)

【在 T*****9 的大作中提到】
: thrust大牛
: 你这个文档是啥?能告诉我么?

T*****9
发帖数: 2484
7
thx!

【在 t****t 的大作中提到】
: this is the approximation of c++98 standard (97/11 draft)
t*****h
发帖数: 137
8
And also the usual arithmetic conversions covered in Arithmetic Conversions
are applied to operands of arithmetic types in relational operators.
This is really a subtle issue which has me caught.
l*****d
发帖数: 359
9
Conversion rules are more complicated when unsigned operands are involved.
The problem is that comparisons between signed and unsigned values are
machine-dependent, because they depend on the sizes of the various integer
types. For example, suppose that int is 16 bits and long is 32 bits. Then -
1L < 1U, because 1U, which is an unsigned int, is promoted to a signed long.
But -1L > 1UL because -1L is promoted to unsigned long and thus appears to
be a large positive number.
t*****h
发帖数: 137
10
thanks.
t****t
发帖数: 6806
11
l***g
发帖数: 1035
12
ansi standard, comparison between sized and unsized int is compiler depe
ndent, in other word, undefined.

【在 t*****h 的大作中提到】
: I have a following question about unsigned and int type, and the result
: really surprised me.
: ubuntu 8.04 and g++ 4.24
: Here is the code
: #include
: using namespace std;
: int main()
: {
: unsigned x = 1;
: int k = 1;

t*****g
发帖数: 12
13
nonono, compiler-dependent (i.e. implementation defined) is totally
different from undefined.

【在 l***g 的大作中提到】
: ansi standard, comparison between sized and unsized int is compiler depe
: ndent, in other word, undefined.

b******g
发帖数: 54
14
就是,非常欠揍

【在 k*****l 的大作中提到】
: 回答的真含蓄 :)
: 这种不同类型之间的运算定义在哪里?我也想看看。

1 (共1页)
进入Programming版参与讨论
相关主题
问个函数指针指向操作符的问题c++环境入门问题
C语言里面的register变量能否进行取地址操作? (转载)C++命名空间和算子重载
(面试题) 给code挑毛病(updated with multiple choices)C++菜问: 怎么这样也可以?
问个无厘头问题C++ 初学者请教一个 iostream 的问题
面试问题请教一个c++ reference问题
a question about bitwise operation题2
小问题member and friend
C++隐式类型转换的规则?请教for_each
相关话题的讨论汇总
话题: unsigned话题: int话题: question话题: arithmetic话题: c++