bad mod function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

bad mod function

vicnorton
The mod function in my Mac system is not working. This is my system
----------------------------------------------------------------------
GNU Octave Version: 4.4.0 (hg id: d703d8bbc6ae)
GNU Octave License: GNU General Public License
Operating System: Darwin 16.7.0
Darwin Kernel Version 16.7.0: Fri Apr 27 17:59:46 PDT 2018;
root:xnu-3789.73.13~1/RELEASE_X86_64 x86_64
----------------------------------------------------------------------
/usr/local/Cellar/octave/4.4.0_1 (2,189 files, 48.5MB) *
  Poured from bottle on 2018-06-16 at 10:40:43
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/octave.rb

The mod problem appears to be a very recent glitch. I’ve used mod quite recently with no problem.

Here is a script that shows the problem.

% the way mod is supposed to be defined
function v = modxy(x, y)  % to replace the faulty mod function
  v = x - y .* floor (x ./ y);
end

% example of the problem
fprintf('-- bad mod function ---\n');
for i = 1 : 11
  fprintf('%4d', i);
  if mod(i, 5) == 1; fprintf('\n'); end
end
fprintf('-- good mod function --\n');
for i = 1 : 11
  fprintf('%4d', i);
  if modxy(i, 5) == 1; fprintf('\n'); end
end
%{
output:
-- bad mod function ---
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
-- good mod function --
   1
   2   3   4   5   6
   7   8   9  10  11
%}

Regards,

Vic
Reply | Threaded
Open this post in threaded view
|

Re: bad mod function

Dmitri A. Sergatskov
On Tue, Jun 19, 2018 at 7:33 AM, Vic Norton <[hidden email]> wrote:

% example of the problem
fprintf('-- bad mod function ---\n');
for i = 1 : 11
  fprintf('%4d', i);
  if mod(i, 5) == 1; fprintf('\n'); end
end

​I have a slightly more recent hg version (which has essentially the same data.cc file
as 4.4.0) and I do not see the problem:

​octave
GNU Octave, version 4.4.0
<...>

octave:1> for i = 1 : 11
>   fprintf('%4d', i);
>   if mod(i, 5) == 1; fprintf('\n'); end
> end
   1
   2   3   4   5   6
   7   8   9  10  11

I suspect this is something specific to Mac. Can you file a bug report
against Homebrew?


Vic

​Sincerely,

​Dmitri.
--

Reply | Threaded
Open this post in threaded view
|

Re: bad mod function

Rik-4
In reply to this post by vicnorton
On 06/19/2018 08:45 AM, [hidden email] wrote:
Subject:
bad mod function - Matlab
From:
Vic Norton [hidden email]
Date:
06/19/2018 08:08 AM
To:
Octave Maintainers List [hidden email]
List-Post:
[hidden email]
Content-Transfer-Encoding:
quoted-printable
Precedence:
list
MIME-Version:
1.0 (Mac OS X Mail 10.3 \(3273\))
Message-ID:
[hidden email]
Content-Type:
text/plain; charset=utf-8
Message:
5

The mod function in my Mac system is not working.
———————————————————————————————————

Here is essentially the same script run with Matlab.
This is the way the mod function is supposed to work

%% testmod.m  - test the mod function
%    with Matlab '9.3.0.713579 (R2017b)'

fprintf('-- bad mod function? --\n');
for i = 1 : 11
 fprintf('%4d', i);
 if mod(i, 5) == 1; fprintf('\n'); end
end
fprintf('-- good mod function --\n');
for i = 1 : 11
 fprintf('%4d', i);
 if modxy(i, 5) == 1; fprintf('\n'); end
end

function v = modxy(x, y)  % to replace the faulty mod function
 v = x - y .* floor (x ./ y);
end

%{
testmod
-- bad mod function? --
  1
  2   3   4   5   6
  7   8   9  10  11
-- good mod function --
  1
  2   3   4   5   6
  7   8   9  10  11
%}


For the moment, skip the for loop and more complicated code.  What does the following return when entered on the Octave command line?

mod (5,5)
mod (6,5)
mod (7,5)

It should be 0, 1, and 2 respectively.  If it isn't then there is something wrong with the installation as Dmitri suggested.

--Rik
Reply | Threaded
Open this post in threaded view
|

Re: bad mod function

vicnorton

> On Jun 19, 2018, at 12:36 PM, Rik <[hidden email]> wrote:
>
> For the moment, skip the for loop and more complicated code.  What does the following return when entered on the Octave command line?
>
> mod (5,5)
> mod (6,5)
> mod (7,5)
>
> It should be 0, 1, and 2 respectively.  If it isn't then there is something wrong with the installation as Dmitri suggested.
>
> --Rik

Everything works the way it should.
octave> mod(5,5)
ans = 0
octave> mod(6,5)
ans =  1
octave> mod(7,5)
ans =  2
octave> mod(5,5) == 1
ans = 0
octave> mod(6,5) == 1
ans = 1
octave> mod(7,5) == 1
ans = 0

Now I’ll just enter the loop code:
octave> for i = 1 : 11
>  fprintf('%4d', i);
>  if mod(i, 5) == 1; fprintf('\n'); end
> end
   1
   2   3   4   5   6
   7   8   9  10  11

Very strange! Now everything works the way it should. There must be gremlins in my computer! And my original code that inspired all this works correctly as well. Apparently I just needed to restart Octave or the computer.

Regards,

Vic


Reply | Threaded
Open this post in threaded view
|

Re: bad mod function

Colin Macdonald-2
On 2018-06-19 10:07 AM, Vic Norton wrote:
> Very strange! Now everything works the way it should. There must be gremlins in my computer! And my original code that inspired all this works correctly as well. Apparently I just needed to restart Octave or the computer.

Whenever I glimpse these gremlins its usually because I had a file
"modxy.m" whose first line was "function mod(...)".  And now there is a
new "mod" in Octave's namespace.

And IIRC, it persists even if I fix "function mod(...)" to "function
modxy(...)", until I restart Octave---begone gremlins.

Colin

Reply | Threaded
Open this post in threaded view
|

Re: bad mod function

Ian McCallion
This could explain some unexpected behaviours I have experienced. Does 

   clear functions 

not kill the gremlin?

Cheers... Ian

On Tue, 19 Jun 2018, 19:20 Colin Macdonald, <[hidden email]> wrote:
On 2018-06-19 10:07 AM, Vic Norton wrote:
> Very strange! Now everything works the way it should. There must be gremlins in my computer! And my original code that inspired all this works correctly as well. Apparently I just needed to restart Octave or the computer.

Whenever I glimpse these gremlins its usually because I had a file
"modxy.m" whose first line was "function mod(...)".  And now there is a
new "mod" in Octave's namespace.

And IIRC, it persists even if I fix "function mod(...)" to "function
modxy(...)", until I restart Octave---begone gremlins.

Colin