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:
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.
//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";
return0;
}
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
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:
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.
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.
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.
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.
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...
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... ;-)
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...
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.