Thread: Mathematics of intergers, doubles, floats, etc.

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Mathematics of intergers, doubles, floats, etc.

    Hello. I'm a beginning C++ programming student coming up on my first test. I'm sure this is a simple enough question. Basically i'm not sure for this particular question how the products of the formulas would appear. If someone could explain and answer these formulas, any help would be so greatly appreicated.


    What are the values of the following expressions? In each line, assume
    that
    double x = 2.5;
    double y = -1.5;
    int m = 18;
    int n = 4;
    i. x + n * y − (x + n) * y
    ii. m/n + m%n
    iii. 5 * x − n/5

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is a matter of substituting the values of those variables, and then evaluating the resulting expressions. You can do it on paper like how you would simplify math expressions.
    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

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    so it behaves just like regular mathematics? such as 5 *2.0= 10 and 2.5*2=5.0?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Somewhat, but there is also the issue of type, e.g., 5 * 2.0 evaluates to 10.0, not 10.
    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

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    oh ok, so does behave just like common mathematics with a few tweaks. Thank you

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Not quite:

    I don't think Laserlights example quite gets across the importance of the type as well as this does...
    Operations on ints give int results. Operations containing float or double operands have float or double results. E.g.
    8/5 is 1
    whereas
    8.0/5.0 is 1.6
    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"

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    Not quite:

    I don't think Laserlights example quite gets across the importance of the type as well as this does...
    Operations on ints give int results. Operations containing float or double operands have float or double results. E.g.
    8/5 is 1
    whereas
    8.0/5.0 is 1.6
    To give one more example in that vein, 8.0/5 is also 1.6. That occurs in this expression because 5 is converted to a value of type double (the same type as 8.0) before performing the division.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The rules are a little tricky, but here is a few guidelines:
    - In an expression, the expression will be promoted to the largest type present in the expression. So, if you have, say, a short and an int, the expression will be an int.
    - If, in an expression, there is a floating point type (float, double) and a non-floating point type, the expression will be promoted to a floating point type.

    Thus, 8/5 is (int/int), and therefore is 1.
    8.0/5 is (double/int), thus, the int is promoted to a double and the expressions becomes 8.0/5.0 (double/double), thus the result is a double and becomes 1.6.

    Beware that expressions are performed on a operation basis.
    Therefore, 8/5 + 1.0 is 2.0, because 8/5 is evaluated to an int, promoted to a double and added to the 2.0.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A doubt about precision in Floats
    By avi2886 in forum C Programming
    Replies: 3
    Last Post: 11-27-2009, 03:05 PM
  2. Double's and floats, i need their bytes.
    By Florian in forum C++ Programming
    Replies: 26
    Last Post: 07-08-2008, 05:42 PM
  3. parse doubles from socket read?
    By willy in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:32 AM
  4. a few opengl(or general programming) questions
    By linuxdude in forum Game Programming
    Replies: 20
    Last Post: 06-14-2004, 07:47 AM
  5. floats and doubles
    By volk in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 04:53 PM