HP Forums
[WP34s] 2 new modulo functions - Printable Version

+- HP Forums (https://archived.hpcalc.org/museumforum)
+-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html)
+--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html)
+--- Thread: [WP34s] 2 new modulo functions (/thread-225701.html)



[WP34s] 2 new modulo functions - fhub - 06-26-2012

Y.Z%X and Z^Y%X

Wouldn't Z.Y%X for the first one be better and more consistent?

Franz


Re: [WP34s] 2 new modulo functions - Walter B - 06-26-2012

Quote:
Y.Z%X and Z^Y%X

Wouldn't Z.Y%X for the first one be better and more consistent?


Hmmmh, RTFM for their real names and functionality ;-)

Edited: 26 June 2012, 7:58 a.m.


Re: [WP34s] 2 new modulo functions - fhub - 06-26-2012

Quote:
Hmmmh, RTFM for their real names and functionality ;-)

Well, for me the file wp34s.op in the SVN has priority over your FM.


Re: [WP34s] 2 new modulo functions - Paul Dale - 06-26-2012

Where did those names come from? I did use them briefly but changed them, I thought, before I committed the code.

I agree the nomenclature was inconsistent and generally poor. That's why they were changed.


- Pauli


Re: [WP34s] 2 new modulo functions - fhub - 06-26-2012

Quote:
Where did those names come from? I did use them briefly but changed them, I thought, before I committed the code.

They are still in the latest wp34s.op (SVN 3172).


Re: [WP34s] 2 new modulo functions - Paul Dale - 06-26-2012

I must have committed an obsolete opcode file by mistake.

All corrected now, these two have their proper names now.


They only function in integer mode and require three positive arguments, the modulus being greater than 1.

I'd have liked to make their support more widspread (negatives & reals) but we lack the flash space. Still, I hope someone finds the useful.


- Pauli


Re: [WP34s] 2 new modulo functions - Paul Dale - 06-26-2012

SVN 3176 is the latest now :)

- Pauli


Re: [WP34s] 2 new modulo functions - Reth - 06-26-2012

And what about the (half-promised earlier) MOD function hp41, hp42 style? Shouldn't be that hard to put in? Like RMDR x<0? RCL-L.

Cheers,


Re: [WP34s] 2 new modulo functions - Paul Dale - 06-26-2012

The difference is these functions already existed in flash but weren't exposed. They are required for the primality tester.

MOD would be new code that has to work in integer and real modes and do the appropriate things with carry & overflow (whatever they are).

The code to do MOD is straightforward:

        a MOD b = a RMDR b + (if a*b < 0 then b)


- Pauli


Re: [WP34s] 2 new modulo functions - Reth - 06-26-2012

Thanks, waiting for it.


Re: [WP34s] 2 new modulo functions - Paul Dale - 06-26-2012

I didn't commit to doing it :-)

A user code routine in the library is possible now.


- Pauli


Re: [WP34s] 2 new modulo functions - Paul Dale - 06-26-2012

How about this:

	LBL'MOD'
LocR 010
[cmplx]STO .00
RMDR
STOS .02
[cmplx]RCL .00
SIGN
x[<->] Y
SIGN
x=? Y
SKIP 005
RCL .00
STO+ .02
STO L
RCLS .02
RTN
RCL .00
BACK 004
END

Put it into flash and call via XEQ'MOD'


- Pauli


Re: [WP34s] 2 new modulo functions - Neil Hamilton (Ottawa) - 06-27-2012

Whoa! That looks way more complicated than the one I have used for quite some time (from the Assembler manual, Figure 4.2):

// Source file(s): wp34s-lib.dat
0001 LBL'MOD'
0002 RMDR
0003 RCL L
0004 x[<->] Y
0005 x<0?
0006 +
0007 RTN
0008 END
// 8 total words used.
// 9 total words used.
// 7 single word instructions.
// 1 double word instructions.



Re: [WP34s] 2 new modulo functions - Paul Dale - 06-27-2012

But my version works :-)

Try -16 ENTER -3 XEQ'MOD'

Your version returns -4 which is clearing incorrect.
Mine returns -1 which is.

Mine also sets L correctly and preserves the stack properly.

Inputs  RMDR  MOD
-16 -3 -1 -1
16 -3 1 -2
-16 3 -1 2
16 3 1 1

You have to add the modulus back in only if the signs of the original numbers are different -- your code doesn't do this.

Still, I'm sure it can be shaved a few instructions.

- Pauli

Edited: 27 June 2012, 5:51 p.m.


Re: [WP34s] 2 new modulo functions - Neil Hamilton (Ottawa) - 06-27-2012

In all my years working with modular numbers in cryptographic applications I have never heard of a negative modulus! Very new to me. I am not sure what this would mean in physical sense considering a modular set is usually described as [0..(modulus-1)]. Perhaps I am too sheltered. :-)

Alas, mine does all I need it to do. YMMV


Re: [WP34s] 2 new modulo functions - Paul Dale - 06-27-2012

I'm just emulating the behaviour of the 41/42 MOD function which deals with negative modulus this way. Might as well do a complete job or not at all :)


- Pauli


Re: [WP34s] 2 new modulo functions - fhub - 06-27-2012

Quote:
In all my years working with modular numbers in cryptographic applications I have never heard of a negative modulus!

Yes, you're right in a mathematical sense, there a modulus is in fact defined as positive number. But in mathematics (and also in your menttioned crypto apps) I doubt that negative numbers in modulo calculations are usual at all.

But for computers and calculators MOD is really defined as Pauli showed in his list: MOD is negative if both arguments have different signs. I've tried it with 4 different calcultors (HP50g, HP39gII, TI-92+ and TI-Nspire), and all 4 return exactly the values of Pauli's table.

BTW, that's exactly why I don't think this RMDR function is very useful when all other calcs have the usual MOD function.

Franz


Re: [WP34s] 2 new modulo functions - Neil Hamilton (Ottawa) - 06-27-2012

I really should know better than to argue with you on mathematical grounds! Once again, you are absolutely correct! I looked up the IEEE 754 definition and, lo and behold, negative modulus is OK. Cool!

Maybe it is the modular equivalent of an imaginary number... :-)

Mind you, I think I will stick with my simpler version since, for all practical applications that I use it for, it was a treat -- and I can understand it.

Cheers...


Re: [WP34s] 2 new modulo functions - fhub - 06-27-2012

Quote:
Still, I'm sure it can be shaved a few instructions.

I've made a version with 2 steps less, and it only needs 2 local regs (no need to save the complete stack) and also preserves LastX. Without LastX it would even be 5 steps less.

LBL'MOD'
LocR 02
cpx STO .00
RMDR
RCL L
cpx x<> .00
SIGN
x<>y
SIGN
-
x!=0?
x<> .00
RCL+ .01
RCL .00
STO L
DROP
END

Franz


Edited: 27 June 2012, 7:00 p.m.


Re: [WP34s] H.MS mode - Reth - 06-28-2012

Thanks again for that. I've strictly specific use of MOD so it's ok.

Another little thing, have you guys considered some different settings to the H.MS display format? (For example through flags - we've got so many of them, I can't imagine who would need them.) I'd have some use of it if it showed rounded to the nearest second result.

Cheers,