Forum: Ruby Exception Handling/Divide By Zero/NaN Done on One Line

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ba79eb31ceaef95e9e52f5e5e4ec6d02?d=identicon&s=25 John Kopanas (Guest)
on 2007-01-24 16:34
(Received via mailing list)
I have the following:

a = (y != 0) ? (x/y) : 0

There must be a sexier way of doing this!  Any suggestions?

On a side note... how do we do exception handling on the same line as
statement?

a = x/y rescue 0

That does not work but does it make sense?  Is it because dividing by
zero does not throw an exception?

Thanks for your help! :-)

Your Friend,

John

--
John Kopanas
john@kopanas.com

http://www.kopanas.com
http://www.cusec.net
http://www.soen.info
843881c344479c7e0de8f67b2c03a4e5?d=identicon&s=25 Niko (Guest)
on 2007-01-24 16:48
(Received via mailing list)
a = begin
   x / y
rescue ZeroDivisionError
   0
end
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2007-01-24 16:53
(Received via mailing list)
On Thu, 25 Jan 2007, John Kopanas wrote:

>
> That does not work but does it make sense?  Is it because dividing by
> zero does not throw an exception?
>
> Thanks for your help! :-)
>

works for me?

   harp:~ > ruby -e'  a = 42/0 rescue 42;  p a  '
   42

i think you're doing the right thing with

> a = x/y rescue 0

how doesn't it work?

regards.

-a
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-01-24 18:11
(Received via mailing list)
On Jan 24, 2007, at 10:52 AM, ara.t.howard@noaa.gov wrote:

>>
>   harp:~ > ruby -e'  a = 42/0 rescue 42;  p a  '
> -a
> --
> we can deny everything, except that we have the possibility of
> being better.
> simply reflect on that.
> - the dalai lama

If you're not limited to Integers:

 >> 1/0
ZeroDivisionError: divided by 0
         from (irb):1:in `/'
         from (irb):1
 >> 1.0/0
=> Infinity
 >> 1/0.0
=> Infinity
 >> 0.0/0.0
=> NaN
 >>

If either x or y are Float, then dividing by zero isn't an exception.

You could force an exception if you want to get back to an Integer:

 >> (1.0/0).to_i
FloatDomainError: Infinity
         from (irb):7:in `to_i'
         from (irb):7
 >> (0.0/0.0).to_i
FloatDomainError: NaN
         from (irb):8:in `to_i'
         from (irb):8

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Ba79eb31ceaef95e9e52f5e5e4ec6d02?d=identicon&s=25 John Kopanas (Guest)
on 2007-01-24 19:47
(Received via mailing list)
I need to keep it as a float... that is my problemo :-(.

On 1/24/07, Rob Biedenharn <Rob@agileconsultingllc.com> wrote:
> >>
> >
> >
>
>  >>
> FloatDomainError: NaN
>
--
John Kopanas
john@kopanas.com

http://www.kopanas.com
http://www.cusec.net
http://www.soen.info
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-01-24 23:34
(Received via mailing list)
On Jan 24, 2007, at 1:46 PM, John Kopanas wrote:
>> >> There must be a sexier way of doing this!  Any suggestions?
>> >> Thanks for your help! :-)
>> >
>>
>>  >> 0.0/0.0
>>          from (irb):7
>
> I need to keep it as a float... that is my problemo :-(.
> --
> John Kopanas
> john@kopanas.com
>
> http://www.kopanas.com
> http://www.cusec.net
> http://www.soen.info

Perhaps...

a = x/y

a = 0.0 unless a.finite?

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.