473,396 Members | 1,907 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,396 software developers and data experts.

Deriving decimal from fraction

I need to derive fractions from decimals as so:

0.3333 = 1/3
0.6666 = 2/3

The decimal could also be 0.33, or 0.3333333 but the point is that it
that the fraction needs to be extracted.

The reason for this is a UI restriction, where users can only enter a
percentage. But in the case where a third needs to be expressed, a
decimal will never be as accurate as teh fraction.
Thanks,
Arun

Jul 14 '06 #1
9 2480
Sorry, the title of this post should be

Deriving fraction from decimal

Jul 14 '06 #2
Your question doesn't make sense. You are correct in that a decimal is not
(necessarily) as accurate as a fraction, but a fraction derived from a
decimal will always be only as accurate as the decimal it was derived from.
For example, let's look at your example:

0.3333 = 1/3

This is incorrect. 0.3333 and 1/3 are 2 entirely different values. 1/3 is
greater than 0.3333. So, there is no way to derive 1/3 from 0.3333. You
*could* derive a fraction from it, but it would not be 1/3. In fact, you
could not derive 0.3333 from 1/3, as they are not equal. 1/3 cannot be
expressed in decimal numbers.

The formula is simple algebra. 1/3 is an expression that indicates 1 divided
by 3. So, let's start out with a fraction that *can* be converted to a
decimal:

0.25 = 1/4

This means that 0.25 is equal to 1 divided by 4. This could be expressed
using variables as:

x = y / z

From algebra, we know that to resolve for y, we use:

y = x * z

To resolve for z:

z = y / x

So, to derive 1 / 4 from 0.25, you would say:

0.25 = y / z

Now, it is important to note here that any number of fractions can be
derived from a decimal. For example:

0.25 = 1 / 4
0.25 = 2 / 8
0.25 = 3 / 12

So, all we have to do is plug in an arbitrary number into either the 'y' or
'z' variable to derive the other:

0.25 = 2 / z
2 / 0.25 = z
2 / .25 = 8
0.25 = 2 / 8

Of course, depending on what value you plug in, you may get a decimal or
fractional result for the other:

0.25 = 3 / z
3 / 0.25 = z
3 / 0.25 = 0.75
0.25 = 3 / 0.75

An easy fix is to use the original decimal, converted to a whole number, to
ensure a whole fraction:

0.25 = 25 / z
0.25 / 25 = z
0.25 / 25 = 100
0.25 = 25 / 100

If you want to, you can reduce the fraction, but I'm sure you know how to do
that.

In any case, as I said, you're not going to get any more accurate this way.
Because fractions *are* more accurate than decimals, you will never achieve
greater accuracy by converting a decimal to a fraction.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
<ar*********@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
>I need to derive fractions from decimals as so:

0.3333 = 1/3
0.6666 = 2/3

The decimal could also be 0.33, or 0.3333333 but the point is that it
that the fraction needs to be extracted.

The reason for this is a UI restriction, where users can only enter a
percentage. But in the case where a third needs to be expressed, a
decimal will never be as accurate as teh fraction.
Thanks,
Arun

Jul 14 '06 #3
ar*********@gmail.com wrote:
I need to derive fractions from decimals as so:

0.3333 = 1/3
0.6666 = 2/3

The decimal could also be 0.33, or 0.3333333 but the point is that it
that the fraction needs to be extracted.

The reason for this is a UI restriction, where users can only enter a
percentage. But in the case where a third needs to be expressed, a
decimal will never be as accurate as teh fraction.
0.3333 = 3333/10000
0.6666 = 3333/5000

The problem here is that both of your examples have a natural and well-defined
fraction representation - it's just not the one you want.

You are getting into AI ("read my mind") territory here I am afraid. I think
that IF you really need an explicit numerator and denominator then you will need
to change the UI to accept them in that form. (And the program to work with
them in that form as well, of course.) You will never get an exact
representation of 1/3 in floating point. What is driving this requirement for
an exact representation? An education-based app of some kind?

-rick-
Jul 14 '06 #4
Sorry, i should have made myself clearer.
And Kevin, i have a mathematically based computer science degree, and
understand fractions!

I am worknig on an old system. The UI allows percentages to be added.
So if we have a number, say 1000000, and you want to represent 20%, you
can enter 20%.

But what if you want to enter a third?

The only solution i can see is to write code to round numbers such as
0.3333 to a third.

YES i know it's not accurate, and i know the difference between 0.3333
and 1/3.

