P
Paulo Matos
Hi all,
Given a float type, is there a way to find the rational which is equal
to it?
I can imagine such a process to work in theory but I don't know if this
will work in C++. I'd like comments on the following, which doesn't
work:
#include <cmath>
#include <iostream>
using namespace std;
int main(void) {
double x = 30.503;
int i = 0;
while(x != floor(x)) { x *= 10; i++; }
cout << "num: " << x << "den: " << pow(10.0, (double)i) << endl;
return 0;
}
is there a way to do it that works?
Regards,
Paulo Matos
Given a float type, is there a way to find the rational which is equal
to it?
I can imagine such a process to work in theory but I don't know if this
will work in C++. I'd like comments on the following, which doesn't
work:
#include <cmath>
#include <iostream>
using namespace std;
int main(void) {
double x = 30.503;
int i = 0;
while(x != floor(x)) { x *= 10; i++; }
cout << "num: " << x << "den: " << pow(10.0, (double)i) << endl;
return 0;
}
is there a way to do it that works?
Regards,
Paulo Matos