Post Reply 
How do I use CAS variables in a Program?
05-30-2017, 09:08 AM
Post: #1
How do I use CAS variables in a Program?
I want to use long integer values in a program. On the CAS home screen, I can define:

* A:=1000!;

and it stores the exact value into that variable.

I've tried methods I saw on various web pages, like

* calling the variables CAS.B and CAS.D
* CAS.B:=19^C-1;

**** (I still got scientific notation when the numbers got too large)

when that didn't work, I tried

* CAS("B:=19^C-1");

**** but this still gave me scientific notation after a certain point.

I'd love to know how to use CAS precision variables in a program if anyone can point me to the information.

Here's the latest version of the program. It is meant to simply give the 2^E and 3^F prime factors of 19^C-1.

Code:

#cas
P18NSUB1():=
BEGIN  
  FOR C FROM 1 TO 15 DO  
  CAS("B:=19^C-1"); 
  CAS("D:=B"); 
  E:=0; 
  F:=0; 
  G:=0; 
  WHILE G = 0 DO 
    CAS("G:=D MOD 2"); 
    IF G = 0 THEN  
      E:=E+1; 
      D:=D/2  END ; 
   END;; 
  G:=0; 
  WHILE G = 0 DO 
    CAS("G:=D MOD 3"); 
    IF G = 0 THEN  
      F:=F+1; 
      D:=D/3  END ; 
   END;; 
  PRINT(1000*E+F+C/1000); 
  PRINT(B); 
  PRINT(-D); 
  END;;  
  RETURN(B);  
END;
#end

(I don't know why this COPY of the program gets double ;; when I only used single ;. I assume there are some places where the ; is optional that I used it, so it was doubled when transferred to the computer. There are also some cases where I have ; in the calcuator and it was deleted when copied to the computer, like in both IF ... END blocks.)
Find all posts by this user
Quote this message in a reply
05-30-2017, 04:54 PM
Post: #2
RE: How do I use CAS variables in a Program?
Certain system variables are always "Home" (i.e. non-CAS) variables, including A-Z (uppercase). The following notes might help:

http://www.hpmuseum.org/forum/thread-215.html

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
05-30-2017, 07:39 PM
Post: #3
RE: How do I use CAS variables in a Program?
Unfortunately, Han, I still cannot make this work for me.

Here is a simple program (double the value and add one) I tried several variations of, and I predefined the CAS variable aa in the CAS screen.

Code:

#cas
TwoUadd1():=
BEGIN
  CAS.aa:=CAS.aa*2+1;
  PRINT(CAS.aa)
  return CAS.aa;
END;
#end

When I run the program, it does not use the long precision, but instead drops it into scientific notation, and as you can see in the attached image, the new value of aa in the CAS window is also now in that notation.

Perhaps it can't be done????

I look forward to any hints you can provide.


Attached File(s) Thumbnail(s)
   
Find all posts by this user
Quote this message in a reply
05-30-2017, 08:52 PM (This post was last modified: 05-30-2017 08:52 PM by Tim Wessman.)
Post: #4
RE: How do I use CAS variables in a Program?
My best guess - "CAS." will be sending everything through the limited precision "home screen". Just remove all the CAS. and use aa directly since you are in a cas program.

In all honesty, CAS. does not really do anything and was purely to help users understand where an item was coming from.

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
05-31-2017, 04:49 AM
Post: #5
RE: How do I use CAS variables in a Program?
Hello,

If your aim is to get long integers in home, then you can not do it as Home does not support long integers...

The best that you CAN do is to use strings that will hold the representation of your long integer.
in Home CAS(string(aa)) for example should return the string representation of the aa variable (which can contain a long integer).

In this case, you need to make sure that you do all your calculations in CAS to ensure that there is not cas to home transform and loss of precision.

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
05-31-2017, 04:22 PM
Post: #6
RE: How do I use CAS variables in a Program?
(05-30-2017 08:52 PM)Tim Wessman Wrote:  My best guess - "CAS." will be sending everything through the limited precision "home screen". Just remove all the CAS. and use aa directly since you are in a cas program.

Thanks, I don't know why it didn't work the first time, but it worked this time. Perhaps it was because the first time, I didn't predefine the value in CAS mode.
Find all posts by this user
Quote this message in a reply
05-31-2017, 04:23 PM
Post: #7
RE: How do I use CAS variables in a Program?
(05-31-2017 04:49 AM)cyrille de brĂ©bisson Wrote:  If your aim is to get long integers in home, then you can not do it as Home does not support long integers...

The best that you CAN do is to use strings that will hold the representation of your long integer.
in Home CAS(string(aa)) for example should return the string representation of the aa variable (which can contain a long integer).

In this case, you need to make sure that you do all your calculations in CAS to ensure that there is not cas to home transform and loss of precision.

Cyrille

Thanks for your idea. I was able to do it without using strings. Perhaps you thought I wanted to use CAS variables in the non-cas HOME mode, but I was doing all my stuff in CAS mode and in a CAS program.

It did ultimately work.
Find all posts by this user
Quote this message in a reply
05-31-2017, 05:11 PM
Post: #8
RE: How do I use CAS variables in a Program?
(05-31-2017 04:22 PM)John@Mazes.com Wrote:  Thanks, I don't know why it didn't work the first time, but it worked this time. Perhaps it was because the first time, I didn't predefine the value in CAS mode.


It probably didn't work the first time because it wasn't within the #cas #end brackets. That throws the parser/compiler into/out of the "CAS" one which does runtime creation of stuff instead of compile time.

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
06-01-2017, 03:50 AM
Post: #9
RE: How do I use CAS variables in a Program?
The CAS() command is a merely a "wrapper" command that enables non-CAS programs (i.e. "Home" programs) to execute CAS commands as if one were in the CAS view. The same goes for CAS.command() or CAS.object.

Since you used the #cas ... #end then you have already declared that everything enclosed within is to be treated as if run from the CAS view. So CAS.command() or CAS.object or even CAS("command or expression") is not only redundant but introduces possible conflicts in environments (CAS vs non-CAS).

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 




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