Simple math

This is a discussion on Simple math within the C Programming forums, part of the General Programming Boards category; I have this program: Code: #include <stdio.h> int main() { float x; x = 1+2-3*4/5-6+7*8/9; printf("%.2lf\n", 1+2-3*4/5-6+7*8/9); printf("%.2lf\n", x); system("PAUSE"); ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    15

    Simple math

    I have this program:

    Code:
    #include <stdio.h>
    
    int main()
    {
        float x;
    
        x = 1+2-3*4/5-6+7*8/9;
        printf("%.2lf\n", 1+2-3*4/5-6+7*8/9);
        printf("%.2lf\n", x);
    
        system("PAUSE");
        return 0;
    }
    I know in reality the answer could be .82 rounding 2 decimal places.
    however, the output of my current program is

    0.00
    1.00

    I expected the outputs to be the same. Could someone guide me to understanding these outputs, and how I could manipulate the program more to get actual values?

    I hope someone can help, Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    You are using integer division, e.g., 4/5 == 0. Use floating point division by using floating point literals instead, e.g., 4.0f / 5.0f
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    On float numbers You have to put ".0" if is division between numbers

    #include <stdio.h>

    int main()
    {
    float x;

    x = 1+2-3*4.0/5.0-6+7*8.0/9.0;
    printf("%.2lf\n", 1+2-3*4.0/5.0-6+7*8.0/9.0);
    printf("%.2lf\n", x);

    system("PAUSE");
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Is 1+2-3*4/5-6+7*8/9 by itself assumed to be solved using only integers?

    if x is assigned as a float, wouldn't the equation be solved using float numbers?

    It is still a little confusing

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Just remember this: C/C++ works on what it currently was!

    EDIT: I mean that when dividing two integers, it produces an integer. The compiler doesn't read your mind!
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Ah, I found a mistake in the codeing:

    Code:
    printf("%.2lf\n", 1+2-3*4/5-6+7*8/9);
    produces an integer, and using %lf is the wrong syntax for an integer. I assume, when you display an integer as a float, it will always produce a 0. My outputs infact, were 1 and 1.00 after the edit

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Here is another interesting problem:

    Code:
    #include <stdio.h>
    
    int main()
    {
        float x;
        float y;
    
        y = 5;
        x = 1+2-3*4/5.0-6+7*8/9.0;
        
        printf("%.2lf\n", 1+2-3*4/5-6+7*8/9);
        printf("%.2lf\n", x);
    
        printf("%.2lf\n", 5);
        printf("%.2lf\n", y);
    
        system("PAUSE");
        return 0;
    }
    The outputs are:

    0.00
    0.82
    0.82
    5.00

    The third printf statement is an unexpected result. Why is it displaying 0.82?

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    So far my idea is that if there is a statement that has conflicting syntax, but is still compiliable like :

    Code:
    printf("%.2lf\n", x);
    printf("%.2lf\n", 1+2-3*4/5-6+7*8/9);
    it will merely display the last value that worked out.

    Tell me if this is wrong, of if you agree

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    You're still printing two integer expressions using %.2lf.
    Perhaps it's just the compiler protesting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Maybe... it really is compiler-dependant on how printf will act if you pass it a wrong parameter.
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    I tried to program a test to see which variable gets assigned a float in the code:

    Code:
    printf("%.2lf\n", (float)(A+B-C*D/E-F+G*H/I));
    however, I couldn't figure out how to test for this.
    I know not all of them are assigned to floats since the output is 1.00 when using my previous values in the same equation.

    My question now, is which variables is the float casted onto?

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Ludicrous View Post
    I tried to program a test to see which variable gets assigned a float in the code:

    Code:
    printf("%.2lf\n", (float)(A+B-C*D/E-F+G*H/I));
    however, I couldn't figure out how to test for this.
    I know not all of them are assigned to floats since the output is 1.00 when using my previous values in the same equation.

    My question now, is which variables is the float casted onto?
    In the example you gave the calculation is completed then the result is cast to a float value.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I'm going to make a guess here as to what the compiler is doing. When you push an int onto the stack there was still some more on the stack from the last time a double was pushed. The %lf format forces the recent int as well as some of the other data there to be used. So in effect, you are printing garbage left from when more of the stack was used by you. The fact that you pushed a 5 seems to have had little effect on the double... but it probably did of you were to print more decimal places.

    This behaviour is compiler dependent. Also depends on the size differential of double vs. int.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by Ludicrous View Post
    I tried to program a test to see which variable gets assigned a float in the code:

    Code:
    printf("%.2lf\n", (float)(A+B-C*D/E-F+G*H/I));
    however, I couldn't figure out how to test for this.
    I know not all of them are assigned to floats since the output is 1.00 when using my previous values in the same equation.

    My question now, is which variables is the float casted onto?
    You don't yet understand. When you do the formula, the compiler will take each constant or variable for what it is. If two ints are added, multiplied, etc., the intermediate result will be int.

    You cast the result of the expression to float. But by then its value was probably zero anyhow.
    In your case none of the variables get promoted to float... only the RESULT, just before it's handed to the printf is.

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Thank you everyone for answering my questions.

    I don't fully understand the issue with printing an integer as a float (Not that I would intentionally do this), however I know what to look for with my current compiler, if I run across this type of error. I still have a lot of studying to do.

    The order of operations is fairly clear, I have plenty of practice problems to test myself.

    Thanks again

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to do simple math with float and int type
    By demuro1 in forum C Programming
    Replies: 16
    Last Post: 09-24-2008, 11:58 AM
  2. Getting bizarre results from simple math
    By mmyers1 in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2004, 06:39 PM
  3. simple math in msvc++ 6
    By isnan in forum Windows Programming
    Replies: 2
    Last Post: 03-24-2004, 08:28 PM
  4. Simple Math Program
    By RazielX in forum C Programming
    Replies: 3
    Last Post: 03-04-2004, 05:22 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21