Re: DecimalToFraction Algorithm
- From: Peter <peter@xxxxxxxxxxxxxxxxxxxxxx*>
- Date: Mon, 15 Aug 2005 20:38:28 +1000
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
.
- References:
- DecimalToFraction Algorithm
- From: Peter
- Re: DecimalToFraction Algorithm
- From: Mike Williams
- DecimalToFraction Algorithm
- Prev by Date: Re: DecimalToFraction Algorithm
- Next by Date: Re: DecimalToFraction Algorithm
- Previous by thread: Re: DecimalToFraction Algorithm
- Next by thread: Re: DecimalToFraction Algorithm
- Index(es):
Relevant Pages
|
Loading