Fractions to Rational Numbers - Ruby Problem.

Post any examples or modules that you want to share here
Post Reply
User avatar
tiffy
Posts: 400
Joined: Wed May 08, 2013 12:14 pm

Fractions to Rational Numbers - Ruby Problem.

Post by tiffy »

I wrote the following Ruby Code that convert Fractions to Rational Numbers with the smallest possible denominator correctly, but I have a problem in that the code as is does not handle the exception error "ZeroDivisionError". I therefore, only used a "0.0" test condition [don't worry too much about the <> signs, that must be corrected as well] as an example as to demonstrate what I intend doing with the code as is. This test condition [i.e. 0.0] must be replaced with whatever will test correctly for "ZeroDivisionError" handling.

Is there someone out there who knows how to 'capture' the 'ZeroDivisionError' in the Flowstone Ruby interpreter and rectify the code below?

As the Ruby code currently is, the Flowstone Ruby interpreter will just 'freeze' when the code is made into a module and executed when it encounters a 'ZeroDivisionError'.

If you study the schematic, you will notice that the main problem in writing the correct 'ZeroDivisionError' handling Ruby code is that the Fraction is fed to the Flowstone Ruby interpreter not as independent 'x' and 'y' values, but as a single value 'z = (x/y)'.

# RATIONAL NUMBER WITH LOWEST POSSIBLE DENOMINATOR:

x = @fraction.to_r

# IF THEN ELSE, STATEMENT:

if (x.to_f < 0.0) then # What test condition to compare with here for "ZeroDivisionError" outputs??

output 0, x = "ZeroDivisionError"
output 1, x = "ZeroDivisionError" else

if (x.to_f > 0.0) then # What condition to compare with here to skip "ZeroDivisionError" outputs??

output 0, x.to_s
output 1, x.to_f
end
end

# I only used "0.0" test condition as an example as to demonstrate what I intend doing.
# This condition [i.e. 0.0] must be replaced with whatever will test correctly for "ZeroDivisionError" handling.

******************************************************************************************************************************************

# The Ruby Exception Hierarchy:

Exception
NoMemoryError
ScriptError
LoadError
NotImplementedError
SyntaxError
SignalException
Interrupt
StandardError
ArgumentError
IOError
EOFError
IndexError
LocalJumpError
NameError
NoMethodError
RangeError
FloatDomainError
RegexpError
RuntimeError
SecurityError
SystemCallError
SystemStackError
ThreadError
TypeError
ZeroDivisionError
SystemExit
fatal
:lol: :mrgreen: :lol:
Attachments
RUBY - IF THEN STATEMENT.fsm
(1.57 KiB) Downloaded 1291 times
User avatar
Nubeat7
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna
Contact:

Re: Fractions to Rational Numbers - Ruby Problem.

Post by Nubeat7 »

interesting, this '.to_r' didn`t know that :)

i think using rescue would fit fine here
Attachments
rescue_zero_division.fsm
(1.43 KiB) Downloaded 1306 times
User avatar
tiffy
Posts: 400
Joined: Wed May 08, 2013 12:14 pm

Re: Fractions to Rational Numbers - Ruby Problem.

Post by tiffy »

Nubeat7 wrote:interesting, this '.to_r' didn`t know that :)

i think using rescue would fit fine here


Thank you Nubeat7 - very exciting feature this 'rescue'.
:D
Post Reply