Modulus

Popular in previous forums place your programming snippets here
Post Reply
User avatar
Zenerdiode
Posts: 81
Joined: Wed Jan 04, 2023 12:45 am
Location: Newcastle, England

Modulus

Post by Zenerdiode »

I was surprised to see that OPL didn’t have a MOD function. I could have sworn it had, but my pea brain is shrivelling. For those who don’t know, a modulo operation returns the remainder of a division. So 7 MOD 3 = 1 (a quotient of 2, remainder 1).

Very useful in programming is a modulus of a power of 2 - x MOD 16 or x MOD 4 etc. So here’s a quick and easy function for OPL:

MOD:
Syntax: x%=MOD%:(y%,z%)
Returns the modulus of the dividend y% divided by the divisor z%.

Code: Select all

MOD%:(NUM%,DIV%)
RETURN NUM%-((NUM%/DIV%)*DIV%)
Christopher. - Check out my TRAP message, it’s not difficult to decode and is sometimes uttered under the breath when said message appears… :|
User avatar
NotFitForPurpose
Posts: 90
Joined: Tue Sep 03, 2024 12:06 pm
Contact:

Re: Modulus

Post by NotFitForPurpose »

:D
The number of times I have gone looking for that and not found it. Then spent far too much time testing.

I will add that to my git hub gists. Cheers.
User avatar
Martin
Global Admin
Posts: 401
Joined: Mon Jan 02, 2023 5:18 pm

Modulus

Post by Martin »

Hi Chaps.. Another good spot...

I'm not sure if it is a Psion oversight or not as both the Organiser (one) Utility Pack and the Pocket Spreadsheet have it.

Organiser (I) Utility pack
MOD (2) Modulo. Calculates the remainder after dividing the first parameter by the second. e.g. MOD(20,8) is translated into 20-8*INT(20/8) which works out as 4. So that MOD(20,8)=4. The second parameter must not be equal to zero.

Spreadsheet
MOD(<exp1>,<exp2>) Returns the remainder from <exp1> divided by <exp2> (modulo). The arguments may be numbers or cell references.


Chris after reading the Utility Pack warning.. in your snippet, could we add in a check for '251 Divide by Zero' error?

Sincerely
Martin
User avatar
Zenerdiode
Posts: 81
Joined: Wed Jan 04, 2023 12:45 am
Location: Newcastle, England

Re: Modulus

Post by Zenerdiode »

NotFitForPurpose wrote: Mon Jan 06, 2025 11:41 pm :D
The number of times I have gone looking for that and not found it. Then spent far too much time testing.

I will add that to my git hub gists. Cheers.
That’s exactly what prompted me to write it and upload it. I’m humbled it makes your Git.
Martin wrote: Tue Jan 07, 2025 8:42 am …and the Pocket Spreadsheet have it.
There it is! I knew I had read it in PSION format somewhere - I actually bought the Pocket Spreadsheet brand new when I was a kid. I’m also amazed that my snippet is exactly how PSION did it, that’s by fluke than design.
Martin wrote: Tue Jan 07, 2025 8:42 am Chris after reading the Utility Pack warning.. in your snippet, could we add in a check for '251 Divide by Zero' error?
I left it out because in my use case I am feeding the divisor a constant; and I also figured a div/0 would either be raised or handled in the main thread.
Christopher. - Check out my TRAP message, it’s not difficult to decode and is sometimes uttered under the breath when said message appears… :|
User avatar
Martin
Global Admin
Posts: 401
Joined: Mon Jan 02, 2023 5:18 pm

Pocket Spreadsheet Import / Export

Post by Martin »

Zenerdiode wrote: Tue Jan 07, 2025 6:31 pm There it is! I knew I had read it in PSION format somewhere - I actually bought the Pocket Spreadsheet brand new when I was a kid. I’m also amazed that my snippet is exactly how PSION did it, that’s by fluke than design.
If you are still 'passionate' about the pocket spreadsheet (and for others watching).

The biggest issue to overcome to make it seriously useful is the getting the 'data' on and off the Organiser. In the (good) old days you could pass your spreadsheet to Lotus 123 and from there we could send it to PC Four Abacus for example. Now of course Excel wont have anything to do with old spreadsheet software files because of the macro viruses. BUT all is not lost...

If you try different PC Comms servers then you will be presented with different Import and Export file formats - for example

CL.EXE (v211) gives
Wks Wk1 Wr1
Dif

Boris's Psi2Win gives
Wks Dif
Csv Txt
Txt(tab)

BUT best of all ORG-Link. After working
with Lostgallifreyan he simply created...
Formula or Numeral

After you have configured the directory and file extensions in ORG-Link you either get a text representation of your spreadsheet or remarkably a file you can open in Excel (or any other) with all the formula intact. You still need to redo the column widths but then again you always did.

Below is a list of the spreadsheet functions that we used to test ORG-Link and is now included in the Republished Spreadsheet Manual. MOD is there look.
.
Spreadsheet Functions_reduced.jpg
You do not have the required permissions to view the files attached to this post.
Post Reply