I'm just asking if there is a better solution to this, in the same way
as a human can look at 0.3333 and intuitively know that the value is
around a third.

Thanks
Arun

Rick Lones wrote:
ar*********@gmail.com wrote:
I need to derive fractions from decimals as so:

0.3333 = 1/3
0.6666 = 2/3

The decimal could also be 0.33, or 0.3333333 but the point is that it
that the fraction needs to be extracted.

The reason for this is a UI restriction, where users can only enter a
percentage. But in the case where a third needs to be expressed, a
decimal will never be as accurate as teh fraction.

0.3333 = 3333/10000
0.6666 = 3333/5000

The problem here is that both of your examples have a natural and well-defined
fraction representation - it's just not the one you want.

You are getting into AI ("read my mind") territory here I am afraid. I think
that IF you really need an explicit numerator and denominator then you will need
to change the UI to accept them in that form. (And the program to work with
them in that form as well, of course.) You will never get an exact
representation of 1/3 in floating point. What is driving this requirement for
an exact representation? An education-based app of some kind?

-rick-
Jul 14 '06 #5

ar*********@gmail.com wrote:
Sorry, i should have made myself clearer.
And Kevin, i have a mathematically based computer science degree, and
understand fractions!

I am worknig on an old system. The UI allows percentages to be added.
So if we have a number, say 1000000, and you want to represent 20%, you
can enter 20%.

But what if you want to enter a third?

The only solution i can see is to write code to round numbers such as
0.3333 to a third.

YES i know it's not accurate, and i know the difference between 0.3333
and 1/3.

I'm just asking if there is a better solution to this, in the same way
as a human can look at 0.3333 and intuitively know that the value is
around a third.

Thanks
Arun
Kevin is right: you have to know the denominator in order to round to
the closest enumerator. The only approach I can suggest is an iterative
one: pick a range of denominators, try each one, and pick the one that
is the closest approximation to the decimal that the user entered. You
could use a tolerance, too, and if none of the values is within
tolerance then reject them all and use the decimal instead.

Jul 14 '06 #6
ar*********@gmail.com wrote:
I need to derive fractions from decimals as so:

0.3333 = 1/3
0.6666 = 2/3

The decimal could also be 0.33, or 0.3333333 but the point is that it
that the fraction needs to be extracted.

The reason for this is a UI restriction, where users can only enter a
percentage. But in the case where a third needs to be expressed, a
decimal will never be as accurate as teh fraction.

There is an old method of doing this by using a Farey Sequence. I used to
play with the pattern as kid and was pleasantly surprised that I didn't invent
it ;-)

So, a nice search finds a good example at Dr. Dobbs.

http://www.ddj.com/184409653

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Jul 14 '06 #7
Yeah, that's what I mean by "AI territory". Your desired inference, that 0.3333
= 1/3, is not possible in general without some explicit indication of where the
repeating decimals occur.

If I input ".3636" how do you (meaning some dumb program) know whether I want:
a) .3636 exactly, or
b) .36366666 . . ., or
c) .3636363636 . . . ?

I think you are stuck in general unless you can obtain additional hints via the
UI, as per, e.g.,
a) .3636,
b) .363(6),
c) .(36)

-rick-

ar*********@gmail.com wrote:
Sorry, i should have made myself clearer.
And Kevin, i have a mathematically based computer science degree, and
understand fractions!

I am worknig on an old system. The UI allows percentages to be added.
So if we have a number, say 1000000, and you want to represent 20%, you
can enter 20%.

But what if you want to enter a third?

The only solution i can see is to write code to round numbers such as
0.3333 to a third.

YES i know it's not accurate, and i know the difference between 0.3333
and 1/3.

I'm just asking if there is a better solution to this, in the same way
as a human can look at 0.3333 and intuitively know that the value is
around a third.

Thanks
Arun

Rick Lones wrote:
>ar*********@gmail.com wrote:
>>I need to derive fractions from decimals as so:

0.3333 = 1/3
0.6666 = 2/3

The decimal could also be 0.33, or 0.3333333 but the point is that it
that the fraction needs to be extracted.

The reason for this is a UI restriction, where users can only enter a
percentage. But in the case where a third needs to be expressed, a
decimal will never be as accurate as teh fraction.
0.3333 = 3333/10000
0.6666 = 3333/5000

The problem here is that both of your examples have a natural and well-defined
fraction representation - it's just not the one you want.

