Post Reply 
HP Prime CAS & Large numbers
08-10-2017, 08:27 AM
Post: #1
HP Prime CAS & Large numbers
Hi

I've been looking on the internet for answers to this question but can't find anything. When using the CAS for very large numbers for example (256^256)*2^40, how do I convert this to scientific format (exact or otherwise)? I can do it online of course but wondered if I can do it on the calculator. I've tried setting scientific format with say 8 digits but it doesn't do anything and of course if I copy the number to the home screen I just get infinity.

Bipman
Find all posts by this user
Quote this message in a reply
08-10-2017, 11:35 AM
Post: #2
RE: HP Prime CAS & Large numbers
Simply type Shift Enter instead of Enter, this provides the result in scientific format.
Hth Arno
Find all posts by this user
Quote this message in a reply
08-10-2017, 11:39 AM
Post: #3
RE: HP Prime CAS & Large numbers
If I try that I get +Inf.

Bipman
Find all posts by this user
Quote this message in a reply
08-10-2017, 12:31 PM
Post: #4
RE: HP Prime CAS & Large numbers
That's because floating-point numbers in Prime's CAS are limited to a maximum of roughly 10^308. Anything greater than that will be returned as infinity.

Examples:
2^1023.9999 --> 1.79756853259e308
2^1024. --> +Inf (notice the decimal point in the input)
2^1024 --> exact 309-digit answer

Here's a trick: If your CAS result is too big for CAS to convert to a real, but smaller than 500 digits, you can easily convert it to a real by importing it into Home. Just press Home, Menu (the physical Menu key), Get from CAS, then select the number. Unfortunately, your example, (256^256)*2^40, is too big even for Home to handle (it's 629 digits long). If you want to see how long it is, just use size(string(Ans)).

You can also use the following "biglog" program to return the common log of large CAS integers. It works with CAS integers of any size.

#cas
biglog(d):=
BEGIN
LOCAL j,k;
IF type(d)<>DOM_INT THEN RETURN "Argument not integer" END;
d:=abs(d);
k:=string(d);
j:=expr(head(k)+"."+tail(k));
return size(k)-1+evalf(log10(j),13);
END;
#end

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
08-10-2017, 02:03 PM
Post: #5
RE: HP Prime CAS & Large numbers
Thanks, that's great!

Bipman
Find all posts by this user
Quote this message in a reply
08-12-2017, 06:12 PM
Post: #6
RE: HP Prime CAS & Large numbers
Ooh ooh ooh, here's a nifty way to convert any large CAS integer into floating-point notation:

format(2^7777,"d20") --> "1.2890693393361565650e+2341"

Change the "20" in the input to however many significant digits you want. Cool, huh?

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
08-13-2017, 09:11 AM
Post: #7
RE: HP Prime CAS & Large numbers
That's cool!

Bipman
Find all posts by this user
Quote this message in a reply
09-08-2022, 09:14 AM
Post: #8
RE: HP Prime CAS & Large numbers
I tried to find the largest integer number that the Prime can manage in CAS.
I guess CAS integer are internally formatted in binary, so I empirically searched the largest power of 2 before the Prime returns "undefined": it is 2^8598.
I guess that the largest integer is (mathematically) 2^8599-1, but you can't write this expression on Prima without getting "undefined".
I tried this: Σ(2^n,n,0,8598).
It returns an integer with 2588 digits that (I think) is the largest integer before getting "undefined"; you can verify this adding just 1 to it and the result is "undefined".
format(Σ(2^n,n,0,8598),"d10") -> "3.605227827e+2588"
Similarly I searched the smallest (most negative) integer, finding it to be Σ(-2^n,n,0,8597). if you try to subtract just 1 to it, you get "undefined".
The question now is: why such particular number of bits (8599) has been chosen for the binary format of integer in CAS?
Other "odds":
- format(Σ(-2^n,n,0,8597),"d10") -> ""; why?
- format(Σ(2^n,n,0,8598),"a12") -> "3.605227827e+2588"; not in hexadecimal form; why?
- format(Σ(2^n,n,0,29),"a12") -> "0x1.fffffff80000p+29"; OK, but...
- format(Σ(2^n,n,0,29),"h12") -> "1073741823"
Note that "a12 (or "h12") shows the max number of fractional digit in the hexadecimal format; such that from "a13" to "a99" you get the same result.
- format(Σ(2^n,n,0,30),"a12") -> "2147483647"; not formatted; why?
- format(999999999999,"a12") -> "999999999999"
- format(999999999999+1,"h12") -> "1.00000000000e+12"
- format(Σ(2^n,n,0,29)*2+1,"a12") -> "0x1.fffffffc0000p+30"
- format(Σ(2^n,n,0,29)*2+2,"a12") -> "2147483648"
Find all posts by this user
Quote this message in a reply
09-27-2023, 10:37 AM
Post: #9
RE: HP Prime CAS & Large numbers
(09-08-2022 09:14 AM)Mario Blasone Wrote:  I tried to find the largest integer number that the Prime can manage in CAS.
I guess CAS integer are internally formatted in binary, so I empirically searched the largest power of 2 before the Prime returns "undefined": it is 2^8598.
I guess that the largest integer is (mathematically) 2^8599-1, but you can't write this expression on Prima without getting "undefined".

