C ++-除法类型? [英] C++ -- type of the division?

查看:58
本文介绍了C ++-除法类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保我对C ++除法的返回类型有所了解,

I want to make sure that my understanding of the return type of C++ division,

int / int => return is int?

float / float => return is which type? float?

double /double => return is double?

int / double => return is double?

int / float => return is float?

如果我错了,请纠正我.

Please correct me if I am wrong.

推荐答案

所有这些都是正确的.这是C ++ 03标准(第5/9节)所说的:

All of those are correct. Here's what the C++03 standard says (§5/9):

许多期望算术或枚举类型的操作数的二进制运算符都以类似的方式引起转换并产生结果类型.目的是产生一个通用类型,它也是结果的类型.这种模式称为常规算术转换,其定义如下:

  • 如果其中一个操作数的类型为 long double ,则另一个应转换为 long double .
  • 否则,如果其中一个操作数为 double ,则另一个应转换为 double .
  • 否则,如果其中一个操作数为 float ,则另一个应转换为 float .
  • 否则,必须对两个操作数执行积分提升(4.5).
  • 然后,如果其中一个操作数为 unsigned long ,则另一个操作数应转换为 unsigned long .
  • 否则,如果一个操作数是 long int ,而另一个 unsigned int ,则如果 long int 可以表示的所有值 unsigned int unsigned int 应该转换为 long int ;否则,两个操作数都应转换为 unsigned long int .
  • 否则,如果其中一个操作数为 long ,则另一个应转换为 long .
  • 否则,如果其中一个操作数为 unsigned ,则另一个应转换为 unsigned .
  • 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.
  • 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.

[注意:否则,唯一剩下的情况是两个操作数都是 int ]

[Note: otherwise, the only remaining case is that both operands are int]

这篇关于C ++-除法类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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