String Printing as float number :D

Hello.. please refer to this for more info ..

http://www.cplusplus.com/forum/beginner/12300/

I'm in front of a problem that I'm supposed to print the result of :
5.1234 ^ 15
the result is :
43992025569.928573701266488041146654993318703707511666295476720493953024
using { cout<<variable; } where variable holds the result ..
the problem is the number is printed by scientefic way (exponential) or approximated
someone told me that i can store the value in a string and output the string itself, but gave no further explination ..

how can i do that ??

thanks
There is no atraitforward way of doing it since 43+ billion clearly exceeds the limmits of all datatypes in C++

In any other case you could use:
 
class stringstream


It is a class defined in <sstream> used for reading numbers into a string

You normally would read the value of a number to a string as follows:

1
2
3
4
5
6
/* #include <cmath>
 * #include <sstream> */
std::stringstream dbf;
dbf<< (long double) pow(5.1234, 15.0);
std::string variable = dbf.str();
std::cout << variable << '\n';


However the overflow problem would cause it to be negative.
Last edited on
I doubt it. Referring to jsmith's post in the link you provided:

Well, you can't even compute the number to that many digits of precision, let alone output it. Your problem is in the computation of the number, not in the output of it.


Then there is this library: http://gmplib.org/ ... as posted by someone kinley.

[edit]
Even still, I suppose it can't hurt try: http://www.cplusplus.com/forum/articles/9645/
Last edited on
Just to add:
cout << someting;
and
1
2
3
stringstream ss;
ss << something;
cout << ss.str();
Will give the same result.

As we have already said: You CAN'T have that precision with C++ standard library
Topic archived. No new replies allowed.