[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fix for rational.rb (accepting Rationals as num/den

Stefan Rusterholz

8/11/2006 4:00:00 PM

Hi, Rational currently doesn't accept Rationals as
Numerator/Denominator. I wrote a patch for it and look for feedback.
Thanks.

def Rational(a, b = 1)
Rational.reduce(*Rational.normalize(a,b))
end

class Rational < Numeric
# normalizes numerator/denominator to non-rationals
def Rational.normalize(num,den)
if a.kind_of?(Rational) then
if b == 1 then
return [a.nominator, a.denominator]
elsif b.kind_of?(Rational) then
return [a.numerator*b.denominator, a.denominator*b.numerator]
else
return [a.numerator, a.denominator*b]
end
elsif b.kind_of?(Rational) then
if a == 1 then
return [b.denominator, b.nominator]
else
return [a*b.denominator, b.numerator]
end
else
return [a,b]
end
end

def Rational.new!(num, den = 1)
new(*Rational.normalize(a,b))
end
end

--
Posted via http://www.ruby-....

5 Answers

Stefan Rusterholz

8/11/2006 4:03:00 PM

0

Whoops, that was the old code snippet...

def Rational(a, b = 1)
Rational.reduce(*Rational.normalize(a,b))
end

class Rational < Numeric
# normalizes numerator/denominator to non-rationals
def Rational.normalize(a,b)
if a.kind_of?(Rational) then
if b == 1 then
return [a.nominator, a.denominator]
elsif b.kind_of?(Rational) then
return [a.numerator*b.denominator, a.denominator*b.numerator]
else
return [a.numerator, a.denominator*b]
end
elsif b.kind_of?(Rational) then
if a == 1 then
return [b.denominator, b.nominator]
else
return [a*b.denominator, b.numerator]
end
else
return [a,b]
end
end

def Rational.new!(num, den = 1)
new(*Rational.normalize(num,den))
end
end

Should work a bit better ;-)

--
Posted via http://www.ruby-....

Bernard Kenik

8/11/2006 8:37:00 PM

0

Stefan Rusterholz wrote:
> Whoops, that was the old code snippet...
>
> def Rational(a, b = 1)
> Rational.reduce(*Rational.normalize(a,b))
> end
>
> class Rational < Numeric
> # normalizes numerator/denominator to non-rationals
> def Rational.normalize(a,b)
> if a.kind_of?(Rational) then
> if b == 1 then
> return [a.nominator, a.denominator]
> elsif b.kind_of?(Rational) then
> return [a.numerator*b.denominator, a.denominator*b.numerator]
> else
> return [a.numerator, a.denominator*b]
> end
> elsif b.kind_of?(Rational) then
> if a == 1 then
> return [b.denominator, b.nominator]
> else
> return [a*b.denominator, b.numerator]
> end
> else
> return [a,b]
> end
> end
>
> def Rational.new!(num, den = 1)
> new(*Rational.normalize(num,den))
> end
> end
>
> Should work a bit better ;-)
>
> --
> Posted via http://www.ruby-....

Tested the code and it appears to do it correctly

add /substact rationals
raise rational to rational
add/substract rational and integer
add/substract rational and float


About the only thing I tried but could not do would be rational to mix
number

Rational(11,7).to_mix => "11 4/7"

Bill Swears

4/25/2009 9:04:00 PM

0

nobody wrote:
> Bill Swears <wswears@gci.net> wrote:
>
>> nobody wrote:
>>> Boudewijn Rempt <boud@valdyas.org> wrote:
>>>
>>>> exclude and blackhole the kind of messages and the people
>>>> that have made "David!" a curse-word and a well-understood synonym for
>>>> uninformed pedantery in our household
>>> What does that mean, anyone?
>>>
>> David Friedman has a tendency to explore any concept or controversy in
>> very great depth. It can go on for hundreds of posts, and he can
>> inadvertently derail an entire thread. Guess what? He hasn't been
>> doing this for the best part of a year, since about the time when we
>> agreed to start using rec.arts.sf.misc for OT and divisive discourse.
>
> Thanks for the clarification Bill. DF is the only "David" that's been
> posting much since I arrived, and though it seems as if he can develop
> some attitude along with the rest of us, I hadn't noticed him being
> uninformedly pedantic. I have a rotting memory, but isn't he a
> professor? Show me any one of that species who is never "pedantic".
>
There are some real professors on this newsgroup, and some of us with
Adjunctivitis. David is definitely real. David's mind arguably works in
different ways than average, and he can worry at a concept, or attempt
to convince you, until your eyes glass over. I've never really seen him
get angry - his posts are imminently logical, but he can stay focused on
one topic until everybody else is ready to tear their hairs out. The
thing is, it always begins as a logical digression, then takes on a life
of it's own, and there isn't really a point where you can say, "OK,
stop! We can agree to disagree."

