Range of a double integer in C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 174
Reputation: tech291083 is an unknown quantity at this point 
Solved Threads: 0
tech291083 tech291083 is offline Offline
Junior Poster

Range of a double integer in C++

 
0
  #1
Apr 20th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Range of a double integer in C++

 
0
  #2
Apr 20th, 2007
just add .0 after the const as in this example and it will be ok

double microsoft=4294967295.0;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 174
Reputation: tech291083 is an unknown quantity at this point 
Solved Threads: 0
tech291083 tech291083 is offline Offline
Junior Poster

Re: Range of a double integer in C++

 
0
  #3
Apr 22nd, 2007
Originally Posted by Ancient Dragon View Post
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Range of a double integer in C++

 
0
  #4
Apr 22nd, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 174
Reputation: tech291083 is an unknown quantity at this point 
Solved Threads: 0
tech291083 tech291083 is offline Offline
Junior Poster

Re: Range of a double integer in C++

 
0
  #5
Apr 22nd, 2007
Originally Posted by Ancient Dragon View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Range of a double integer in C++

 
0
  #6
Apr 22nd, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 174
Reputation: tech291083 is an unknown quantity at this point 
Solved Threads: 0
tech291083 tech291083 is offline Offline
Junior Poster

Re: Range of a double integer in C++

 
0
  #7
Apr 22nd, 2007
Originally Posted by ~s.o.s~ View Post
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...
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Range of a double integer in C++

 
0
  #8
Apr 22nd, 2007
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... ;-)
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Range of a double integer in C++

 
0
  #9
Apr 22nd, 2007
Originally Posted by tech291083 View Post
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...
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Range of a double integer in C++

 
0
  #10
Apr 22nd, 2007
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC