简体   繁体   中英

integer division vs regular division

I need to compare an integer in a loop to the quotient of a long and a long . 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. Btw is this code correct:

primes[i*prime] = 0;

because I am worried that a long * int won't work for an array index.

Thanks so much!

Why not while((i * prime) < inputNumber) instead? A long multiplied by an int results in a long ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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