c++ - 将 long long 除以 int 会遇到什么问题?

标签 c++ c

什么是执行除法的更安全的方法:

long long/int 还是 long long/(long long) int

将 long long 除以 int 时,是否会存在一些安全问题或任何逻辑问题?

最佳答案

On dividing a long long by an int, can there be some security issues or any logical issues?

答案是NO,前提是你的分母不为 0。

来自文档:

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.
  • 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]

那就是

int / int => returns int

float / float => returns float

double /double => returns double

int / double => returns double

int / float => returns float

关于c++ - 将 long long 除以 int 会遇到什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931379/

相关文章:

c++ - 绕过私有(private)复制构造函数/复制赋值 C++

c - 递归求幂

c - 帮助 C 程序可执行 - Linux

c - AVL树中平衡因子的重新计算

c - 我是否正确释放内存?

c 内联汇编字符数组转短

c++ - 关闭 C++ 文件流未打开

c++ - 预编译的 Clang 看不到 Visual Studio header

c++ - 获取着色器字节码以传递给 CreateVertexShader()

c++ - 在函数之间更新 Sprite 的位置