Can you explain to me why this doesn’t work:
#include <iostream>
using namespace std;
double data_convert(int n);
int main(void) {
cout << data_convert(sizeof(int));
}
double data_convert(int n) {
int i;
double x;
x = 8 * n;
for(i = 0; i < 32; i++)
x = x * 32;
return x;
}
I tried using pow from cmath, but I got the same results. Apparently, this outputs “4.67681e+049”. Where as it should output (using Windows Calculator) “4294967296”.
The for loop is my own hardcoded pow() function for this specific task. All I wanna do is make a program that can show how big a data type is, along with it’s range (bit range or something, yeah?)
If you want
2^32, you should be multiplying by 2 each time. Your code multiplies by 32 each time, so you’ll end up with a much larger value.Also, your
xvalue should start from 1.8 * nis actually the number of bits in the integer, so that should be your upper limit for the loop:A simpler method would be to bitwise negate 0, which will give you the largest possible integer:
will give you 2^32 – 1 = 4294967295 (on a 32-bit machine).