help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help - warning division by zero


From: Andrew Janke
Subject: Re: help - warning division by zero
Date: Fri, 25 Oct 2019 02:22:16 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.0

Hi Joe,

Convention on this mailing list is to "bottom-post", putting your reply
below the previous emails. Scroll down to see reply.

On 10/25/19 2:12 AM, Joe Tusek wrote:
> -----Original Message-----
> From: Andrew Janke <address@hidden> 
> Sent: Friday, 25 October 2019 5:04 PM
> To: Joe Tusek <address@hidden>; address@hidden
> Subject: Re: help - warning division by zero
> 
> 
> 
> On 10/25/19 1:58 AM, Joe Tusek wrote:
>> Hi,
>>
>>
>> I have code that uses ifelse statements to prevent the possibility of 
>> a division by zero. It appears though that all the conditions of the 
>> ifelse are evaluated as part of the ifelse execution and this results 
>> in a warning about a division by zero, even if the code should prevent 
>> such a case.
>>
>> This leaves a warning of division by zero in the Command Window when 
>> the "executed code" had not resulted in one.
>>
>> The code snippet has been crafted to generate the behaviour
>>
>>>> ifelse (1==1,0,1/0)
>>
>> warning: division by zero
>>
>> ans = 0
>>
>>>> ifelse (1==0,0,1/0)
>>
>> warning: division by zero
>>
>> ans =  Inf
>>
>>>> ifelse (1==0,0,1)
>>
>> ans =  1
>>
>> Should this warning be supressed (so that it does not appear in the 
>> Command Window) when there was no active code that resulted in a 
>> division by zero?
> 
> Hi Joe,
> 
> What is this "ifelse" statement or function that you're using in your example 
> code? Octave doesn't have an "ifelse" statement.
> 
> If this is a function you've implemented yourself, you need to know that 
> Octave is an "eager-evaulation" language: all the input arguments to a 
> function are *always* evaluated before being passed in. So that "1/0"
> that is the 3rd argument to the function always happens. This isn't like a 
> Lisp or whatever where you can have "lazy" evaluation.
> 
> To have something like this "short-circuit" and prevent argument evaluation 
> requires special support at the language level.
> 
> Cheers,
> Andrew
> 

> I hadn't even considered the possibility that it was not an Octave
command. I just thought it was undocumented.
>
> What is the easiest way to find out where it is coming from?
>
> I tried stepping into it but couldn't.
>
> Regards,
> Joe
>


Holy cow, it actually exists! My bad.

> What is the easiest way to find out where it is coming from?

The easiest way to find out where any function or named thing is coming
from is using the "which" command.

>> which ifelse
'ifelse' is a built-in function from the file libinterp/corefcn/data.cc

>> help ifelse
'ifelse' is a built-in function from the file libinterp/corefcn/data.cc

 -- merge (MASK, TVAL, FVAL)
 -- ifelse (MASK, TVAL, FVAL)
     Merge elements of TRUE_VAL and FALSE_VAL, depending on the value of
     MASK.


So, it actually is an Octave thing. But since it's a _function_, and not
a special syntax, it doesn't have the short-circuiting behavior you
want. That is, you might be familiar with the ternary operator
"condition ? if-case : else-case" from C/C++ and elsewhere.
Unfortunately, Octave's ifelse() doesn't work like that.

If you want real short-circuiting behavior, you'll have to use regular
"if/else" statements at the Octave/M-code syntax level. You can write
them as one-liners if you really want:

if 1==1; 0 else 1/0; end


No warning happens when you do it this way.

Cheers,
Andrew



reply via email to

[Prev in Thread] Current Thread [Next in Thread]