cancel
Showing results for
Did you mean:
Highlighted
New member
2 1 0 0
Message 1 of 21
1,367
Flag Post
HP Recommended

# HP Prime - Bug - Modular Arithmetic Error

HP Prime

When computing some results using the MOD (modulo) operator I found there were errors.

There is no explicit error message the results are just wrong.

Take this simple example :

1984^1 MOD 2237 = 1984 - ok

1984^2 MOD 2237 = 1373 - ok

1984^3 MOD 2237 = 1603 - ok

1984^4 MOD 2237 = 1539 - not ok - this should be 1575

1984^5 MOD 2237 = 1709 - not ok - this should be 1948

1984^6 MOD 2237 ...

...

1984^23 MOD 2237 = 558 - not ok - this should be 1165

From the fourth power all results are just plain wrong.

From my understanding I believe that the left side (power) overflows or loses precision before evaluating the modulus,

but if this was the case the user should be notified that the result will/may be wrong.

Please fix this or at least have an error message when the left side is too big and that the modulus is computed on an approximate value or an overflow.

After some thought this is probably an error due to the approximate representation of the left hand side after rising this to the power of.

But still a warning or indication that the value is being rounded would be welcome.

Thank you.

Tags (1)
20 REPLIES 20
Highlighted
Level 8
544 531 94 170
Message 2 of 21
1,353
Flag Post
HP Recommended

## HP Prime - Bug - Modular Arithmetic Error

Short answer: DO it in CAS, not in Home, and you'll get what you want.

More complete answer: Not a bug.  You're getting roundoff errors because you're doing those calculations in Home view, which is designed to only handle up to 12-digit-long mantissas.  Any integer greater than 999999999999 will be rounded off to the closest possible 12-digit-mantissa (and displayed in scientific notation).  So it is not possible to calculate 1984^4 exactly in Home, since the exact answer is 14 digits long (15494111297536).  Home rounded it to 15494111297500, and that is what you took MOD 2237 of.

Whenever you want to work with EXACT integers, be sure to use CAS view, not Home view.  CAS can handle spectacularly long integers exactly. In CAS, all of the problems you listed return the results that you expected.

In general, use Home for real-world, floating-point, approximate, rounded-to-12-significant-digits number crunching.  Use CAS for symbolic math (e.g. equation solving using variables), and exact math (e.g. long integers, including ratios of long integers), and combinations of the two (e.g. algebraic expressions with exact coefficients, exact powers, etc).  Hint: Be sure to NEVER include a decimal point anywhere in CAS expressions; they flip CAS into approximate mode without warning.

Hope this helps!  Disclaimer: I don't work for HP. I'm just another happy HP calculator enthusiast.

-Joe-
Highlighted
Level 8
544 531 94 170
Message 3 of 21
1,351
Flag Post
HP Recommended

## HP Prime - Bug - Modular Arithmetic Error

Another thought: Prime's POWMOD function gets the correct answers even in Home:

powmod(1984,23,2237) --> 1165

POWMOD uses Number Theory tricks that allow powers far greater than even CAS allows.

Example: What are the last 10 digits of 12345^56789?

powmod(12345,56789,10^10) --> 2822265625

-Joe-
Highlighted
Level 9
970 953 119 185
Message 4 of 21
1,329
Flag Post
HP Recommended

## HP Prime - Bug - Modular Arithmetic Error

@Joe_Horn wrote:

Short answer: DO it in CAS, not in Home, and you'll get what you want.

More complete answer: Not a bug.

Wrong!!

It IS a bug!

The bug is that input range checking is not done on the MOD functions. This affects ALL HP calculators that I have tested that have this function (for those with CAS it affects non-CAS mode or Approx. mode).

All competitor calculators that I have tested give an error when the inputs are out-of-range, which is what HP should have done when they first implemented the function.

Regards.

-Bart
_________________________________________________________
calculator enthusiast
Highlighted
New member
2 1 0 0
Message 5 of 21
1,320
Flag Post
HP Recommended

## HP Prime - Bug - Modular Arithmetic Error

Thank you for your answers, I understand the problem, a warning would have been nice.

I didn't know home and CAS were different (I didn't use the HP Prime more than a few times, since I usually use a HP50g)

But I will keep this in mind and work in CAS when needed.

Thank you.
Highlighted
Level 9
3,551 3,490 180 537
Message 6 of 21
1,304
Flag Post
HP Recommended

