Results 1 to 5 of 5

Thread: Range of a double integer in C++

  1. #1

    Range of a double integer in C++

    Hi,

    I have this program in C++ and I am struggling to understand as to why I am not able to store a value in an integer declared in the program as double within the acceptable range of 2.2e-308 to 1.8e308. Here this page also says so in a table:

    http://www.samspublishing.com/articl...p?p=26933&rl=1

    As far as I understand a double in C++ should be able to store a value of more than 308 digits right? Please correct me if I am wrong as I am new to C++ programming. I am on Fedora Core 5 32+ bit OS. Can any one please help me with this? Here is the code: Thanks a lot.


    Code:
    #include <iostream>
    //Testing the actual range of a double integer........
    int main()
    {
    using namespace std;
    double microsoft=4294967295;
    double intel=55555557777777770;
    double total=microsoft+intel;
    cout<<"The total is: "<<total<<" dollars\n";
    return 0;
    }
    Error:

    [root@localhost Prac]# g++ -o exp13 exp13.cpp
    exp13.cpp:9: warning: this decimal constant is unsigned only in ISO C90
    exp13.cpp:10: error: integer constant is too large for ‘long’ type
    [root@localhost Prac]# ./exp13
    The total is: 4.35052e+09 dollars

  2. #2
    Double is a floating point data type, not integer (otherwise it could store a number such as 2.2e-308 !).
    That said, the compiler is complaining about an integer constant being too large. Perhaps you could make it explicitly non integer by expressing it as "55555557777777770.0". However, you are going to lose accuracy at some point as you can not get 308 digits stored, you can get a number of that magnitude stored to a number of significant digits.

  3. #3
    Quote Originally Posted by pam View Post
    Double is a floating point data type, not integer (otherwise it could store a number such as 2.2e-308 !).
    That said, the compiler is complaining about an integer constant being too large. Perhaps you could make it explicitly non integer by expressing it as "55555557777777770.0". However, you are going to lose accuracy at some point as you can not get 308 digits stored, you can get a number of that magnitude stored to a number of significant digits.
    Hi,

    Thanks for the reply, but I have made a terrible blunder here. What actually I want to do with the program is, get an output in double data type not double integer. As I mentioned earlier the double data type can store any value with a between the acceptable range of 2.2e-308 to 1.8e308. Here this page also says the same in a table:

    http://www.samspublishing.com/articl...p?p=26933&rl=1


    so as far as I understand a double in C++ should be able to store a value of more than 308 digits right? And this value could be any real number meaning negative or positive with /without decimal points. So with this program I am just trying to test whether I can add two numbers and the their total could be processed by the compiler considering the fact that both the numbers (double data type) are fairly large and try to get as close as possible to the limit of the double data type ie 2.2e-308 to 1.8e308. I also want the result to be displayed as a number with all the zeroes in it (no matter how many there are 100, 200, 300 or more than that) rather than something like this 1.0001e+271. How do I achieve this? Please tell me if I am doing it right. Thanks a lot.

    Code:
    #include <iostream> 
     
    using namespace std;
     
    int main()
    {
     
    double microsoft=1(followed by 308 zeroes).00;
     
    // something like 100000000000.00
     
    double intel=0.0;
     
    double total=microsoft+intel;
     
    cout<<"The total is: "<<total<<" dollars\n";
     
    cin.ignore();
     
    return 0;
     
    }

  4. #4
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811
    Depending on your architecture, the size of double is around 64 bits, this means it can hold a value of 2^64 which comes no where near your claimed value consisting of 308 digits.

    The claimed article confirms this
    Listing 3.1 Determining the Size of Variable Types on Your Computer
    Code:
      0:  #include <iostream>
      1:  
      2:  int main()
      3:  {
      4:     using std::cout;
      5:  
      6:     cout << "The size of an int is:\t\t"    
      7:        << sizeof(int)    << " bytes.\n";
      8:     cout << "The size of a short int is:\t" 
      9:        << sizeof(short)  << " bytes.\n";
     10:     cout << "The size of a long int is:\t"  
     11:        << sizeof(long)   << " bytes.\n";
     12:     cout << "The size of a char is:\t\t"    
     13:        << sizeof(char)   << " bytes.\n";
     14:     cout << "The size of a float is:\t\t"   
     15:         << sizeof(float)  << " bytes.\n";
     16:     cout << "The size of a double is:\t"    
     17:        << sizeof(double) << " bytes.\n";
     18:     cout << "The size of a bool is:\t"      
     19:        << sizeof(bool)   << " bytes.\n";
     20:  
     21:     return 0;
     22:  }
    Output
    Code:
    The size of an int is:          4 bytes.
    The size of a short int is:     2 bytes.
    The size of a long int is:      4 bytes.
    The size of a char is:          1 bytes.
    The size of a float is:         4 bytes.
    The size of a double is:        8 bytes.
    The size of a bool is:          1 bytes.
    Now with signed/unsigned in mind, you need to know that one bit is used as the signed bit which in total gives it a max value of +/- 2^63
    when considdering it as signed.

    Like Pam said, double is a floating point data type, meaning it is designed to ease floating point arithmetic, not meaning giving more storage space for your integer calculations.

    In my mind "Teaching yourself <anything> in 21 days" isn't the best learning experience, I would considder using a real book, like Osborne "The complete C++ Reference"
    And do keep in mind, C++ is Teaching yourself programming in 10 years
    Don't worry Ma'am. We're university students, - We know what We're doing.
    'Ruiat coelum, fiat voluntas tua.'
    Datalogi - en livsstil; Intet liv, ingen stil.

  5. #5
    Quote Originally Posted by redhead View Post
    Depending on your architecture, the size of double is around 64 bits, this means it can hold a value of 2^64 which comes no where near your claimed value consisting of 308 digits.

    The claimed article confirms thisNow with signed/unsigned in mind, you need to know that one bit is used as the signed bit which in total gives it a max value of +/- 2^63
    when considdering it as signed.

    Like Pam said, double is a floating point data type, meaning it is designed to ease floating point arithmetic, not meaning giving more storage space for your integer calculations.

    In my mind "Teaching yourself <anything> in 21 days" isn't the best learning experience, I would considder using a real book, like Osborne "The complete C++ Reference"
    And do keep in mind, C++ is Teaching yourself programming in 10 years
    Many thnaks... makes sense to me now....

Similar Threads

  1. Double startup problem
    By toastrack in forum Linux - Software, Applications & Programming
    Replies: 0
    Last Post: 11-01-2008, 11:36 AM
  2. Double spacing
    By geeezeit in forum Windows - General Topics
    Replies: 0
    Last Post: 10-25-2006, 01:09 AM
  3. 1 NIC, 1 Router, and an out of range IP address
    By etehigraphy in forum Linux - Hardware, Networking & Security
    Replies: 6
    Last Post: 03-05-2005, 06:59 PM
  4. removed a double post.
    By pbharris in forum Announcements and Suggestions
    Replies: 0
    Last Post: 11-09-2002, 02:13 AM
  5. Am I seeing double??
    By Black666 in forum Linux - General Topics
    Replies: 3
    Last Post: 05-26-2002, 01:19 PM

Bookmarks

Posting Permissions

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