同类数值类型之间的转换 [英] Conversion between numeric types of the same kind
问题描述
我正在阅读 http://www.cplusplus.com/doc/tutorial/typecasting/.它说:
否则,如果转换是在同类型的数值类型之间(整数到整数或浮点到浮点),转换有效,但该值是特定于实现的(并且可能不可移植).
Otherwise, if the conversion is between numeric types of the same kind (integer-to-integer or floating-to-floating), the conversion is valid, but the value is implementation-specific (and may not be portable).
但是我真的不明白上面的引用是什么意思?有人会用一个简单的例子解释它吗?为什么相同类型的数字类型之间的转换会导致特定于实现的值?是什么原因?
But I really didn't understand what does the above quote mean to say? Will someone please explain it using an simple example? Why conversion between numeric type of same kind results in implementation-specific value? What is the reason?
推荐答案
让我们考虑以下示例:
long long int lli = 5000000000;
long int li;
int i;
li = lli;
i = li;
你能预测lli
、li
和i
的值吗?或者 li
和 i
是否具有相同的值?
Can you predict the values of lli
, li
and i
? Or whether li
and i
have the same value?
答案是 - 值取决于为每种类型分配的字节数!IE.对于某些情况 int
等于 long int
,对于其他情况 long int
等于 long long int
,但是一般来说,long
er 类型可以更长.float
、double
和 long double
类似(在内存大小方面).
Answer is - values depend on the number of bytes allocated for each type!
I.e. for some cases int
is equal to long int
, for others long int
is equal to long long int
, but in general long
er types just CAN be longer. Similar (in sense of memory size) for float
, double
and long double
.
这篇关于同类数值类型之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!