简体   繁体   English

整数除法与常规除法

[英]integer division vs regular division

I need to compare an integer in a loop to the quotient of a long and a long . 我需要将循环中的整数与longlong的商进行比较。 in order to not do integer division, if I understand correctly do I need to convert one of the longs into a double? 为了不进行整数除法,如果我理解正确,是否需要将多头之一转换为双精度?

long prime = primes[d];
int i = 1;

//  "inputNumber / prime" should not be integer division, while it is now.
//  How do I do this yet still compare it to an "int" afterwards? 
while (i < (inputNumber / prime))
{
    primes[i*prime] = 0;
    i++;
}

That's the code snippet. 那是代码片段。 primes is an array filled with long s. primes是一个由long填充的数组。 Btw is this code correct: 顺便说一句,此代码是正确的:

primes[i*prime] = 0;

because I am worried that a long * int won't work for an array index. 因为我担心long * int不适用于数组索引。

Thanks so much! 非常感谢!

Why not while((i * prime) < inputNumber) instead? 为什么不用while((i * prime) < inputNumber)代替呢? A long multiplied by an int results in a long ... long乘以int导致long ...

您可以将整数除法的操作数之一乘以1.0以避免结果被整数截断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM