Re: DecimalToFraction Algorithm



In article <ddpiss$60k$1@xxxxxxxxxxxxxxxxxxx>,
"Mike Williams" <Mike@xxxxxxxxxxxxxxxxx> wrote:

> "Peter" <peter@xxxxxxxxxxxxxxxxxxxxxx*> wrote in message
> news:peter-CB415C.15305015082005@xxxxxxxxxxxxxxxxxxxxxxxx
>
> > I recently requested an alg to convert a decimal to fraction.
> > I found a paper with some delphi code and converted it to
> > RB. Here it is:
>
> Depends what you're after, I suppose. Personally your algorithm wouldn't
> do for me, or for most engineers. I'd want something that came up with a
> fraction where the denominator is 2, 4, 8, 16, 32 or 64 and no other
> value. For example, I'd want 0.333 to come up with the answer 21/64 and
> I'd want 0.4 to come up with the answer 13/32, as in the following
> "imperial drill size" list:
>
> http://modratec.com/mud_drill.php
>
> I'm certain that Rick can come up with such a solution . . . but can you
> do the same (in as few lines of code as possible)?
>
> (Don't ya' just love a challenge!)
>
> Mike

Well yes, I guess if you wanted to do that then Ricks code would be
better. I just wanted something a bit more general (and exact).

Sure, off the top of my head:

denominator = 64 // could be 2,4,8 etc.

numerator = Round(myValue * denominator)

while numerator mod 2 = 0
numerator = numerator/2
denominator = denominator/2
wend


Peter
.



Relevant Pages

  • Re: help with radical expressions
    ... > in high school and College Algebra, but they went over it so quickly I ... the denominator, ... I see occurs in both terms in the numerator ... So I pull out a fraction that has ...
    (sci.math)
  • Re: Letter to the Editor: The truth of ID is self-evident
    ... The numerator and denominator of a fraction can be any ... And yes, 7 does divide 22. ...
    (talk.origins)
  • Re: reducing large fractions
    ... > Factor the numerator and denominator and cancel common factors. ... so you cannot reduce the fraction. ...
    (sci.math)
  • Re: Cannot find the divison bar on EE
    ... The horizontal line that separates the numerator and denominator of a ... "shilling" fraction? ...
    (microsoft.public.word.docmanagement)
  • Re: Conflicting needs for __init__ method
    ... single integer, another Rational instance, and perhaps floats, Decimal ... And when initializing from a pair of integers---a numerator ... sure that the denominator is positive. ... like negation or raising to a positive integer power, ...
    (comp.lang.python)

Loading