Post Reply 
All decimal digits of reals in CAS
04-07-2015, 03:17 AM (This post was last modified: 05-29-2017 01:41 PM by Joe Horn.)
Post: #1
All decimal digits of reals in CAS
This program, called "dec", outputs the exact internal decimal value of a floating point number in CAS. It is similar to (and is based on) my "hex" program with the only difference being that the output of "dec" is in standard decimal format, instead of hexadecimal.

Syntax in CAS: dec(real)
Output: exact decimal representation of the real in scientific notation.

Example in CAS: dec(pi) --> "3.1415926535897824578569270670413970947265625"
This means that pi in CAS is internally stored as exactly the same value as the displayed decimal number. The value is actually stored in binary, of course, but sometimes seeing its decimal equivalent is helpful, so that's what "dec" was created to do. If the display modes allowed settings up to 50 digits, there would be no need for this program.

Trivial bonus feature: If the input is a ratio of exact integers, "dec" attempts to return all the digits of the ratio, up to 1000 digits. I cannot imagine any possible use for this feature.
Example:
dec(1./1537) --> "6.506180871828206591089838184416294097900390625E-4" (notice the "." in the input)
dec(1 /1537) --> "6.50618087182823682498373454782042940793754066363044892648015614834...E-4" (1000 digits)

"dec", a CAS program:
Code:
#cas
dec(x):=BEGIN
 LOCAL p,s,f;
  IF x==0 THEN RETURN("0");  END ;
  f:=Sign(x);
  x:=abs(x);
  p:=XPON(x);
  x/=ALOG(p);
  IF IP(x)==0 THEN p--; x*=10; END;
  s:=STRING(IP(x))+".";
  x:=FP(x);
  WHILE (x>0) AND ((SIZE(s))<1000) DO
    x*=10;
    s+=STRING(IP(x));
    x:=FP(x);
   END;;
  IF f<0 THEN s:="-"+s;  END ;
  IF p>0 THEN s:=s+"E+"+STRING(p);  END ;
  IF p<0 THEN s:=s+"E-"+STRING(abs(p));  END ;
  RETURN(s);
END;
#end

Interesting exploration:
Compare dec(pi) with dec(pI) with "pi" and "pI" spelled exactly as shown.
See the difference in the results? pI is CAS's name for Home's pi.

Disclaimer: Extensive testing on extreme inputs and goofy inputs and illegal inputs has not been done. It'll probably choke on stupid inputs. Weeding them out is left as an exercise for the student.

<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
All decimal digits of reals in CAS - Joe Horn - 04-07-2015 03:17 AM



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