Solved

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

Posted on 2008-10-12
9
214 Views
Last Modified: 2013-12-14
why is it that, (25%3/3.0) is  0.333333

which operator takes precedence?
0
Comment
Question by:Emmatee1
  • 5
  • 2
9 Comments
 
LVL 12

Accepted Solution

by:
jazzIIIlove earned 500 total points
ID: 22698222
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
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 22698224
high to low order:
1)...
2)...
3)...
0
 
LVL 13

Expert Comment

by:josgood
ID: 22698234
% 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Expert Comment

by:DrunkenELF
ID: 22698258

Dear emmatee,

[/] Division has precedence than modulus [%]

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

Cheers!
 D-ELF
0
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 22698296
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
 
LVL 13

Expert Comment

by:josgood
ID: 22698368
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
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 22698771
<<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
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 22772066
come on...
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
importing  maven Project 5 288
What is the best editor for PowerShell scripts? 7 95
regex. How to include lines between 10 83
eclipse apache tomcat admin console 52 97
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question