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
04-10-2015, 02:55 PM (This post was last modified: 04-11-2015 03:10 AM by compsystems.)
Post: #2
RE: "dec": All decimal digits of reals in CAS
Thank You
I'm porting the code to other models of calculators, ti68k not have the XPON() command that I know, What does this function? mathematically (algorithm)

ti68k & tinspireCAS code

Code:

fileName: main.dec.89f: unlock(dec): unarchive(dec):

dec(x)
Func
  ©// By Joe Horn 2015, ti68k port by jaimeza AKA compSystems
  Local p,s,f

  If x=0
    Return "0"

  sign(x)→f
  abs(x)→x
  xpon(x)→p
  x/10^p→x

  If int(x)=0 Then
    p-1→p
    x*10→x
  EndIf

  string(int(x))&"."→s
  fpart(approx(x))→x
  While x>0 and dim(s)<1000
    x*10→x
    s&string(int(x))→s
    fpart(approx(x))→x
  EndWhile

  If f<0
    "-"&s→s
  If p>0
    s&"E+"&string(p)→s
  If p<0
    s&"E-"&string(abs(p))→s
  Return s

EndFunc
Find all posts by this user
Quote this message in a reply
04-10-2015, 08:14 PM
Post: #3
RE: "dec": All decimal digits of reals in CAS
Quote:ti68k not have the XPON() command that I know, What does this function?

If you type the command in on the calculator and press help, you will see

Code:

XPON()
Syntax: XPON(value) Exponent. 
Returns the exponent of value. 
Example: XPON(123.4) ➔ 2

Hope it helps

Cheers, Terje
Find all posts by this user
Quote this message in a reply
04-10-2015, 09:02 PM
Post: #4
RE: "dec": All decimal digits of reals in CAS
(04-10-2015 08:14 PM)Terje Vallestad Wrote:  
Quote:ti68k not have the XPON() command that I know, What does this function?

If you type the command in on the calculator and press help, you will see

Code:

XPON()
Syntax: XPON(value) Exponent. 
Returns the exponent of value. 
Example: XPON(123.4) ➔ 2

Hope it helps

Cheers, Terje

XPON(X) is equivalent to \( \lfloor \log_{10}(X) \rfloor \) -- the floor of the base-10 logarithm of X.

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
04-11-2015, 02:43 AM (This post was last modified: 04-11-2015 03:42 AM by compsystems.)
Post: #5
RE: "dec": All decimal digits of reals in CAS
thanks Han Wink

versión 1 in tibasic ti68k
Code:
 
define dec(x)=floor(log(abs(x)))

version 2 in tibasic ti68k
Code:
 
define dec(x)=int(log(abs(x)))

version3 in tibasic ti68k with solve cmd
Code:
 
define dec(x)=floor(right(solve(10^expo=abs(x),expo)))

another samples
dec(e) => "2.7182818284590410939927096478641033172607421875"
dec(√(2)) "1.4142135623730922588947578333318233489990234375"
always returns ~50 digits

Joe Horn, please add at the beginning of your code, check flag should be in exact mode, otherwise always returns until 50 digits

in tibasic is
Code:
setMode("exact/approx","exact")

Another possible improvement is to support complex numbers, operating in the imaginary part and real part separately

in tibasic some commands are extended to the field of complex

sample

fpart(1.234+5.6789*i) => 0.234+0.6789*i
Find all posts by this user
Quote this message in a reply
04-12-2015, 03:15 AM (This post was last modified: 04-12-2015 03:21 AM by Joe Horn.)
Post: #6
RE: "dec": All decimal digits of reals in CAS
(04-11-2015 02:43 AM)compsystems Wrote:  dec(e) => "2.7182818284590410939927096478641033172607421875"
dec(√(2)) "1.4142135623730922588947578333318233489990234375"
always returns ~50 digits

Joe Horn, please add at the beginning of your code, check flag should be in exact mode, otherwise always returns until 50 digits

There seems to be a misunderstanding here. The "dec" program does not return "about 50 digits". It always returns ALL the digits, no matter how many that may be. The reason that dec(e) returns "2.7182818284590410939927096478641033172607421875" is because that decimal number is EXACTLY how Prime stores e internally in CAS. It's not an approximation.* "dec" returns the exact decimal of the real input, no matter how many decimal digits that may require. It doesn't arbitrarily stop after 50 digits, or any other number of digits. (Exception: if the input is a ratio of two integer-type objects, "dec" stops after outputting 1000 digits, as explained in the original posting).

Other ways of seeing exactly the same value using my other routines:

hex(e) --> "1.5BF0A8B14576p+1"
d2f(e) --> 191282078589627/2^46

