Mathematics of intergers, doubles, floats, etc.

This is a discussion on Mathematics of intergers, doubles, floats, etc. within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm a beginning C++ programming student coming up on my first test. I'm sure this is a simple enough ...

  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
    21,893
    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.
    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
    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
    21,893
    Somewhat, but there is also the issue of type, e.g., 5 * 2.0 evaluates to 10.0, not 10.
    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

  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,308
    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,429
    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%.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    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, 02: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, 03:53 PM

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