| | |
Range of a double integer in C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 174
Reputation:
Solved Threads: 0
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.
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
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.
c Syntax (Toggle Plain Text)
#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
Last edited by Ancient Dragon; Apr 20th, 2007 at 8:09 am. Reason: modified code tags to show line numbers
•
•
Join Date: Oct 2006
Posts: 174
Reputation:
Solved Threads: 0
•
•
•
•
just add .0 after the const as in this example and it will be ok
double microsoft=4294967295.0;
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)
#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; }
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
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.
•
•
Join Date: Oct 2006
Posts: 174
Reputation:
Solved Threads: 0
•
•
•
•
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 zerosIt 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
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.
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.
•
•
Join Date: Oct 2006
Posts: 174
Reputation:
Solved Threads: 0
•
•
•
•
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.
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...
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
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)
stm << fixed << setprecision(200) << number << '\n' ;
see this example:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <limits> #include <typeinfo> #include <iomanip> using namespace std ; template<typename T> void show_limits() { typedef numeric_limits<T> limits ; cout << "********************* type " << typeid(T).name() << " **************************\n" ; cout << "smallest nonzero denormalized value: " << limits::denorm_min() << '\n' ; cout << "allows denormalized values? " << boolalpha << (limits::has_denorm==denorm_present) << '\n' ; cout << "difference between 1 and the smallest value greater than 1: " << limits::epsilon() << '\n' ; cout << "maximum rounding error: " << limits::round_error() << '\n' ; cout << "base (radix) used for the representation: " << limits::radix << '\n' ; cout << "minimum value of exponent (radix): " << limits::min_exponent << '\n' ; cout << "approximate minimum value of exponent (decimal): " << limits::min_exponent10 << '\n' ; cout << "maximum value of exponent (radix): " << limits::max_exponent << '\n' ; cout << "approximate maximum value of exponent (decimal): " << limits::max_exponent10 << '\n' ; cout << "minimum normalized value: " << limits::min() << '\n' ; cout << "maximum normalized value: " << limits::max() << "\n\n" ; } int main() { show_limits<float>() ; show_limits<double>() ; show_limits<long double>() ; double x = 1.0e+20, y = 1.0e-20, z = -1.0e+20 ; cout << scientific << "x+y+z: " << x+y+z << '\n' ; cout << "x+z+y: " << x+z+y << '\n' ; cout << fixed << setprecision(50) << "x+y+z: " << x+y+z << '\n' ; cout << "x+z+y: " << x+z+y << '\n' ; }
Last edited by vijayan121; Apr 22nd, 2007 at 12:22 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: mulitplication of two huge integers
- Next Thread: problem with getline
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