* Ok, ok, to be perfectly clear: it IS an approximation of the math constant e... but it's not an approximation of CAS's real floating-point value of e. It's exactly equal to THAT value, which is itself a rational approximation of the irrational constant known as "e".

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
04-22-2015, 03:33 AM (This post was last modified: 04-22-2015 03:33 AM by Joe Horn.)
Post: #7
RE: "dec": All decimal digits of reals in CAS
(04-20-2015 02:54 PM)compsystems Wrote:  Please Joe Horn can comment on each line of code DEC() to understand the logic of the algorithm

thanks

See listing below. The explanations are below their respective lines of code. "Pathological input" means reals so close to integers that some CAS functions fail to distinguish between them and the neighboring integer.

Basic concept: Multiply by 10, chop off the integer part, and repeat, until input goes to zero.

Input --> Output string at each iteration
3.1416 --> "3."
1.416 --> "3.1"
4.16 --> "3.14"
1.6 --> "3.141"
6 --> "3.1416"
0 --> Exit

Code:
#cas
dec(x):=BEGIN
 LOCAL p,s,f;
// p = input's XPON (Power of 10)
// s = output String
// f = sign of input
  IF x = 0 THEN RETURN("0");  END ;
// bugfix: handle input of 0
  f:=Sign(x);
// Sign (not SIGN or sign) for pathological inputs (e.g. 0.5-0.4-0.1)
  x:=abs(x);
// force input to be positive to simplify code;
// restore sign before exiting if input was negative
  p:=XPON(x);
  x/=10^p;
// similar to x:=MANT(x) but more accurate for pathological inputs
  IF IP(x)=0 THEN p--; x*=10; END;
// bugfix: lets inputs like 0.1 work correctly
  s:=STRING(IP(x))+".";
// seed output with integer part and decimal point 
  x:=FP(x);
// remove integer part and begin main loop 
  WHILE (x>0) AND ((SIZE(s))<1000) DO
// stop if input is exhausted or Prime is bored
    x*=10;
// shift next digit to the left of the decimal point
    s+=STRING(IP(x));
// append next digit to the output string
    x:=FP(x);
// discard that digit and repeat loop
   END;;
  IF f<0 THEN s:="-"+s;  END ;
// retore negative sign if input was negative
  IF p>0 THEN s:=s+"E+"+STRING(p);  END ;
  IF p<0 THEN s:=s+"E-"+STRING(abs(p));  END ;
// if there was an exponent, append it
  RETURN(s);
END;
#end

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
11-02-2016, 10:05 PM
Post: #8
RE: "dec": All decimal digits of reals in CAS
I wonder what has changed since 10637, but running this as dec(pi) results on non-stop "Running non recursive evaluator"

Any suggestions? Thanks
Find all posts by this user
Quote this message in a reply
11-06-2016, 04:04 AM
Post: #9
RE: "dec": All decimal digits of reals in CAS
(11-02-2016 10:05 PM)Alberto Candel Wrote:  I wonder what has changed since 10637, but running this as dec(pi) results on non-stop "Running non recursive evaluator"

Any suggestions? Thanks

Strange; I get "3.1415926535897824578569270670413970947265625" on my rev 10637 Prime after about 0.885 seconds. Are you sure your listing is correct?

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
11-06-2016, 05:44 AM
Post: #10
RE: "dec": All decimal digits of reals in CAS
Thank you for looking into it. But yes, I have 10637.
dec(pi) (or anything) shows "Running no recursive evaluator" and breaking the program shows "Bad index error: Bad Argument value" It could be something else with my Prime.
Find all posts by this user
Quote this message in a reply
11-07-2016, 04:16 AM
Post: #11
RE: "dec": All decimal digits of reals in CAS
(11-06-2016 05:44 AM)Alberto Candel Wrote:  Thank you for looking into it. But yes, I have 10637.
dec(pi) (or anything) shows "Running no recursive evaluator" and breaking the program shows "Bad index error: Bad Argument value" It could be something else with my Prime.

Just a guess: try resetting the recursive CAS settings to their defaults (Shift CAS, Page down, Shift Esc).

If that doesn't help, then please post your program listing exactly as-is, since there might be a typo in it.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
11-07-2016, 03:05 PM (This post was last modified: 11-07-2016 06:16 PM by Alberto Candel.)
Post: #12
RE: "dec": All decimal digits of reals in CAS
Well, that was it! For some reason I do not know, in my 2 HP Primes the recursive setting was "1" After reseting it is "100"
Your "dec" program works now like a charm. Thank you!
Find all posts by this user
Quote this message in a reply
Post Reply 




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