944,175 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 31098
  • C++ RSS
Apr 20th, 2007
0

Range of a double integer in C++

Expand Post »
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.

  1.  
  2. #include <iostream>
  3. //Testing the actual range of a double integer........
  4. int main()
  5. {
  6. using namespace std;
  7. double microsoft=4294967295;
  8. double intel=55555557777777770;
  9. double total=microsoft+intel;
  10. cout<<"The total is: "<<total<<" dollars\n";
  11. return 0;
  12. }


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
Last edited by Ancient Dragon; Apr 20th, 2007 at 8:09 am. Reason: modified code tags to show line numbers
Reputation Points: 15
Solved Threads: 0
Junior Poster
tech291083 is offline Offline
181 posts
since Oct 2006
Apr 20th, 2007
0

Re: Range of a double integer in C++

just add .0 after the const as in this example and it will be ok

double microsoft=4294967295.0;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 22nd, 2007
0

Re: Range of a double integer in C++

just add .0 after the const as in this example and it will be ok

double microsoft=4294967295.0;
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 200 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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. double microsoft=1(followed by 308 zeroes).00;
  9.  
  10. // something like 100000000000.00
  11.  
  12. double intel=0.0;
  13.  
  14. double total=microsoft+intel;
  15.  
  16. cout<<"The total is: "<<total<<" dollars\n";
  17.  
  18. cin.ignore();
  19.  
  20. return 0;
  21.  
  22. }
Reputation Points: 15
Solved Threads: 0
Junior Poster
tech291083 is offline Offline
181 posts
since Oct 2006
Apr 22nd, 2007
0

Re: Range of a double integer in C++

I know of no way to do what you want with standard C/C++ libraries. You do have some options: I think boost libraries may help you, or you could save the data in strings and write your own functions to manipulate them. But I honestly don't understand the point of wanting to print out a number with 200 zeros It might make your project look neat but it will be pretty meaningless -- who in his right mind is going to even attemp to read such a number -- it would take half a sheet of paper just to print it. Seeing 1.0001e+271 is a lot more understanding then something like this ungodly thing: 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Last edited by Ancient Dragon; Apr 22nd, 2007 at 10:47 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 22nd, 2007
0

Re: Range of a double integer in C++

I know of no way to do what you want with standard C/C++ libraries. You do have some options: I think boost libraries may help you, or you could save the data in strings and write your own functions to manipulate them. But I honestly don't understand the point of wanting to print out a number with 200 zeros It might make your project look neat but it will be pretty meaningless -- who in his right mind is going to even attemp to read such a number -- it would take half a sheet of paper just to print it. Seeing 1.0001e+271 is a lot more understanding then something like this ungodly thing: 1000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000
Hi,

Thanks,

I fully agree with you. But I just want to see if it is possible. Actually I am in the process of learning c++ on my own and just curious to see if there is something that can be done to print all the zeroes in a number.
Last edited by tech291083; Apr 22nd, 2007 at 10:54 am.
Reputation Points: 15
Solved Threads: 0
Junior Poster
tech291083 is offline Offline
181 posts
since Oct 2006
Apr 22nd, 2007
0

Re: Range of a double integer in C++

You have to realize that floating point numbers are nothing but normal 32/64 bit datatypes with special interpretation of the bit pattern. If you are really interested, read this.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 22nd, 2007
0

Re: Range of a double integer in C++

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
You have to realize that floating point numbers are nothing but normal 32/64 bit datatypes with special interpretation of the bit pattern. If you are really interested, read this.
Thanks,

Fully agree with your reply, but I just want to see if it is possible to tell the compiler to display the number/output in a manner (with all the zeros/digits in it) that one wants to. Cheers...
Reputation Points: 15
Solved Threads: 0
Junior Poster
tech291083 is offline Offline
181 posts
since Oct 2006
Apr 22nd, 2007
0

Re: Range of a double integer in C++

Though I haven't as such tested it, but I think this is what you are looking for. If not, then there always are better libraries/languages out there... ;-)
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 22nd, 2007
0

Re: Range of a double integer in C++

Click to Expand / Collapse  Quote originally posted by tech291083 ...
Thanks,

Fully agree with your reply, but I just want to see if it is possible to tell the compiler to display the number/output in a manner (with all the zeros/digits in it) that one wants to. Cheers...
C++ Syntax (Toggle Plain Text)
  1.  
  2. stm << fixed << setprecision(200) << number << '\n' ;
would do what you want. obviously this will not give you an accuracy of 200 digits after the decimal.
see this example:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <limits>
  3. #include <typeinfo>
  4. #include <iomanip>
  5. using namespace std ;
  6.  
  7. template<typename T> void show_limits()
  8. {
  9. typedef numeric_limits<T> limits ;
  10. cout << "********************* type " << typeid(T).name()
  11. << " **************************\n" ;
  12. cout << "smallest nonzero denormalized value: "
  13. << limits::denorm_min() << '\n' ;
  14. cout << "allows denormalized values? " << boolalpha
  15. << (limits::has_denorm==denorm_present) << '\n' ;
  16. cout << "difference between 1 and the smallest value greater than 1: "
  17. << limits::epsilon() << '\n' ;
  18. cout << "maximum rounding error: " << limits::round_error() << '\n' ;
  19. cout << "base (radix) used for the representation: "
  20. << limits::radix << '\n' ;
  21. cout << "minimum value of exponent (radix): "
  22. << limits::min_exponent << '\n' ;
  23. cout << "approximate minimum value of exponent (decimal): "
  24. << limits::min_exponent10 << '\n' ;
  25. cout << "maximum value of exponent (radix): "
  26. << limits::max_exponent << '\n' ;
  27. cout << "approximate maximum value of exponent (decimal): "
  28. << limits::max_exponent10 << '\n' ;
  29. cout << "minimum normalized value: " << limits::min() << '\n' ;
  30. cout << "maximum normalized value: " << limits::max() << "\n\n" ;
  31.  
  32. }
  33.  
  34. int main()
  35. {
  36. show_limits<float>() ;
  37. show_limits<double>() ;
  38. show_limits<long double>() ;
  39.  
  40. double x = 1.0e+20, y = 1.0e-20, z = -1.0e+20 ;
  41. cout << scientific << "x+y+z: " << x+y+z << '\n' ;
  42. cout << "x+z+y: " << x+z+y << '\n' ;
  43. cout << fixed << setprecision(50) << "x+y+z: " << x+y+z << '\n' ;
  44. cout << "x+z+y: " << x+z+y << '\n' ;
  45. }
Last edited by vijayan121; Apr 22nd, 2007 at 12:22 pm.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 22nd, 2007
0

Re: Range of a double integer in C++

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: mulitplication of two huge integers
Next Thread in C++ Forum Timeline: problem with getline





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC