0

In the following code the expression can not be evaluated since the compiler gives me an error

#include <iostream>
int main()
{
    std::cout<< "Welcome\nto\n\nc++!\n";
   // std::cout<< "to c++ ! \n";
   double a=2;
   double b=2;
   double c=3;
   double d=4;
   double e=5;
   double f=6;
   double sum;
  sum=(a*b%c+d/e-f); // error: invalid operand of  types double and double to binary operators%
   std::cout <<"Sum is:" << (float) sum;
    return 0;
}

I know that the fmod(x,y) function can be used on doubles but the expression won't remain the same,is there some other way I can do it or I need to make an expression evaluator myself.If so then how?I am new to C++ please help and why isn't the compiler intelligent enough though?

  • What do you mean by "won't remain the same"? You just rewrite it to use `fmod()` instead of `%`; [one is prefix notation, the other is infix](http://www.cs.man.ac.uk/~pjj/cs2121/fix.html). – DanielKO Oct 17 '13 at 08:58
  • because that will alter the order of precedence, it will calculate the mod `fmod(b,c)`before multiplying `a*b`while in BODMAS we move from left to right – Romantic Electron Oct 17 '13 at 09:41
  • By "rewrite it to use `fmod()`" I meant you rewrite while maintaining the same meaning. Did you check the link I posted? `a + b * c + d` can be rewritten as `add(add(a, (mul(b, c)), d)`. Don't just shove `fmod` in, figure out the order of the evaluation and nest the expressions properly. – DanielKO Oct 20 '13 at 07:57

1 Answers1

0

An binary operator takes two (= binary) arguments (like a function can), so the expression 5 + 4 could just as easily be written with some function add(5, 4).

Notice the error message you are getting, keywords: "invalid operand binary operator %"

So when you want to use fmod you want to (double check the documentation) and decide what is the right and left hand operand in your expression. The output is dependent on how you write the expression, here is two possible ways:

fmod(a*b, c) + d/e-f This is the correct order of precedence the way you have written it.

If however, we assume you want everything left of the modulus to be numerator and everything right of modulus to be denominator, we could write it as such:

fmod(a*b, c+d/e-f) This would not be equivalent to your original expression.

Notice some documentation for this function where it says parameters.

In C++, there is a minor expectation that the programmer have the intelligence, not the compiler. It is this way so that programmers have more explicit control over how the code behaves.

Leonardo
  • 1,452
  • 3
  • 15
  • 26
  • Insinuating the OP lacks intelligence is not constructive. Also, your code (`fmod(a*b , c+d / e-f)`) doesn't evaluate the expression the same way as the original (`a*b%c+d/e-f`), as `%` has higher precedence than addition. Who's lacking intelligence now? – DanielKO Oct 17 '13 at 08:54
  • @DanielKO It is hard to tell the intelligence of anyone based only on how and what they write, and I tried to address the misunderstandings as I interpreted them. I only answer the question's here, where is the insult please be as explicit as I have. – Leonardo Oct 17 '13 at 09:03