HP Forums

Full Version: Bug with Integers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Today I thought of writing some simple programs to work with integers on my prime, so I first tried something like 7^23 MOD 143, the first part is correctly computed and gives 27368747340080916343. But then 7^23 Mod 143 on the real calculator, the PC and android deliver 23 (in Cas and Home) which simply is wrong as it must be 2, confirmed by xcas, my old and trusty (sad that it has defective keys) 49G+ and even Geogebra.
This is the first time that I encounter a mathematical error on the prime, but this renders it nearly unusable, as from now on I will clearly double-check every calculation that I do until I know that mistakes using numbers are fixed, unbearable.
I can live with bugs, find matching workarounds as long as the problems are not essential, but a device for doing maths has to correctly do what its main purpose (crunching numbers) is, especially when a function is built-in and quite simple to program.
Arno
I get the correct answer (2, in CAS).

[attachment=2451]

What are your settings?
Now try it in HOME

7^23 MOD 143 ==> 23

(r8151 Default settings)
Hm, that's annoying, CAS settings : Radians, Standard number format, Integers: Decimal, Simplify: Minimum, Exact:on,use (sqrt) On Principal. on, Page 2 : 5,1,100,0.00000000000001, 1E-15,40. Home: Rad, Standard. 123456.789, Textbook, Decimal, 32 a+b*i, English. I reset the calc and still got the same results.
The only setting concerning integers are Integers on both pages which I now tried to change without any success. Os is the latest, that is 8151.
Thanks for your reply
Arno
(08-20-2015 09:59 PM)DrD Wrote: [ -> ]Now try it in HOME

7^23 MOD 143 ==> 23

(r8151 Default settings)

Perhaps Helge is using an older version, and I get this in Home and CAS,I think hardware does not matter here, this is the result I get from real calc, Pc and Android.
Thank you
Arno
(08-20-2015 10:06 PM)Arno K Wrote: [ -> ]
(08-20-2015 09:59 PM)DrD Wrote: [ -> ]Now try it in HOME

7^23 MOD 143 ==> 23

(r8151 Default settings)

Perhaps Helge is using an older version, and I get this in Home and CAS,I think hardware does not matter here, this is the result I get from real calc, Pc and Android.
Thank you
Arno

Well, in Home this result is normal: I get 23 also on my 48GX. You are limited by the number of significant digits. You cannot fully express 7^23 with 12 significant digits.
On my 41CX I get 66 and on the WP 34S I get 84.

In CAS I get 2, as Helge, both on the real and the virtual calculators and I have the latest Revision: 8151.

Juste one question: are you entering MOD in upper case in CAS ?
Now try it in CAS approximate mode:

7^23 MOD 143 ==> 131072

Lower case mod results in syntax error. VC toolbox MOD produces mod. Lots of interesting trivia here.

-Dale-
(08-20-2015 11:08 PM)DrD Wrote: [ -> ]Now try it in CAS approximate mode:

7^23 MOD 143 ==> 131072

Lower case mod results in syntax error. VC toolbox MOD produces mod. Lots of interesting trivia here.

-Dale-

Clearly Home is for number-crunching(physics, engineering) but in cas-mode the entry mod(7^23, 143) is changed to 7^23 MOD 143 for display, this provides 23 in textbook as well as in algebraic entry mode, manually entered:7^23 MOD 143 provides 2. Interesting and confusing, what does a program using these commands do, well, I have not tested it, MOD/mod is a command that has to work and not to be tested, moreover, everytime I start something on the prime I encounter some unawaited difficulties, here I need to rewrite MOD, some days ago I wanted to make a program providing results for different numerical integrations where I had to use 4 cas-functions, on the 49G+ it was more difficult to get things running, but when I found out how to do, things ALWAYS worked as expected AND like I wanted. How much do I want a 50G using this hardware...
Perhaps I´ll get the emu for my mobile (which is faster than the real device) and go on with the restricted display.
Arno
FWIW, on the 50g,

7 ENTER 23 Y^X 143 MOD ==> 2
'7 Y^X 23 MOD 143' ENTER EVAL ==> 2

but

'7 Y^X 23 MOD 143' ENTER ->NUM ==> 23.

Note the decimal point.

Does the Prime not have an option to always show a decimal point on an floating point value? Otherwise how do you know if you're looking at a (precise) integral value or an approximation (potentially with rounding error or overflow)?
(08-20-2015 10:31 PM)Didier Lachieze Wrote: [ -> ]
(08-20-2015 10:06 PM)Arno K Wrote: [ -> ]Perhaps Helge is using an older version, and I get this in Home and CAS,I think hardware does not matter here, this is the result I get from real calc, Pc and Android.
Thank you
Arno

Well, in Home this result is normal: I get 23 also on my 48GX. You are limited by the number of significant digits. You cannot fully express 7^23 with 12 significant digits.
On my 41CX I get 66 and on the WP 34S I get 84.

In CAS I get 2, as Helge, both on the real and the virtual calculators and I have the latest Revision: 8151.

Juste one question: are you entering MOD in upper case in CAS ?

As you can read in my answer to drd I now tried entering that directly from the keyboard, that in fact provides the correct result, but: Shouldn't a calculator like the prime provide correct results withOUT regard to one of two possible entry versions.
Arno
P.S. This is not my first HP, I owned a 32S 48, 49 and a 49G+ before, a TI 57, 92, 92PLUS, casio Pb100=fx702, sharp PC1401,1403, 500....they all never provided that false answers
(08-21-2015 12:00 AM)nlj Wrote: [ -> ]FWIW, on the 50g,

7 ENTER 23 Y^X 143 MOD ==> 2
'7 Y^X 23 MOD 143' ENTER EVAL ==> 2

but

'7 Y^X 23 MOD 143' ENTER ->NUM ==> 23.

Note the decimal point.

Does the Prime not have an option to always show a decimal point on an floating point value? Otherwise how do you know if you're looking at a (precise) integral value or an approximation (potentially with rounding error or overflow)?

I haven't tried that with approx mode on on the 49G+, in exact mode this calc provided the awaited 2, interesting that approx mode delivers the 23 like the prime does. On the prime the problem seems to lie deeper in the system, both exact and approx with the CAS-order (mod(...)) deliver 23, only the (non-CAS)-command a MOD is correct, this must be manually entered and display with both possibilities is the same: 7^23 MOD 143, manually entered provides 2, mod chosen from catalog: 23. So I really am annoyed.
THX for your ideas
Arno
(08-21-2015 12:09 AM)Arno K Wrote: [ -> ]I haven't tried that with approx mode on on the 49G+, in exact mode this calc provided the awaited 2, interesting that approx mode delivers the 23 like the prime does.

Well, in approximate mode 23 is the correct answer (for some value of correct, and extending the meaning of modulus to rational numbers):

7^23 ==> 2.73687473401E19
2.73687473401E19 MOD 143 ==> 23.

(08-21-2015 12:09 AM)Arno K Wrote: [ -> ]On the prime the problem seems to lie deeper in the system, both exact and approx with the CAS-order (mod(...)) deliver 23, only the (non-CAS)-command a MOD is correct, this must be manually entered and display with both possibilities is the same

The Home/CAS dichotomy on the Prime is worse than a multiple personality disorder; more like a severed corpus callosum. I'm not sure if the complexity of this model causes more headaches and confusion for users or for the poor developers who have to make this untidiness work inside, but I'm utterly amazed that it's resulted in so _few_ bugs!
As pointed out above, you can't expect a long integer modulo 143 to return the correct result in Home, or in a Home program, because long integers aren't supported in Home.

Besides the manual entry in CAS, a CAS program works just fine, e.g.,

#cas
TEST(k,l):=
k^l MOD 143;
END;
#end

At first, coming from the 48/49/50 environment, I didn't like the CAS/Home dichotomy either, but I have now learned to appreciate it. For sheer number crunching, Home is ideal since CAS doesn't get in the way, and for everything else, I use the built-in command power of CAS, which makes most day-to-day math tasks a cinch!
(08-21-2015 01:55 AM)Helge Gabert Wrote: [ -> ]As pointed out above, you can't expect a long integer modulo 143 to return the correct result in Home, or in a Home program, because long integers aren't supported in Home.

Besides the manual entry in CAS, a CAS program works just fine, e.g.,

#cas
TEST(k,l):=
k^l MOD 143;
END;
#end

At first, coming from the 48/49/50 environment, I didn't like the CAS/Home dichotomy either, but I have now learned to appreciate it. For sheer number crunching, Home is ideal since CAS doesn't get in the way, and for everything else, I use the built-in command power of CAS, which makes most day-to-day math tasks a cinch!

Thank you, a little program like that is what I wanted to do today, you are right, I also use Home for physics... and cas for math, but this has happened in cas, as I know that home doesn't handle long integers correctly, I then tried home to find out.
Arno
(08-21-2015 08:54 AM)Arno K Wrote: [ -> ]I also use Home for physics... and cas for math, but this has happened in cas, as I know that home doesn't handle long integers correctly, I then tried home to find out.

One of the sources of confusion is that CAS silently resorts to home functions when there is no CAS function especially if spelled "the wrong way", i. e. upper case instead of lower case. This mode switch converts the arguments to home reals as necessary.
It is very confusing to know what is the "good way" to spell the appropriate function. As you can see on the screenshot below, there is no way to see what is the good answer :

[Image: modpb.png]

the wrong answer is obtained by typing "mod(7^23,143)" in lower case in CAS mode
the good answer is obtained by typing "(7^23) MOD 143" in upper case in CAS mode too

All this is very confusing.
MOD was implemented inside the CAS for the first version of the 39GII, it was not meant to be used with exact data, the CAS function for integer remainder is irem, and here the right function is powmod(7,23,143) because computing 7^23 before doing remaindering is much much less efficient.
I will add integer checking for arguments inside MOD.
Thanks Parisse, and so we learn.

To be fair, Excel and Scilab didn't return the correct result for this example, either. Maxima, and Wolfram Alpha did return the result as expected. There is enough confusion to go around.

There still remains a problem with CAS and the Prime [Toolbox] which returns a lowercase mod, and always results in an error. The help documentation for MOD could be improved, as well.

-Dale-
(08-21-2015 11:09 AM)parisse Wrote: [ -> ]MOD was implemented inside the CAS for the first version of the 39GII, it was not meant to be used with exact data, the CAS function for integer remainder is irem, and here the right function is powmod(7,23,143) because computing 7^23 before doing remaindering is much much less efficient.
I will add integer checking for arguments inside MOD.

Ups, I totally forgot to check available commands for integers before I started, but if I had, this bug would not have been found. Your remark concerning implementation for the HP39 clarifies matters.
I don't know if the manual provides information about ranges, but perhaps you can give them here.
Thanks
Arno
(08-20-2015 10:31 PM)Didier Lachieze Wrote: [ -> ][ 7^23 mod 143 ]
On my 41CX I get 66 and on the WP 34S I get 84.

For the record: the 34s correctly returns 2.

Since 7^23 has more than 16 significant digits (which is obvious as it appears as an intermediate result of ~10^19) of course you have to switch to double precision.

Dieter
Pages: 1 2
Reference URL's