+ 9
A problem with pow function with large numbers
Hi everyone, I have a problem using the pow function to return large number, according to cppreference pow function supports long double as argument and return value, but I failed it, with pow function returning zero on each attempt. http://en.cppreference.com/w/cpp/numeric/math/pow I tried using double before, but it overflowed, so I wanted to try long double. Please help me get this to work, code included. Thanks in advance, https://code.sololearn.com/cjP1BGY5fpE3/?ref=app
9 odpowiedzi
+ 18
SL "BLOODY" COMPILER IS DRIVING PEOPLE CRAZY!
Your code works perfectly fine.
#include <iostream>
#include <iomanip>
#include <cmath>
//using namespace std;
int main() {
std::string szname[] = {"Kilobyte (KB)","Megabyte (MB)","Gigabyte (GB)","Terabyte (TB)","Petabyte (PB)","Exabyte (EB)","Zettabyte (ZB)","Yottabyte (YB)","Brontobyte (BB)","Geopbyte (GPB)"};
std::cout << "Bit (Binary Digit, 0 or 1)\n";
std::cout << "Nibble 4 bits (half Byte)\n";
std::cout << "Byte 8 bits\n";
long double num, base = 2;
int i, idx = 0;
// Use power of two for measuring.
for(i = 10; i <= 100; i += 10)
{
num = std::pow(base,i);
// Added
std::cout << std::fixed << std::setprecision(0);
std::cout << "1 " << szname[idx] << " (2^" << i << ") = " << num << " bits" << std::endl;
idx++;
}
}
[http://cpp.sh/83ed]
+ 21
Output on my Windows machine:
long double type capable of holding 1.79769e+308 @ max.
The largest integer power of 10 that is a valid finite floating-point value is 308
And its size is 8 Bytes.
1.23457e+008 ^ 0: 1
1.23457e+008 ^ 1: 1.23457e+008
1.23457e+008 ^ 2: 1.52416e+016
1.23457e+008 ^ 3: 1.88168e+024
1.23457e+008 ^ 4: 2.32306e+032
1.23457e+008 ^ 5: 2.86797e+040
1.23457e+008 ^ 6: 3.54071e+048
1.23457e+008 ^ 7: 4.37124e+056
1.23457e+008 ^ 8: 5.39659e+064
1.23457e+008 ^ 9: 6.66246e+072
1.23457e+008 ^ 10: 8.22526e+080
1.23457e+008 ^ 11: 1.01546e+089
1.23457e+008 ^ 12: 1.25366e+097
1.23457e+008 ^ 13: 1.54773e+105
1.23457e+008 ^ 14: 1.91078e+113
1.23457e+008 ^ 15: 2.35898e+121
1.23457e+008 ^ 16: 2.91232e+129
1.23457e+008 ^ 17: 3.59546e+137
1.23457e+008 ^ 18: 4.43884e+145
1.23457e+008 ^ 19: 5.48005e+153
1.23457e+008 ^ 20: 6.76549e+161
1.23457e+008 ^ 21: 8.35246e+169
1.23457e+008 ^ 22: 1.03117e+178
1.23457e+008 ^ 23: 1.27305e+186
1.23457e+008 ^ 24: 1.57166e+194
1.23457e+008 ^ 25: 1.94032e+202
1.23457e+008 ^ 26: 2.39546e+210
1.23457e+008 ^ 27: 2.95736e+218
1.23457e+008 ^ 28: 3.65106e+226
1.23457e+008 ^ 29: 4.50749e+234
1.23457e+008 ^ 30: 5.5648e+242
1.23457e+008 ^ 31: 6.87012e+250
1.23457e+008 ^ 32: 8.48163e+258
1.23457e+008 ^ 33: 1.04711e+267
1.23457e+008 ^ 34: 1.29273e+275
1.23457e+008 ^ 35: 1.59597e+283
1.23457e+008 ^ 36: 1.97033e+291
1.23457e+008 ^ 37: 2.43251e+299
1.23457e+008 ^ 38: 3.0031e+307
1.23457e+008 ^ 39: 1.#INF
1.23457e+008 ^ 40: 1.#INF
1.23457e+008 ^ 41: 1.#INF
1.23457e+008 ^ 42: 1.#INF
1.23457e+008 ^ 43: 1.#INF
1.23457e+008 ^ 44: 1.#INF
1.23457e+008 ^ 45: 1.#INF
1.23457e+008 ^ 46: 1.#INF
1.23457e+008 ^ 47: 1.#INF
1.23457e+008 ^ 48: 1.#INF
1.23457e+008 ^ 49: 1.#INF
1.23457e+008 ^ 50: 1.#INF
Press any key to continue . . .
+ 21
//========================================
// CodeName : A quick look at pow function
// and long double type
// Done for : Ipang
// By : Babak Sheykhan
// Created : Nov 12, 2017
// Modified : Nov 12, 2017
//========================================
#include <iostream>
#include <cmath>
#include <limits>
using std::cout; using std::cin; using std::endl;
int main() {
long double ld_max = std::numeric_limits<long double>::max();
int exp_max = std::numeric_limits<long double>::max_exponent10;
cout << "long double type capable of holding " << ld_max << " @ max.\n";
cout << "The largest integer power of 10 that is a valid finite floating-point value is "
<< exp_max << endl;
cout << "And its size is " << sizeof(long double) << " Bytes.\n\n";
//////////// cppref /////////
// long double pow( long double base, long double exp ); (3)
// long double pow( long double base, int iexp ); (6) (until C++11)
long double test = 123456789.123456789;
for (int i = 0; i <= 50; ++i)
cout << test << " ^ " << i << ": " << pow(test, i) << endl;
}
+ 20
Online compilers showing an invalid maximum size for long double type
SL code : [https://code.sololearn.com/cyxY0vd01kRX]
cpp.sh code : [http://cpp.sh/82t3z]
Although, they are showing 4932 as a valid power and 16 byte as size of long double, but as you can see, maximum valid power stops @ 307
+ 20
No problem, dear Ipang. ;)
+ 19
I'm using Visual Studio for my daily stuff. Sadly, your code wasn't available and I wrote and arbitrary code to explanation.
+ 3
@Babak, please pardon me, I had problem uploading this question, probably the code link is corrupted on the way, I will edit my original post, I will post again after it is successfully updated.
Really sorry, I should have checked first :)
+ 2
@Babak mate,
Thank you for answering promptly, in Code Playground the code output looks different with your output copy, I guess your compiler is more accurate somehow.
Did you run my code? it only calculated two to some power, the thing is, if overflow was the issue how come it's not returning a number for small power as 10 in the first loop iteration? using double worked, (pow returned value) at least until it overflowed, but long double always return zero.
Can you please show me what I did wrong with the code that pow doesn't return a value as expected? I'm really curious about that.
Big Thanks :)
+ 2
@Babak mate, it appears the code link was cropped due to text length limit.
I have revised the question so the link code can fit in, pardon me for the inconvenience mate :)
Thanks very much