Bill

--
Living on the polemic may be temporarily satisfying, but it will raise
your blood-pressure, and gives you tunnel vision.

John

4/25/2009 9:25:00 PM

0

Bill Swears <wswears@gci.net> wrote:

>nobody wrote:
>> Bill Swears <wswears@gci.net> wrote:
>>
>>> nobody wrote:
>>>> Boudewijn Rempt <boud@valdyas.org> wrote:
>>>>
>>>>> exclude and blackhole the kind of messages and the people
>>>>> that have made "David!" a curse-word and a well-understood synonym for
>>>>> uninformed pedantery in our household
>>>> What does that mean, anyone?
>>>>
>>> David Friedman has a tendency to explore any concept or controversy in
>>> very great depth. It can go on for hundreds of posts, and he can
>>> inadvertently derail an entire thread. Guess what? He hasn't been
>>> doing this for the best part of a year, since about the time when we
>>> agreed to start using rec.arts.sf.misc for OT and divisive discourse.
>>
>> Thanks for the clarification Bill. DF is the only "David" that's been
>> posting much since I arrived, and though it seems as if he can develop
>> some attitude along with the rest of us, I hadn't noticed him being
>> uninformedly pedantic. I have a rotting memory, but isn't he a
>> professor? Show me any one of that species who is never "pedantic".
>>
>There are some real professors on this newsgroup, and some of us with
>Adjunctivitis. David is definitely real. David's mind arguably works in
>different ways than average, and he can worry at a concept, or attempt
>to convince you, until your eyes glass over. I've never really seen him
>get angry - his posts are imminently logical, but he can stay focused on
>one topic until everybody else is ready to tear their hairs out. The
>thing is, it always begins as a logical digression, then takes on a life
>of it's own, and there isn't really a point where you can say, "OK,
>stop! We can agree to disagree."
>
>Bill

That seems like a special case of a more generalized phenomenon that
could affect rasfc in other ways. What exactly the phenomenon is I
can't say... "creeping somethingorother".

--
FAQ POINTER: http://www.lshelby.com/ras...
my fiction: http://fictionfromnobody.bl...

Jacey Bedford

4/26/2009 12:34:00 AM

0

In message <GaCdnRX9gL5M527UnZ2dnUVZ_oKdnZ2d@posted.mtasolutions>, Bill
Swears <wswears@gci.net> writes
>nobody wrote:
>> Bill Swears <wswears@gci.net> wrote:
>>
>>> nobody wrote:
>>>> Boudewijn Rempt <boud@valdyas.org> wrote:
>>>>
>>>>> exclude and blackhole the kind of messages and the people that
>>>>>have made "David!" a curse-word and a well-understood synonym for
>>>>>uninformed pedantery in our household
>>>> What does that mean, anyone?
>>>>
>>> David Friedman has a tendency to explore any concept or controversy
>>>in very great depth. It can go on for hundreds of posts, and he can
>>>inadvertently derail an entire thread. Guess what? He hasn't been
>>>doing this for the best part of a year, since about the time when we
>>>agreed to start using rec.arts.sf.misc for OT and divisive discourse.
>> Thanks for the clarification Bill. DF is the only "David" that's
>>been
>> posting much since I arrived, and though it seems as if he can develop
>> some attitude along with the rest of us, I hadn't noticed him being
>> uninformedly pedantic. I have a rotting memory, but isn't he a
>> professor? Show me any one of that species who is never "pedantic".
>>
>There are some real professors on this newsgroup, and some of us with
>Adjunctivitis. David is definitely real. David's mind arguably works
>in different ways than average, and he can worry at a concept, or
>attempt to convince you, until your eyes glass over. I've never really
>seen him get angry - his posts are imminently logical, but he can stay
>focused on one topic until everybody else is ready to tear their hairs
>out. The thing is, it always begins as a logical digression, then
>takes on a life of it's own, and there isn't really a point where you
>can say, "OK, stop! We can agree to disagree."


And if you need information on medieval recipes, he's your man - and
very helpful.

Jacey
--
Jacey Bedford
jacey at artisan hyphen harmony dot com
posting via usenet and not googlegroups, ourdebate
or any other forum that reprints usenet posts as
though they were the forum's own