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/articles/article.asp?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.

#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

just add .0 after the const as in this example and it will be ok double microsoft=4294967295.0;

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/articles/article.asp?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.

#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 :-O 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

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 :-O 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.

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... ;-)

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...

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:

#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' ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.