Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Fractions to Rational Numbers - Ruby Problem.

Post any examples or modules that you want to share here

Fractions to Rational Numbers - Ruby Problem.

Postby tiffy » Wed Oct 09, 2013 11:03 am

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 1213 times
User avatar
tiffy
 
Posts: 400
Joined: Wed May 08, 2013 12:14 pm

Re: Fractions to Rational Numbers - Ruby Problem.

Postby Nubeat7 » Wed Oct 09, 2013 12:19 pm

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 1221 times
User avatar
Nubeat7
 
Posts: 1347
Joined: Sat Apr 14, 2012 9:59 am
Location: Vienna

Re: Fractions to Rational Numbers - Ruby Problem.

Postby tiffy » Wed Oct 09, 2013 12:52 pm

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
User avatar
tiffy
 
Posts: 400
Joined: Wed May 08, 2013 12:14 pm


Return to User Examples

Who is online

Users browsing this forum: No registered users and 91 guests