You are getting into AI ("read my mind") territory here I am afraid. I think
that IF you really need an explicit numerator and denominator then you will need
to change the UI to accept them in that form. (And the program to work with
them in that form as well, of course.) You will never get an exact
representation of 1/3 in floating point. What is driving this requirement for
an exact representation? An education-based app of some kind?

-rick-
Jul 14 '06 #8
Hi Arun,

If you want a human to look at the number and intuitively know what the
amount is, think graphics. Humans do not natively understand numbers. We
have to be taught to think in terms of numbers. But our brains are actually
incredibly accurate computers, as exemplified by, for example, a figure
skater. The calculations required to maintain balance while perched on top
of a couple of pieces of metal while sliding across an ice surface and
performing leaps, twirls, and jumps, is something no computer can do.

What humans *do* understand is graphics. We see from the time we are born,
and learn to calculate by sight, how large things are, how far away they
are, etc. This is why pie charts are so popular. So, what about displaying a
pie chart, a graph, or some other visual representation of the number?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
<ar*********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Sorry, i should have made myself clearer.
And Kevin, i have a mathematically based computer science degree, and
understand fractions!

I am worknig on an old system. The UI allows percentages to be added.
So if we have a number, say 1000000, and you want to represent 20%, you
can enter 20%.

But what if you want to enter a third?

The only solution i can see is to write code to round numbers such as
0.3333 to a third.

YES i know it's not accurate, and i know the difference between 0.3333
and 1/3.

I'm just asking if there is a better solution to this, in the same way
as a human can look at 0.3333 and intuitively know that the value is
around a third.

Thanks
Arun

Rick Lones wrote:
>ar*********@gmail.com wrote:
I need to derive fractions from decimals as so:

0.3333 = 1/3
0.6666 = 2/3

The decimal could also be 0.33, or 0.3333333 but the point is that it
that the fraction needs to be extracted.

The reason for this is a UI restriction, where users can only enter a
percentage. But in the case where a third needs to be expressed, a
decimal will never be as accurate as teh fraction.

0.3333 = 3333/10000
0.6666 = 3333/5000

The problem here is that both of your examples have a natural and
well-defined
fraction representation - it's just not the one you want.

You are getting into AI ("read my mind") territory here I am afraid. I
think
that IF you really need an explicit numerator and denominator then you
will need
to change the UI to accept them in that form. (And the program to work
with
them in that form as well, of course.) You will never get an exact
representation of 1/3 in floating point. What is driving this
requirement for
an exact representation? An education-based app of some kind?

-rick-

Jul 14 '06 #9
Thanks - i think there's something i could look at.

Thomas T. Veldhouse wrote:
ar*********@gmail.com wrote:
I need to derive fractions from decimals as so:

0.3333 = 1/3
0.6666 = 2/3

The decimal could also be 0.33, or 0.3333333 but the point is that it
that the fraction needs to be extracted.

The reason for this is a UI restriction, where users can only enter a
percentage. But in the case where a third needs to be expressed, a
decimal will never be as accurate as teh fraction.

There is an old method of doing this by using a Farey Sequence. I used to
play with the pattern as kid and was pleasantly surprised that I didn't invent
it ;-)

So, a nice search finds a good example at Dr. Dobbs.

http://www.ddj.com/184409653

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Jul 17 '06 #10

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
5
by: Michael A. Covington | last post by:
Microsoft's documentation is a bit unclear, but is Decimal in fact a radix-100 (base-100) arithmetic data type (in which each byte ranges only from 0 to 99)? It should be straightforward to dump...
9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
13
by: Steve | last post by:
I am having trouble finding the answer to this question, I'm thinking the solution must be blindingly obvious, so therefore of course I cannot see it. I wish to take a fraction in DEC say .4 and...
7
by: Oenone | last post by:
Can anyone explain why the following happens? \\\ Dim d1 As Decimal = CDec("100") Dim d2 As Decimal = CDec("100.00") MsgBox(d1.ToString) 'displays "100" MsgBox(d2.ToString) 'displays...
0
by: guindy | last post by:
Hello friends , I am a New bee to VB . I Need a solution for a little thing Consider a situation where the result is of a floating point value. I need to print the value separately in two...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
10
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
4
by: Astley Le Jasper | last post by:
I've been getting errors recently when using pysqlite. I've declared the table columns as real numbers to 2 decimal places (I'm dealing with money), but when doing division on two numbers that...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.