Data Types

This is a discussion on Data Types within the C++ Programming forums, part of the General Programming Boards category; Hello. Struggling with getting my head around data types. Clearly a newcomers question, but why does this produce 4.2*10^9? Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Data Types

    Hello. Struggling with getting my head around data types. Clearly a newcomers question, but why does this produce 4.2*10^9?
    Code:
    // playing with variables
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int a(2);
        unsigned int b(3);
        float result;
        result=a-b;
        cout<<result;
        return 0;
    }


    Thanks. Feel free to insult the sheer ignorance.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    When you do the a-b, a is promoted to unsigned int, and so the expression result of -1 overflows to INT_MAX. Your platform has 32-bit ints, so this is 4,294,967,296. Assigned to a float, this becomes 4.29497e9 (you only get 6 significant digits with float).

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    nice one, thanks.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,606
    3 things here.
    Firstly,
    int a(2);
    unsigned int b(3);
    While correct syntax, to some it can be a little confusing, so it's better to do
    int a = 2;
    unsigned int b = 3;
    I think most agree on this.

    Second, it is unwise to do operations on one signed and one unsigned.
    Preferably, both types should be the same, otherwise you can use a cast to make one signed or unsigned, depending on what you want. static_cast should work fine for this.

    Thirdly, you are doing integer subtraction, so the result will be an integer and not a float, so using a float to store the answer is really unnecessary and can actually be a bad thing since your answer might not be what you want due to floating point inconsistencies.
    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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by Elysia View Post
    Second, it is unwise to do operations on one signed and one unsigned.
    Preferably, both types should be the same, otherwise you can use a cast to make one signed or unsigned, depending on what you want. static_cast should work fine for this.

    Thirdly, you are doing integer subtraction, so the result will be an integer and not a float, so using a float to store the answer is really unnecessary and can actually be a bad thing since your answer might not be what you want due to floating point inconsistencies.
    QFE, except the word you were looking for is "inaccuracies", not "inconsistencies".

    Turn your compiler's warning level up to the max and leave it there. Pay attention to everything it tells you.
    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"

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,606
    Right you are.
    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.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by iMalc View Post
    QFE, except the word you were looking for is "inaccuracies", not "inconsistencies".

    Turn your compiler's warning level up to the max and leave it there. Pay attention to everything it tells you.
    "QFE"? There's one I haven't seen before. What's the long form?

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quoted for emphasis. Closely related to QFT.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM

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