473,659 Members | 3,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2497
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*********@gm ail.comwrote in message
news:11******** *************@m 79g2000cwm.goog legroups.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*********@gma il.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*********@gma il.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*********@gma il.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*********@gma il.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*********@gma il.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*********@gma il.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
representati on 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*********@gm ail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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*********@gma il.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
representati on 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*********@gma il.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
4518
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
1976
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 some Decimal values onto a binary file (or into a stream that writes into an array of bytes) and examine them.
9
2291
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
13
9835
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 convert it to a HEX value. I also need to find a way to determine if a number is a fraction in HEX and convert it back to DEC. (This would be from a disassembly, and we are learning this for troubleshooting purposes)
7
2663
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 "100.00" MsgBox(d1 = d2) 'displays "True"
0
1335
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 text boxes . One with the whole number and other with the decimal fraction . Ex. If 15.4 is the answer , I need to print it as 15 in one text box and 4 in another text box .
52
1565
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly.
10
5315
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 number text box, a numerator text box, and a denominator text box. I realize that this is probably the closest I'll get to having fractions displayed in VB, so that's no big deal. I'm able to simplify most numbers with some code I've written,...
4
4264
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 happen to have no decimal fractions, the results through pysqlite are coming through as integers. The funny thing is that when looking at the database using SQLite Manager or SQLite Pro the results there are displayed correctly. As a temporary fix...
0
8335
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.