Results 1 to 7 of 7

Thread: Math help

  1. #1
    Member
    Join Date
    Oct 2003
    Location
    Exiled in Korea
    Posts
    58
    Thanks
    0
    Thanked
    0 times in 0 posts

    Math help

    I`m stuck in Korea in my apartment, so anybody want to explain this to me


    The sum is

    X = 22

    X + 3 / (++X + 2)


    Now when I work it out, i do the following

    X + 3 / (23 + 2) (23 because its X + 1 basicly)
    X + 3 / (25)
    X + .12
    22.12

    Now the answer according to C++ should be
    23

    I wish I had studied in math alot more all those years ago!

    Idiots answer and theory please and any links to a good program that can explain math problems in steps so i can see where I am going wrong!


    Thanks

  2. #2
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    Make sure X is a float, and not an int. If I'm not mistaken, int types ignore anything beyond the decimal point, which could explain why you get that weird answer (It considers 3 / 25 to be > 0, so makes it 1).

    Secondly, I suspect that it's really 3 / (22 + 2), or, 3 / 24. Again, if I recall correctly, "++X" increases X by one after the rest of the code is executed. "X++" would increase it by one beforehand, which is what I think you want.

    So:

    X = 22;
    Z = X + 3 / (++X + 2);

    // Z = 22.125 if it is a float, 23 if not.
    // X = 23

    X = 22;
    Z = X + 3 / (X++ + 2);

    // Z = 22.12 if it is a float, 23 if not.
    // X = 23


    Hope that helps.

  3. #3
    Member
    Join Date
    Oct 2003
    Location
    Exiled in Korea
    Posts
    58
    Thanks
    0
    Thanked
    0 times in 0 posts
    Okay its an Int,

    so basicly my math is right but instead of 22.125 it rounds up (not down) and makes it 23...


    Great, I thought I was going crazy.


    Just for referance the code is... (with x being int)
    Cut & Paste
    ----------------------------------------------
    x = 22;
    answer = x + 3 / (++x + 2);
    ----------------------------------------------
    so the answer is 22.125 rounded up.

    Ints always round up. (I guess you are saying).

    Thanks for the help.

  4. #4
    Member
    Join Date
    Oct 2003
    Location
    Exiled in Korea
    Posts
    58
    Thanks
    0
    Thanked
    0 times in 0 posts
    Okay think i have it a bit more in my head

    x = 22;
    answer = x + 3 / (++x + 2);


    X value is 22


    (++X + 2)

    Inceases X value to 23 for the function

    so it is 23 + 3 / 25
    23 + .125

    its a Int so it rounds it down (that makes more sense)

    so its 23.

    That right??

  5. #5
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    Sorry, I got it the wrong way around.

    X = 22;
    answer = X + 3 / (++X + 2);


    is the same as

    X = 22;
    X = X + 1; // X = 23 now
    answer = X + 3 / (X + 2); // answer = 22.12



    However:

    X = 22;
    answer = X + 3 / (X++ + 2);


    is the same as

    X = 22;
    answer = X + 3 / (X + 2); // answer = 22.125
    X = X + 1; // X = 23 now

  6. #6
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    Originally posted by eldren

    Secondly, I suspect that it's really 3 / (22 + 2), or, 3 / 24. Again, if I recall correctly, "++X" increases X by one after the rest of the code is executed. "X++" would increase it by one beforehand, which is what I think you want.
    That's the wrong way around.

    ++x will increment x and then evaluate the rest of the expression.

    x++ will evaluate the expression then increment x.
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  7. #7
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    Originally posted by DaBeeeenster
    That's the wrong way around.

    ++x will increment x and then evaluate the rest of the expression.

    x++ will evaluate the expression then increment x.
    *prods* See my 10:02 post? But, yes, I did get it wrong, first time round.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •