473,378 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Best way to find rational for a given float

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

Nov 19 '06 #1
7 2114
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.

--

Frederick Gotham
Nov 19 '06 #2
"Paulo Matos" <po******@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
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).
Nov 20 '06 #3
Paulo Matos wrote:
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.
Nov 20 '06 #4
Paulo Matos wrote:
Hi all,

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

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 20 '06 #5
Paulo Matos wrote:
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.
Nov 20 '06 #6
Pete Becker <pe**@versatilecoding.comwrote:
>Paulo Matos wrote:
>Given a float type, is there a way to find the rational which is equal
to it?
>Read about frexp.
Good idea. Note when using that frexp wants a double, not a float.

Steve
Nov 20 '06 #7

Pete Becker escreveu:
Paulo Matos wrote:
Hi all,

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

Read about frexp.
This is just what I needed.
Thank you and thank you all for your excellent replies! :-)
--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Mike Meyer | last post by:
PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $ Last-Modified: $Date: 2003/09/22 04:51:50 $ Author: Mike Meyer <mwm@mired.org> Status: Draft Type: Staqndards...
20
by: Mike Meyer | last post by:
This version includes the input from various and sundry people. Thanks to everyone who contributed. <mike PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $...
11
by: ike | last post by:
Could you please give your opinion on the portability of this code? /* -------- begin findindex.c -------- */ struct Foo { int junk0; }; struct Bar { int junk1; struct Foo foo; int junk2; }; ...
7
by: skip | last post by:
A guy at work asked for functionality commonly found with rational numbers, so I said I'd find and install something. I figured gmpy would be suitable, alas I'm having trouble successfully...
2
by: Rod | last post by:
While working with a datagrid I have a value for the datakey, from this I want to find the ItemIndex. How please? many thanks in advance.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.