-2

I don't want to know what the remainder is, I just want to know if there was a remainder as a boolean value. As such, using the modulo operator is not what I'm looking for. Something in C would be preferable, but any language works.

  • 2
    `(a % b) != 0` is the way to go, although even `a % b` is fine – qrdl Mar 15 '21 at 08:09
  • 'using the modulo operator is not what I'm looking for'....I beg to differ. Your request is like 'I need to drive a nail but I don't want to use a hammer':( – Martin James Mar 15 '21 at 08:48
  • Hey, welcome to Stack Overflow. The modulo operator having a non-zero result would be the boolean you are looking for like @qrdl suggested. – Daniel Soutar Mar 15 '21 at 12:13
  • "As such, using the modulo operator is not what I'm looking for." Why not? Too easy, too readable? – Lundin Mar 15 '21 at 15:03

3 Answers3

4

If really you cannot use the remainder operation (homework constraint ?), then you can use

check_remainder = b*(a/b) != a; 

But the remainder use % is the natural way to go.

Damien
  • 4,809
  • 4
  • 15
  • 20
  • This is kind of the definition of how modulo works... see https://godbolt.org/z/rWas1r. Identical machine code. – Lundin Mar 15 '21 at 15:02
  • @Lundin I know. I interpret this exercise as a homework, or a simple challenge: *How to avoid using the remainder `%` operator* ... – Damien Mar 15 '21 at 15:05
  • 1
    Yeah and Google "c remainder without modulo" gives some 5 or so different flavours of your answer here (or the icky iterative approach)... – Lundin Mar 15 '21 at 15:09
2

Use div(), which, hopefully, calculates quotient and remainder with 1 single operation.

#include <stdlib.h>
//...
int a = 42, b = 5;
div_t c = div(a, b);
if (c.rem) /* remainder for a/b is not zero */;
//...
pmg
  • 106,608
  • 13
  • 126
  • 198
  • Is this faster than modulo? I guess my question is really: Is modulo the fastest way to check for divisibility, or is there some faster way to check if a number divides another, forgoing know the remainder? – J Chirinos Mar 18 '21 at 07:57
  • According to my timing, on my low-end Debian computer: **no**. Using `%` or [Damien's expression](https://stackoverflow.com/a/66634535/25324) is about the same and faster than using `div` by a factor of `2`, again, on my computer with preferred compilation settings. – pmg Mar 18 '21 at 08:06
0

You cannot tell if there is a remainder until you calculate such remainder, which means you have to perform the division.

That said, there are some shortcuts you can take but only if the divisor is a power of 2. For example, a division by 2 will have a remainder if the least significant bit is 1. In general, a division by 2 raised to the power n will have a remainder if the least n significant bits have a value other than 0000.....0 (n zeroes).

At the assembly level, some architectures, such as x86 provide you with both the quotient and the remainder as he result of a DIV instruction.

If using the modulus operator (or the division operator which is used internally) is not an option, you can do it the naive way:

unsigned has_remainder (unsigned a, unsigned b)
{
  while (a >= b)
    a -= b;
  return a;
}
mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32