Row Swapping With Nonexistent Rows

Post here to share useful tips and tricks, to ask questions about using your DM42 or to report software-related problems
Post Reply
Epidiah
Posts: 21
Joined: Mon Jan 29, 2018 3:08 am

Row Swapping With Nonexistent Rows

Post by Epidiah »

I was writing a program to create random permutation matrix by taking an identity matrix and going through a row at a time, swapping the current row with a random row further down the matrix. I did a little arithmetic wrong and instead of choosing a random row from the current to the end of the matrix, I was choosing a random row starting one past the current row and going to one past the end of the matrix. I got some odd results!

You can try this at home (though I'm not sure that's advised, see below):

Code: Select all

01 LBL "TEST"
02 3
03 ENTER
04 NEWMAT
05 STO "TEST"
06 INDEX "TEST"
07 1
08 4
09 R<>R
10 EDITN "TEST"
11 END
I ran this a few times. Row 1 would be filled with 0s, very large numbers, very small numbers, or blank alpha characters "". And then it locked up. When I reset, it gave me this code and asked me to report it: Reg Id d3770103, fw 3.12

Oops! So, don't try this unless you've got a saved state to return to, I guess.

Same thing happens when I try it on the Free42 app. I don't have an HP 42s, so I can't check that to see if it is expected behavior. The manual for the 42s doesn't specifically mention any error code that should happen under these circumstances. So who knows?
Dave Britten
Posts: 137
Joined: Wed Jun 14, 2017 9:27 pm

Re: Row Swapping With Nonexistent Rows

Post by Dave Britten »

I just tested this on my 42S, and the R<>R step fails with "Dimension Error". Free42 should ideally do the same dimension checking, then.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Row Swapping With Nonexistent Rows

Post by Thomas Okken »

Dave Britten wrote:
Fri Oct 11, 2019 9:07 pm
I just tested this on my 42S, and the R<>R step fails with "Dimension Error". Free42 should ideally do the same dimension checking, then.
It should! But it turns out there is an off-by-one error in that dimension check; it complains about index >= dimension + 2, but it allows index = dimension + 1. Yikes. Bug fix release coming up...
Epidiah
Posts: 21
Joined: Mon Jan 29, 2018 3:08 am

Re: Row Swapping With Nonexistent Rows

Post by Epidiah »

Thank you!

Looks like I managed to hop right into that edge case. I'll endeavor to be less sloppy with my arithmetic in the future.
Thomas Okken
Posts: 1100
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Row Swapping With Nonexistent Rows

Post by Thomas Okken »

You were no sloppier than I was when I coded that range check! :oops:
Dave Britten
Posts: 137
Joined: Wed Jun 14, 2017 9:27 pm

Re: Row Swapping With Nonexistent Rows

Post by Dave Britten »

It seems one off-by-one error revealed another off-by-one error. ;)
Post Reply