Deriving decimal from fraction

A

arun.hallan

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
 
K

Kevin Spencer

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.
 
R

Rick Lones

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-
 
A

arun.hallan

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
 
B

Bruce Wood

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.
 
T

Thomas T. Veldhouse

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
 
R

Rick Lones

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-
 
K

Kevin Spencer

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.
 

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

Top