Thread: Question about math in immediate mode

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    11

    Question about math in immediate mode

    I'm trying to sort out an issue I'm having with an equation in C. I was playing around with the order of operations, and entered the following:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float unknown;
    
        unknown = 5 * 2 / 6 * 6 / 2;
        printf("The answer is %f",unknown);
        return(0);
    }
    If I do the problem with a calculator, 5 * 2 / 6 * 6 / 2 resolves to 5. However in C, it resolves to 3. If I understand it, it seems that the equation is handled as an int rather than a float (which drops the remainder of 10/6=1.666666, rounding it to 10/6=1):

    5*2/6*6/2
    10 /6*6/2
    1 *6/2
    6 /2
    =3

    Am I understanding this correctly, and could someone please explain why this rounding is the case, so that I can avoid such a mistake in the future? Thanks

    Michael
    Last edited by gmontag451; 05-01-2018 at 04:09 PM. Reason: removed repeated words

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You're doing integer math which means that there are no fractions. And remember multiplication and division have the same priority and C evaluates the calculation from left to right.

    So 5 * 2 / 6 * 6 / 2 would be

    Code:
    10 / 6 * 6 / 2  (5 * 2 == 10).
    
    1 * 6 / 2  (10 / 6 == 1 (remember no fractions))
    
    6 / 2  (1 * 6 == 6)
    
    3. (6 / 2 == 3)
    it seems that the equation is handled as an int rather than a float (which drops the remainder of 10/6=1.666666, rounding it to 10/6=1):
    Yes the equation is all integer math. But there is no rounding, the "fractional" part is truncated not rounded.
    Last edited by jimblumberg; 05-01-2018 at 04:30 PM.

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    You're doing integer math which means that there are no fractions.
    Ah, I had hoped that by defining 'unknown' as a float would result in the equation being treated as such. Thanks.

    Casting the equation as a float returns the expected result, if such a solution is kosher:


    Code:
        unknown = (float)5 * 2 / 6 * 6 / 2;

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by gmontag451 View Post
    Casting the equation as a float returns the expected result, if such a solution is kosher:
    Kind of. Putting it in front of the first value works for that equation, but you need to watch out. Consider this slightly modified equation:
    Code:
    unknown = (float)5 * 2 / 6 + 6 / 7;
    5 is made a float, so 2 is promoted before the mult, then 6 is promoted before the div.
    But now before the addition the other div needs to be performed before, but both the operands to the div are integers, so it's done in integer arithmetic!

    That one would need to be:
    Code:
    unknown = (float)5 * 2 / 6 + (float)6 / 7;
    And the term "immediate mode" doesn't really pertain here.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gmontag451
    Casting the equation as a float returns the expected result, if such a solution is kosher:
    It is "kosher", but you aren't casting the "equation" (or rather the "expression"), but rather you are just casting one part of it, i.e., the integer literal 5. This subsequently affects the other subexpressions in the entire right hand side expression, hence the type of the entire expression becomes float rather than int. However, this is not always so, e.g.,
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float unknown;
        unknown = (float)5 * 2 + 3 / 2;
        printf("The answer is %f\n", unknown);
        return 0;
    }
    The output you would get would be along the lines of:
    Code:
    The answer is 11.000000
    The reason is that the cast of 5 to float only affects the subexpression "(float)5 * 2", because the precedence of * and / is higher than that of + so this part of the expression is grouped together, and the other part "3 / 2" is grouped together separately. Thus, the type of the subexpression "(float)5 + 2" is float because when you have a float operand and an int operand, the usual arithmetic conversions causes the int operand to be converted to float, and the result to be of type float. However, the type of the subexpression and operands of "3 / 2 " remains int, hence integer division was performed.

    Contrast this with your expression "(float)5 * 2 / 6 * 6 / 2", where * and / have the same level of precedence so they are all grouped together, so with left to right association you end up with the equivalent of "((((float)5 * 2) / 6) * 6) / 2", and so each pair of operands has one operand of type float and the other of type int, resulting in conversion to float each time.
    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

  6. #6
    Registered User
    Join Date
    May 2018
    Posts
    11
    Quote Originally Posted by john.c View Post
    And the term "immediate mode" doesn't really pertain here.
    Quote Originally Posted by laserlight View Post
    "equation" (or rather the "expression"),
    Words never were my thing

    Thank you both for the info on casting to float. I suppose one wouldn't normally be working with numbers in this way, but still, I learned something today!

    I want to thank the three of you for your help and patience. It's not always this way on certain other sites, and I really appreciate it.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    By the way when using constants if you want floating point math you would be better using floating point constants instead of the integer constants or casting.
    Code:
    unknown = 5.0 * 2.0 / 6.0 * 6.0 / 2.0;

  8. #8
    Registered User
    Join Date
    May 2018
    Posts
    11
    Quote Originally Posted by jimblumberg View Post
    By the way when using constants if you want floating point math you would be better using floating point constants instead of the integer constants or casting.
    Awesome, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2010, 01:35 PM
  2. Replies: 4
    Last Post: 09-16-2006, 07:11 PM
  3. Question about mode 13h
    By myk_raniu in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2005, 01:59 PM
  4. LaTeX: Accents in math mode
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 04-20-2005, 09:37 AM
  5. Math Question
    By DarkEldar77 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2001, 12:52 PM

Tags for this Thread