"hex": exact internal form of reals in CAS +- HP Forums (http://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP Prime Software Library (/forum-15.html) +--- Thread: "hex": exact internal form of reals in CAS (/thread-720.html) |
"hex": exact internal form of reals in CAS - Joe Horn - 02-20-2014 04:50 AM 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 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. |