Exact internal form of reals in CAS
02-20-2014, 09:50 AM (This post was last modified: 03-23-2018 05:51 AM by Joe Horn.)
Post: #1
 Joe Horn Senior Member Posts: 1,502 Joined: Dec 2013
Exact internal form of reals in CAS
EDIT: Recent firmware versions have obsoleted the following program by including its functionality in the format() function. To see if your firmware is recent enough, try this in CAS:

format(pi,"a12")

If an empty string is returned, then you have an ancient version of the firmware; run the Connectivity Kit and upgrade today! Then you'll get this output instead:

"0x1.921fb54442d0p+1"

which is the exact internal form of the floating-point real value of pi in CAS, which is what the program below was written to provide. So you don't need this program any more; just use format(number, "a12").

------ Original posting (now obsolete) -----

Since Prime's CAS only displays reals in rounded decimal form, it is often impossible to see the exact value of a real, even if the format() function is used.

Hence this program, called "hex", which outputs the internal representation of any real number in CAS.

Syntax: hex(real)
Output: exact hex representation of the real in "binary scientific notation".

Example: hex(pi) --> "1.921FB54442Dp+1"
This means that pi in CAS is internally stored as the exact hex number 1.921FB54442D times 2^1.

Notes about the output:
Leading sign follows the usual convention: "-" for negative, none for positive.
The leading digit is always 1.
The next 11 digits are hex nibbles.
The 12th digit (if any) is always an even nibble.
The exponent (if any) is of the form "p" followed by +n or -n, where n is the power of 2, as a base-ten integer.

Trivial bonus feature: If the input is a ratio of exact integers, "hex" attempts to return all the hex digits of the ratio, up to 1000 digits. I cannot imagine any possible use for this feature.
Example:
hex(1./1537) --> "1.551C7B40CA88p-11" (notice the "." in the input)
hex(1 /1537) --> "1.551C7B40CA88E92E78414A739766C ... p-11" (1000 digits)

Programming note: CAS functions often act differently depending on how they are spelled (UPPERCASE, lowercase, or MixedCase). That's why I used Sign(x) below instead of SIGN(x) or sign(x). Only Sign(x) has full 48-bit CAS precision; SIGN and sign only have Home-level 12-digit precision, and they therefore get borderline cases wrong. Example: Try Sign(.5-.4-.1) in CAS. It correctly gets 1, because .5-.4-.1 returns 2^-49 in CAS, not zero. SIGN and sign get 0, which is wrong.

"hex", a CAS program:
Code:
#cas hex(x):=BEGIN    LOCAL p,s,f;    IF x==0 THEN RETURN("0");  END ;     f:=Sign(x);     x:=abs(x);     p:=floor(logb(x,2));     s:="1.";     x:=FP(x/2^p);     WHILE (x>0) AND ((SIZE(s))<1000) DO      x*=16;      s+=MID("0123456789ABCDEF",IP(x)+1,1);      x:=FP(x);     END;;     IF f==-1 THEN s:="-"+s;  END ;     IF p>0 THEN s:=s+"p+"+STRING(p);  END ;     IF p<0 THEN s:=s+"p-"+STRING(abs(p));  END ;     RETURN(s);   END; #end

Edit: Updated for the improved functions in Prime rev 6030. The changes made are:

was: x:=frac(x/2^p);
now: x:=FP(x/2^p);

was: s+=MID("0123456789ABCDEF",exact(iPart(x))+1,1);
now: s+=MID("0123456789ABCDEF",IP(x)+1,1);

was: x:=frac(x);
now: x:=FP(x);

was: IF f<0 THEN s:="-"+s; END ;
now: IF f = -1 THEN s:="-"+s; END ;

Edit #2: Updated for compatibility with Prime rev 6940, by wrapping it in #cas and #end, and modifying the second line.

<0|ɸ|0>
-Joe-
02-21-2014, 01:02 AM
Post: #2
 rprosperi Senior Member Posts: 3,618 Joined: Dec 2013