## HP Prime - Bug - Modular Arithmetic Error

This must be compute, in CAS MODE.

If check with Wolfram Alpha, is ...

With HP PRIME GRAPHING CALCULATOR, is ...

Kind Regards !.
Have a nice day !.
Provost in HP Spanish Public Forum ... https://h30467.www3.hp.com/
Highlighted
Level 8
544 531 94 170
Message 7 of 21
1,292
Flag Post
HP Recommended

## HP Prime - Bug - Modular Arithmetic Error

@BartdB wrote:

Joe_Horn wrote: Not a bug.

Wrong!!

It IS a bug!

The bug is that input range checking is not done on the MOD functions. This affects ALL HP calculators that I have tested that have this function (for those with CAS it affects non-CAS mode or Approx. mode).

All competitor calculators that I have tested give an error when the inputs are out-of-range, which is what HP should have done when they first implemented the function.

You're assuming that all inputs to MOD which are greater than or equal to 10^12 are "out of range"... but they're not. HP calculators allow you to use them exactly as they are, and HP returns the correct answers for them.

Example: What is 10^50 MOD 7?  HP calculators (even the lowly HP 35S) return the correct answer of 2, rather than saying "Out Of Range" like the competition's inferior calculators. 😉

So, as the old saying goes, it's not a bug, it's a feature.

-Joe-
Highlighted
Level 9
3,551 3,490 180 537
Message 8 of 21
1,270
Flag Post
HP Recommended

## HP Prime - Bug - Modular Arithmetic Error

Spoiler

In HP PRIME GRAPHING CALCULATOR your example, is True, not Out of Range and not  is a bug. In the HP41CV, HP28C, HP28S, HP75C, HP48G, HP48G+, HP48GX, HP49G, HP49G+, HP48GII, HP50G, etc ..., is same.

With, Wolfram Alpha ...

In HP PRIME GRAPHING CALCULATOR, is same ...

See ... Fast modular exponentiation ... https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/fast-modular-exp...

Kind Regards !.
Have a nice day !.
Provost in HP Spanish Public Forum ... https://h30467.www3.hp.com/
Level 9
970 953 119 185
Message 9 of 21
1,222
Flag Post
HP Recommended

## HP Prime - Bug - Modular Arithmetic Error

Hi Joe,

😉 nice example, the HP-35s gets it right by accident, not design. Try RMDR(79^10,85), the 35s will give 45 when the correct answer is 66.

Also on the HP-50g in Numeric/Approx mode: ' 79^10 MOD 85' will also give the erroneous answer of 45. As will the 39gii (and Prime(*) in HOME, and and and)

*) using MOD and not POWMOD - how will the user know to use POWMOD (or CAS mode) if MOD just spews out an answer anyway?

Now whenever I am instructing my fellow engineers, I am always reminding them to check the sensibility of their answer, but:

a) other functions on the calculator have input range checks, so this lacks consistency.

b) it is difficult to check sensibility of answers on this function, particularly when the evaluated number above 1E12 is not directly visible, especially with algebraic entry (hence my use of algebraic entry in my example above).

And when they start calling them features, they are just bugs that "the powers that be" don't want to fix 😛

@Joe_Horn wrote:

You're assuming that all inputs to MOD which are greater than or equal to 10^12 are "out of range"... but they're not. HP calculators allow you to use them exactly as they are, and HP returns the correct answers for them.

Example: What is 10^50 MOD 7?  HP calculators (even the lowly HP 35S) return the correct answer of 2, rather than saying "Out Of Range" like the competition's inferior calculators. 😉

So, as the old saying goes, it's not a bug, it's a feature.

-Bart
_________________________________________________________
calculator enthusiast
Highlighted
Level 6
305 297 32 75
Message 10 of 21
1,210
Flag Post
HP Recommended

## HP Prime - Bug - Modular Arithmetic Error

In my opinion the Prime in Home mode is not as bad as you suggest BartdB, because a straightforward input gives a correct result.

So when someone would insist on using Home mode for this kind of problems instead of CAS, where CAS is evidently the right choice in this case, the solution is simply to  split the problem in different steps, and each step turns out to be correct.

For instance, in Home mode the Prime gives:

Correct!

Be alert for scammers posting fake support phone numbers on the community. If you think you have received a fake HP Support message, please report it to us by clicking on "Flag Post".
† The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation