Post Reply 
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
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-
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Exact internal form of reals in CAS - Joe Horn - 02-20-2014 09:50 AM



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