- HP Community
- >
- Tablets
- >
- Calculators
- >
- Re: HP Prime - Bug - Modular Arithmetic Error

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

# HP Prime - Bug - Modular Arithmetic Error

01-10-2018 06:11 AM - edited 01-10-2018 06:12 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

## HP Prime - Bug - Modular Arithmetic Error

01-10-2018 07:08 AM

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 (154941112975__36__). Home rounded it to 154941112975__00__, 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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

## HP Prime - Bug - Modular Arithmetic Error

01-10-2018 07:12 AM - edited 01-10-2018 07:20 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

## HP Prime - Bug - Modular Arithmetic Error

01-10-2018 09:34 AM - edited 01-10-2018 09:35 AM

@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.

_________________________________________________________

calculator enthusiast

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

## HP Prime - Bug - Modular Arithmetic Error

01-10-2018 10:10 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

## HP Prime - Bug - Modular Arithmetic Error

01-10-2018 04:18 PM - edited 01-10-2018 04:19 PM

This must be compute, in CAS MODE.

If check with Wolfram Alpha, is ...

With HP PRIME GRAPHING CALCULATOR, is ...

Have a nice day !.

@Maké (Technical Advisor Premium - HP Program Top Contributor).

Provost in HP Spanish Public Forum ... https://h30467.www3.hp.com/

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

## HP Prime - Bug - Modular Arithmetic Error

01-10-2018 08:29 PM

@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

HP calculators that I have tested that have this function (for those with CAS it affects non-CAS mode or Approx. mode).ALL

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

## HP Prime - Bug - Modular Arithmetic Error

01-11-2018 09:10 AM - edited 01-11-2018 01:13 PM

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...

Have a nice day !.

@Maké (Technical Advisor Premium - HP Program Top Contributor).

Provost in HP Spanish Public Forum ... https://h30467.www3.hp.com/

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

## HP Prime - Bug - Modular Arithmetic Error

01-12-2018 10:05 AM

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.

_________________________________________________________

calculator enthusiast

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Flag Post

## HP Prime - Bug - Modular Arithmetic Error

01-12-2018 11:22 AM

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".

Didn't find what you were looking for? Ask the community