同类数值类型之间的转换 [英] Conversion between numeric types of the same kind

查看:38
本文介绍了同类数值类型之间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 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;

你能预测llilii的值吗?或者 lii 是否具有相同的值?

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,但是一般来说,longer 类型可以更长.floatdoublelong 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 longer types just CAN be longer. Similar (in sense of memory size) for float, double and long double.

这篇关于同类数值类型之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