分开忽视休息的有效方法

1 c++ division floor modulus

我发现有两种方法可以从c ++中的一个分区得到一个整数

问题是哪种方式更有效(更快)

第一种方式:

Quotient = value1 / value2;  // normal division haveing splitted number

floor(Quotient);             // rounding the number down to the first integer
Run Code Online (Sandbox Code Playgroud)

第二种方式:

Rest = value1 % value2;             // getting the Rest with modulus % operator

Quotient = (value1-Rest) / value2;  // substracting the Rest so the division will match
Run Code Online (Sandbox Code Playgroud)

还请说明如何找出哪种方法更快

n. *_* m. 5

如果你正在处理整数,那么通常的方法是

Quotient = value1 / value2;
Run Code Online (Sandbox Code Playgroud)

而已.结果已经是整数.无需使用该floor(Quotient);声明.无论如何它都没有效果.如果需要,你会想要使用Quotient = floor(Quotient);.

如果您有浮点数,那么第二种方法根本不起作用,因为%只为整数定义.但是从实数的一个分区得到一个整数是什么意思呢?当你将8.5除以3.2时得到什么整数?问这个问题有意义吗?

作为旁注,你称之为"休息"的东西通常被称为"提醒".剩余.