HP Forums
Strange results for MOD in CAS - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Strange results for MOD in CAS (/thread-21355.html)



Strange results for MOD in CAS - Joe Horn - 02-25-2024 08:15 AM

In CAS view:
(-27) MOD (-5) --> -7

Huh???

Although -7 is in fact congruent to -27 (mod -5), wouldn't an output of -2 (as returned in Home view) be reasonably expected as the result?

Disclaimer: The above output is produced by several recent firmware versions, but might not be produced by all versions.


RE: Strange results for MOD in CAS - Albert Chan - 02-25-2024 02:30 PM

Perhaps you mean irem(-27, -5)?

Yes. this bug is added to latest HP emulator.

2.1.14181 (2018 10 6) Cas> irem(-27, -5)      → 3
2.1.14730 (2023 4 13) Cas> irem(-27, -5)      → -7 ???

The bug was added to latest XCas too.

XCas 1-5-0> irem(-27, -5)      → 3
XCas 1-9-0> irem(-27, -5)      → -7 ???


RE: Strange results for MOD in CAS - parisse - 02-27-2024 12:22 PM

Indeed, I did not realize that b could be negative in a MOD b.


RE: Strange results for MOD in CAS - komame - 03-03-2024 08:54 AM

Does the "mod" (written in lowercase) used in CAS have different functionality depending on some settings? I have two sessions of the Virtual Calculator. In the first one, when I enter 27 mod 5 in CAS, it returns the result "2", with the original command automatically changed to "irem(27,5)" at the left side. However, in the second session, entering the same command in CAS gives the result "2 %% 5", and the original command changes to "%%(27,5)". I don't see any differences in the CAS settings, but the same command works differently.
[attachment=13326]
What could be the reason for this?

Executing this on the HOME side changes "mod" to "MOD" and always returns the result 2 there (as expected).


RE: Strange results for MOD in CAS - Albert Chan - 03-03-2024 11:10 AM

Hi, komame

That explained it! There are actually 2 bugs (1st based on your session output)

1. Cas (a mod b) sometimes switch to irem(a,b)
2. irem(a,b) return negative numbers.


RE: Strange results for MOD in CAS - parisse - 03-03-2024 07:32 PM

MOD is not a synonym of mod in CAS.

If xcas_mode()==0, mod will build an element of Z/nZ, like 27 mod 5 -> 2 %% 5.
If xcas_mode()>0, mod will call irem and return an element of Z; 27 mod 5 -> 2
The difference will become clear if you make further operations on the result, e.g. multiply by 3.
It's unclear for me how xcas_mode can be modified on the Prime...

MOD in CAS will call _MODULO in rpn.cc, it's not the same code as irem, I don't remember the difference, probably _MODULO behaves more like MOD in HOME.