什么是执行除法的更安全的方法:
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 tolong double
.- Otherwise, if either operand is
double
, the other shall be converted todouble
.- Otherwise, if either operand is
float
, the other shall be converted tofloat
.- Otherwise, the integral promotions (4.5) shall be performed on both operands.
- Then, if either operand is
unsigned long
the other shall be converted tounsigned long
.- Otherwise, if one operand is a
long int
and the otherunsigned int
, then if along int
can represent all the values of anunsigned int
, theunsigned int
shall be converted to along int
; otherwise both operands shall be converted tounsigned long int
.- Otherwise, if either operand is
long
, the other shall be converted tolong
.- Otherwise, if either operand is
unsigned
, the other shall be converted tounsigned
.[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/