I have two integers i1 and i2, the second of which is guaranteed to be
between 0 and 99, and I encode them into one double:
double encoded = (double)i1 + (double)i2 / (double)100;
So, for example, 324 and 2 become 324.02. Now I want to decode them
using the function given below but it decodes the example as 324 and
1, instead of 324 and 2.
Can anyone tell me what's wrong and how to do this right? (my code see
below)
Thanks!
Markus
#include <iostream>
void decode(double n, int& i1, int& i2){
i1 = int(n);
double rest = n - int(n);
i2 = int(rest * 100.0); // i2 is 1, should be
2
}
int main(int argc, char** argv){
double n = 324.02;
int p;
int i;
decode(n, p, i);
std::cerr << "n=" << n <<", p=" << p << ", i=" << i << std::endl;
return EXIT_SUCCESS;
}
between 0 and 99, and I encode them into one double:
double encoded = (double)i1 + (double)i2 / (double)100;
So, for example, 324 and 2 become 324.02. Now I want to decode them
using the function given below but it decodes the example as 324 and
1, instead of 324 and 2.
Can anyone tell me what's wrong and how to do this right? (my code see
below)
Thanks!
Markus
#include <iostream>
void decode(double n, int& i1, int& i2){
i1 = int(n);
double rest = n - int(n);
i2 = int(rest * 100.0); // i2 is 1, should be
2
}
int main(int argc, char** argv){
double n = 324.02;
int p;
int i;
decode(n, p, i);
std::cerr << "n=" << n <<", p=" << p << ", i=" << i << std::endl;
return EXIT_SUCCESS;
}
Comment