-1

After compiling the entire code, this is the error i got:

[Error] invalid operands of types '__gnu_cxx::__promote_2<int, int, double, double>::__type {aka double}' and 'int' to binary 'operator%'

I have tried type casting for n/2 to int in the last line of the code but still showed the same error:

int rem(int n,int b,int d)
{
    if(n==1)
    {
        return b%d;
    }
    else
    {
        return (pow(rem(n/2,b,d),2)%d);
    }

}
Obsidian
  • 3,719
  • 8
  • 17
  • 30
user173379
  • 21
  • 6
  • "I have tried type casting for n/2 to int in the last line..." please show the code, or is this included in the code? I dont understand what you refer to – 463035818_is_not_an_ai May 17 '19 at 13:04
  • Possibly related: [Why does modulus division (%) only work with integers?](https://stackoverflow.com/q/6102948/580083) – Daniel Langr May 17 '19 at 13:05
  • @formerlyknownas_463035818 Sorry I can't upload the entire code it's huge – user173379 May 17 '19 at 13:10
  • There's enough here for an answer as the OP has supplied the compiler diagnostic. – Bathsheba May 17 '19 at 13:10
  • thats a common misunderstanding, nodody asked for / wants to see your entire code base. What is required is a [mcve], which is just enough code to reproduce the error. Actually you already have one, though I was wondering what you mean with "I have tried type casting" because I cannot see anything like that in your code – 463035818_is_not_an_ai May 17 '19 at 13:11
  • @formerlyknownas_463035818: My comment wasn't targeted at you. – Bathsheba May 17 '19 at 13:14
  • 1
    oh nevermind then :) – 463035818_is_not_an_ai May 17 '19 at 13:14
  • @formerlyknownas_463035818 I'm sorry I was not specific where I type casted. I just tried type casting for n/2 rather than type casting pow function. Now my code works perfectly.Thanks guys – user173379 May 17 '19 at 13:14

1 Answers1

1

The issue is with pow. It returns a double, and using % with non-intergal type arguments is not allowed in C++ (it is in Java by the way).

But, you should not be using pow anyway to square a number.

Writing

auto r = rem(n/2,b,d);
return r * r % d;

is far better. Some folk might prefer (r * r) % d if they like to use superfluous parentheses.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483