RE: "hex": exact internal form of reals in CAS
...and thus Prime Hacking became an official sport.

We were all wondering how long it would take to begin...

And your comments about the UPPER/lower/Mixed case spellings of CAS (and HOME!) function names are critically important! This is easily the most frustrating aspect (among several...) of learning to write PPL programs, and especially so when using CAS programs.

I, and I believe many other folks, hope that the mixed case names (and the closely related issue of Functionxyz() doing this and FUNCTIONXYZ() doing that) is addressed in an early ROM update. The confusion and frustration these issues is causing (exacerbated by the lack of documentation on many of these spell-alike functions) is at best wasting time of programmers trying to learn Prime, and at worst permanently losing Prime developers and users.

Is it just me? Do others agree these are priority issues that need to be corrected ASAP?

--Bob Prosperi
02-21-2014, 01:40 AM
Post: #3
 patrice Member Posts: 180 Joined: Dec 2013
RE: "hex": exact internal form of reals in CAS
(02-21-2014 01:02 AM)rprosperi Wrote:  ...and thus Prime Hacking became an official sport.

(02-21-2014 01:02 AM)rprosperi Wrote:  And your comments about the UPPER/lower/Mixed case spellings of CAS (and HOME!) function names are critically important! This is easily the most frustrating aspect (among several...) of learning to write PPL programs, and especially so when using CAS programs.

I, and I believe many other folks, hope that the mixed case names (and the closely related issue of Functionxyz() doing this and FUNCTIONXYZ() doing that) is addressed in an early ROM update. The confusion and frustration these issues is causing (exacerbated by the lack of documentation on many of these spell-alike functions) is at best wasting time of programmers trying to learn Prime, and at worst permanently losing Prime developers and users.

Is it just me? Do others agree these are priority issues that need to be corrected ASAP?
Don't hope too much, PPL and CAS are really 2 beasts put together and there is really a huge number of functions with only case differences in the name.
If you want an idea of the undocumented functions, looks for GIAC and XCAS on Internet.

Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
02-24-2014, 03:55 PM
Post: #4
 patrice Member Posts: 180 Joined: Dec 2013
RE: "hex": exact internal form of reals in CAS
Hi Joe,
When I read your programs, I learn things too.
Different domain but always interesting.

Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
02-24-2014, 07:32 PM
Post: #5
 Joe Horn Senior Member Posts: 1,502 Joined: Dec 2013
RE: "hex": exact internal form of reals in CAS
(02-24-2014 03:55 PM)patrice Wrote:  Hi Joe,
When I read your programs, I learn things too.
Different domain but always interesting.

Thanks, Patrice!

<0|ɸ|0>
-Joe-
04-15-2014, 10:53 PM (This post was last modified: 04-15-2014 10:54 PM by Eddie W. Shore.)
Post: #6
 Eddie W. Shore Senior Member Posts: 999 Joined: Dec 2013
RE: "hex": exact internal form of reals in CAS
Awesome program Joe!

OT: I was curious to whether the += and *= works in non-CAS modes/programs. Sadly they don't, these are neat functions. The CAS also has -= and /= (can't be textbook mode, maybe in the program editor?).
05-28-2014, 05:28 AM (This post was last modified: 12-06-2014 05:10 AM by Joe Horn.)
Post: #7
 Joe Horn Senior Member Posts: 1,502 Joined: Dec 2013
RE: "hex": exact internal form of reals in CAS
Update: The program listing in the original posting has been edited to reflect some improvements in the Prime update released today, rev 6030. Some CAS functions are better behaved now. For example, exact(iPart(x)) can be replaced by just IP(x) without any loss of accuracy when x is a real that's very close to, but not equal to, a whole number.

Edit: Update on 5 December 2014: The original posting has been edited to reflect the hugely improved handling of CAS programs in Prime rev 6940.

<0|ɸ|0>
-Joe-
 « Next Oldest | Next Newest »

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