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:03:52 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.0


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



reply via email to

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