x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 234

# why is ( X = 25%3/3.0 ) is 0.3333333. which operator takes precedence?

why is it that, (25%3/3.0) is  0.333333

which operator takes precedence?
0
Emmatee1
• 5
• 2
1 Solution

Commented:
1) ()
2) / % same order, so left to right
3) = it is right to left

suppose, we miswrite:
double x = (25/3%3.0);
or
int x = (25/3%3.0);

You got this:
'%' : illegal, right operand has type 'const double'

i think, it's clear...

Best regards...
0

Commented:
high to low order:
1)...
2)...
3)...
0

Commented:
% and / have the same precedence
See http://www.cppreference.com/wiki/operator_precedence, for example

Since they have the same precedence, they have left-to-right associativity.

So 25 % 3 = 1.0 is evaluated first.
Then 1.0 / 3.0
0

Commented:

Dear emmatee,

[/] Division has precedence than modulus [%]

FYI...
the order is
*  /  % Multiplication/division/modulus

Cheers!
D-ELF
0

Commented:
well...

# include <stdio.h>

int main()

{

int a = (12*2/3); //8
int b = (12/2*3); //18
int c = (12*2%6); //0
int d = (12%5*2); //4
int e = (12/2%3); //0
int f = (12%5/2); //1
printf("X: %d\n",a);
printf("X: %d\n",b);
printf("X: %d\n",c);
printf("X: %d\n",d);
printf("X: %d\n",e);
printf("X: %d\n",f);

return 1;
}

So, the precedence of operators is same precedence group but changed because from left-to-right process of the operators in the same precedence group...
0

Commented:
Agreeing with jazzIIIlove and quoting from the standard:

5.6  Multiplicative operators                               [expr.mul]

1 The multiplicative operators *, /, and % group left-to-right.
multiplicative-expression:
pm-expression
multiplicative-expression * pm-expression
multiplicative-expression / pm-expression
multiplicative-expression % pm-expression
0

Commented:
<<the order is
<<*  /  % Multiplication/division/modulus

No...

To clear the issue, they are in the SAME precedence group and the order is evaluated from left to right, if more than one is existent...

Best regards...
0

Commented:
come on...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.