Your conclusions seems substantially correct.
Just to be more precise, the max_integer obtained with Sum(2^n,0,2598) is 8599 bits long (not 8598), because each addend is a binary 1 at position n followed by all binary 0, but n ranger from 0 to 8598; i.e 8599 positions (bits). It is more evident if the expression to get the max_integer on HP Prime is written in this equivalent mode: Sum(2^(n-1),1,8599) i.e. summation of 8599 terms from 2^0 to 2^8598.
Min negative integer (max negative) is Sum(-2^n,1,8598).
Globally speaking, on HP CAS, integers seems to be stored in binary form with 8600 digits (including sign), ranging from -2^8598 (through 0) up to 2^8599.
Find all posts by this user
Quote this message in a reply
09-27-2023, 01:22 PM
Post: #10
RE: HP Prime CAS & Large numbers
(09-27-2023 10:37 AM)Mario Blasone Wrote:  Globally speaking, on HP CAS, integers seems to be stored in binary form with 8600 digits (including sign), ranging from -2^8598 (through 0) up to 2^8599.

This seems sad, considering the comparatively vast amount of memory available in the Prime, especially in the G2. Exact integers on the HP 49 and 50 are limited only by memory, and I have done calculations (albeit slowly) involving numbers with over 50000 digits.

I would really like to see the end of artificial limitations on number of digits, as well as the sizes of lists and arrays, or at least to know the trade-offs involved.
Find all posts by this user
Quote this message in a reply
09-27-2023, 05:18 PM
Post: #11
RE: HP Prime CAS & Large numbers
(09-08-2022 09:14 AM)Mario Blasone Wrote:  I tried to find the largest integer number that the Prime can manage in CAS.
I guess CAS integer are internally formatted in binary, so I empirically searched the largest power of 2 before the Prime returns "undefined": it is 2^8598.
I guess that the largest integer is (mathematically) 2^8599-1, but you can't write this expression on Prima without getting "undefined".
I tried this: Σ(2^n,n,0,8598).
It returns an integer with 2588 digits that (I think) is the largest integer before getting "undefined"; you can verify this adding just 1 to it and the result is "undefined".
format(Σ(2^n,n,0,8598),"d10") -> "3.605227827e+2588"
Similarly I searched the smallest (most negative) integer, finding it to be Σ(-2^n,n,0,8597). if you try to subtract just 1 to it, you get "undefined".
The question now is: why such particular number of bits (8599) has been chosen for the binary format of integer in CAS?
Other "odds":
- format(Σ(-2^n,n,0,8597),"d10") -> ""; why?
- format(Σ(2^n,n,0,8598),"a12") -> "3.605227827e+2588"; not in hexadecimal form; why?
- format(Σ(2^n,n,0,29),"a12") -> "0x1.fffffff80000p+29"; OK, but...
- format(Σ(2^n,n,0,29),"h12") -> "1073741823"
Note that "a12 (or "h12") shows the max number of fractional digit in the hexadecimal format; such that from "a13" to "a99" you get the same result.
- format(Σ(2^n,n,0,30),"a12") -> "2147483647"; not formatted; why?
- format(999999999999,"a12") -> "999999999999"
- format(999999999999+1,"h12") -> "1.00000000000e+12"
- format(Σ(2^n,n,0,29)*2+1,"a12") -> "0x1.fffffffc0000p+30"
- format(Σ(2^n,n,0,29)*2+2,"a12") -> "2147483648"

Concerning "odds" with 'format' command:
I guess that format(obj,"a12") initially converts obj to float and then put the result in the hex format.
The maximum integer n that may be used in approx(n), before getting +inf, is largely smaller than max_integer.
After several trials I obtained that such max n is Sum(2^(n-1),1,1023); i.e. binary integers with 1024 bits (including sign).
approx(Σ(2^(n-1),n,1,1023))=8.98846567431e307
approx(Σ(2^(n-1),n,1,1023)+1)=+inf
approx(Σ(-2^(n-1),n,1,1023))=−8.98846567431e307
approx(Σ(-2^(n-1),n,1,1023)-1)=+inf (Not -inf; this is really odd!!)

format(n,"a8") shows the value of approx(n) in hex format for n≤(2*Σ(2^(n-1),n,1,30)+1), that is mathematically equivalent (2^31-1), without triggering the internal limit on"a8" format
format(2*Σ(2^(n-1),n,1,30)+1,"a8")="0x1.fffffffcp+30".
But format(2^31-1,"a8")="2.1474836e+9"
Note that the above fractional part (ff ff ff fc)_base16 corresponds to 30 consecutive binary ones (7 Bytes and half, or 15 nibles, or 7*4+2=30 bits, all at binary 1).
format(2*Σ(2^(n-1),n,1,30)+2"a8")="2.1474836e+9" (it reverts to "s8" scientific notation).
Hexadecimal formats longer than "a8" ("a9" and onward) add only non significant zeros after 8-th hex fractional digit.
format(π,"d16")="3.141592653589782" saturates the number of fractional digits (from "a17" onward produces the same result).
format(n/m,"a8") for small rationals a/b (as 1/(2^n) ) seems not having the same limitation.
Find all posts by this user
Quote this message in a reply
09-27-2023, 06:37 PM
Post: #12
RE: HP Prime CAS & Large numbers
There is indeed a max for integers, in global.cc
Code:

  double powlog2float=1e4;
  int MPZ_MAXLOG2=8600; // max 2^8600 about 1K
As far as I remember, the long integer library is not optimal for integer operations (it uses Karatsuba multiplication, not FFT), if you multiply/divide really large integers it will be slow. There is always a trade-off between speed and size limits, and I tend to be conservative. I don't remember when the limits were set, if it's an heritage of the hp39gii, then they should probably be raised, perhaps multiplied by 10.
Find all posts by this user
Quote this message in a reply
09-28-2023, 01:25 PM
Post: #13
RE: HP Prime CAS & Large numbers
(09-27-2023 06:37 PM)parisse Wrote:  There is indeed a max for integers, in global.cc
Code:

  double powlog2float=1e4;
  int MPZ_MAXLOG2=8600; // max 2^8600 about 1K
As far as I remember, the long integer library is not optimal for integer operations (it uses Karatsuba multiplication, not FFT), if you multiply/divide really large integers it will be slow. There is always a trade-off between speed and size limits, and I tend to be conservative. I don't remember when the limits were set, if it's an heritage of the hp39gii, then they should probably be raised, perhaps multiplied by 10.

It seems that you confirm that long integers are stored with a limit of 8600 bits (one of which dedicated to sign): ranging from -2^8598 to +2^8599 (0 to 2^8599 plus 1 bit for sign; the single value -2^8599 excluded). (==> MPZ_MAXLOG2=8600)
Anyway it's not evident what the cited piece of code means and what global.cc is, including the comment "// max 2^8600 about 1K" (?).

Now let me start a completely new subject.
I find very useful that the names of user defined functions appear in the ToolBox Catlg list (in italic).
I would appreciate the possibility to add some (used written) help text for them to recall inside the Catlg with the Help key (as for built-in functions). These would provide a mean to remind yourself the scope and syntax of your user defined functions.
Is there any way to get this? For example creating a "Note" to associate with the user defined programs/functions...
Find all posts by this user
Quote this message in a reply
09-29-2023, 10:46 AM
Post: #14
RE: HP Prime CAS & Large numbers
About 1K means that a multiprecision integer can currently not exceed 1K in storage. That's not much on the Prime, but you should not forget that the Prime is a child of the hp39gii and on the 39gii, 1K is not negligible.
As for global.cc, it's from the source code of Giac/Xcas.
For your other question, it's not my domain, I have no idea, you should perhaps start a new thread...
Find all posts by this user
Quote this message in a reply
10-01-2023, 10:44 AM
Post: #15
RE: HP Prime CAS & Large numbers
Thank you Parisse for your clarifications.
(8600_bit=8600/(8*1024)_Byte=1075/1024_Byte, about 1.05_KByte)
I made some trials with my virtual HP Prime assigning growing values to an integer variable n and looking a its memory size (with [shift] Mem / CAS Variables / n ; values shown in KB and up to 2 decimals for small sizes).
The memory used to store n grows with the value up to 2KB for monster big integers.
The reported memory size is rounded and the threshold values of n to increase the size are not precisely found from experimentation (what precisely means 0.01_KB ? round(1024/100) ?).
Anyway, for integer values up to (2^31)-1 the mem_size is 0.01_KB and building a table gives:
Int n values Mem_size
+---------------------+-----------
0 .. (2^31)-1 0.01_KB
2^31 .. (2^44)-1 0.02_KB
2^44 .. (2^84)-1 0.03_KB
2^84 .. (2^128)-1 0.04_KB
2^128 .. (2^168)-1 0.05_KB
and so on.
Very last threshold interval seems different:
2^3936 .. (2^3976)-1 0.98_KB (Ok)
2^3976 .. (2^4016)-1 0.99_KB (Ok)
2^4016 .. (2^6084)-1 1_KB (large interval, no further fractional values for mem_size)
2^6084 .. max_int 2_KB (without intermediate steps!).
Probably this is due to round-off of reported mem_size. For the same reason the 2_exp intervals of successive threshold values are about 40, but not always constant ( 44-31=11 (exception); 84-44=40; 40; 44; ...; 40; 40; 2068 (last jump) ).

I will open a new thread about adding "Help" instructions to user defined functions / programs.
Find all posts by this user
Quote this message in a reply
08-12-2024, 12:36 AM
Post: #16
RE: HP Prime CAS & Large numbers
Max number is;

(1+(2^8598-1)/(2^8598)) * 2^8598
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)