Best way to find rational for a given float

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
 
F

Frederick Gotham

Paulo Matos:
Given a float type, is there a way to find the rational which is equal
to it?

Do you mean a fraction whereby the top line and the bottom line are both
integers?

Eg.

Input: 2523.25235

Output: 252325235
---------
100000

Hmm... floating-point isn't my speciality. My first guess though would be
that you'd have to figure out how to interpret the mantissa (or whatever it's
called), which might be an implementation-specific job.
 
J

Jim Langston

Paulo Matos said:
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

You are going to run into problems with floating points storage. If I
needed to do this what I would probably do is get the decimal part as an
integer. Since I know that floating point doesn't go directly to integers,
I would do it via a stringstream into a string, then into an integer (there
is probably a better way).
 
R

Robert Mabee

Paulo said:
Given a float type, is there a way to find the rational which is equal
to it?

Imagine that the float F is represented by an integer I with a certain
range, and an exponent E of some agreed-on base B such that
F = I * B ^ E (using ^ for exponentiation here)

F is clearly a multiple of B ^ E. When that's >= 1 (E >= 0) then F is
an integer so you're done (denominator = 1). Otherwise B ^ E is a
fraction equal to 1 / (B ^ -E). That would make a good denominator
for a first step, after which you might look to reduce by common
factors.

You can find E by experiment. Supposing you at least know that B = 2,
B ^ E is the most negative power of 2 that when added to F doesn't equal
F.
 
R

Ron Natalie

Paulo said:
Hi all,

Given a float type, is there a way to find the rational which is equal
to it?

Sure. Every computer I've ever used implements floats as rational
numbers.
while(x != floor(x)) { x *= 10; i++; }
Hold the phone? What makes you think that a floating
point number is precisely implented as a base 10 fraction?
It's base 2 on every computer I've used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top