HP Forums

Full Version: Strange results for MOD in CAS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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 ???
Indeed, I did not realize that b could be negative in a MOD b.
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).
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.
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.
Reference URL's