Thread: C program :Calculation

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    C program :Calculation

    I fail to understand why this program prints -8


    Code:
    #include <stdio.h>
    
    int main(void) {
    
    
       int x=2;
       int y=3;
       int z=4;
       
       x*=-2*(y+z)/3;
       printf("result %d",x);
    
     return 0;
    }
    
    my understanding :  
    x*=-2*(y+z)/3;
    => x=x*{-2*(y+z)/3}
    =>x=2*{-2*(3+4)/3}
    =>x=2*{-2*7/3}
    =>x=2*{-2*2.33333....}
    =>x=2*(-4.66666666667)
    =>x=-9.33333333334
    since x is intger ..I expect it to print -9 here (leaving the fractional parts)

    But it prints -8

    How ?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You have to do everything in the right order and you have to do each operation as an integer operation, truncating as you go:

    x = x * (-2 * (y + z) / 3)
    2 * (-2 * (3 + 4) / 3)
    2 * (-2 * 7 / 3)
    2 * ( -14 / 3)
    2 * -4
    -8
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    You have to do everything in the right order
    what is the right order?

    does it not follow Math rules? in Math rules you Divide first and then multiply.

    So as per Math rule it should be
    (-2 * 7 / 3)
    => -2*(7/3) //divide first

    But you wrote
    (-2 * 7 / 3)
    => -14/3

    you multiplied first.

    hence the confusion.

    Could you please clarify this part.
    Last edited by Stream; 04-28-2018 at 12:57 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    You also need to be aware of C operator precedence and associativity.
    C Operator Precedence - cppreference.com

    After the ( ) have been resolved, the -2 * 7 / 3 part is evaluated left to right, since both * and / have equal precedence.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Stream View Post
    does it not follow Math rules? in Math rules you Divide first and then multiply.
    That is not a rule in mathematics... just sayin'

  6. #6
    Registered User
    Join Date
    Apr 2018
    Posts
    2
    It's nothing to do with order, multiply or division first will get the same result; the reason result became -8 is about type conversion, division operator doesn't always return float type.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Quote Originally Posted by Hodor View Post
    That is not a rule in mathematics... just sayin'
    why not ? Its the Math rule ..from left to right ....divide first and multiplication second.
    see this
    C program :Calculation-bodmas-png

    Source: Ordering Mathematical Operations, BODMAS | SkillsYouNeed

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Stream
    Its the Math rule ..from left to right ....divide first and multiplication second.
    The exact rules depend on the precedence conventions adopted, even amongst mathematicians, e.g., some might rank division as having higher precedence, others might rank division and multiplication as having equal precedence, and the precedence convention may differ where the multiplication is implicit rather than expressed with an explicit operator.

    Anyway, while that is interesting mathematical trivia, in programming we have something similiar: the rules of precedence are determined by the rules of the language, hence it can differ amongst different programming languages, and does not need to follow any mathematical convention, although typically programming languages try to keep to some mathematical convention in order to make use of the mathematical background knowledge of programmers and would-be programmers. Hence, C has precedence rules that place division and multiplication as having equal precedence, because that is a convention from mathematics, even if the convention that you learnt is different.

    Quote Originally Posted by Stream
    Note that your source actually states the other convention, i.e., "Multiplication and division rank equally".
    Last edited by laserlight; 04-29-2018 at 03:18 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by Stream View Post
    why not ? Its the Math rule ..from left to right ....divide first and multiplication second.
    see this
    C program :Calculation-bodmas-png

    Source: Ordering Mathematical Operations, BODMAS | SkillsYouNeed
    They improved BODMAS by adding color, but it is still a subtile hint--division and multiplication are equal precedence, as are addition and subtraction.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    In an alternative reality, BOMDAS rules and we're discussing multiplying first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program for calculation
    By tommytmh in forum C Programming
    Replies: 8
    Last Post: 12-03-2011, 07:50 AM
  2. Calculation in C Program
    By Jpeg6 in forum C Programming
    Replies: 12
    Last Post: 09-17-2010, 11:06 AM
  3. Program calculation problems?
    By rebel in forum C++ Programming
    Replies: 7
    Last Post: 11-28-2005, 03:31 PM
  4. help with pay calculation program
    By shugstone in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 09:38 AM

Tags for this Thread