All decimal digits of reals in CAS - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP Prime Software Library (/forum-15.html) +--- Thread: All decimal digits of reals in CAS (/thread-3571.html) |
All decimal digits of reals in CAS - Joe Horn - 04-07-2015 03:17 AM 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 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. RE: "dec": All decimal digits of reals in CAS - compsystems - 04-10-2015 02:55 PM 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:
RE: "dec": All decimal digits of reals in CAS - Terje Vallestad - 04-10-2015 08:14 PM 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:
Hope it helps Cheers, Terje RE: "dec": All decimal digits of reals in CAS - Han - 04-10-2015 09:02 PM (04-10-2015 08:14 PM)Terje Vallestad Wrote:Quote:ti68k not have the XPON() command that I know, What does this function? XPON(X) is equivalent to \( \lfloor \log_{10}(X) \rfloor \) -- the floor of the base-10 logarithm of X. RE: "dec": All decimal digits of reals in CAS - compsystems - 04-11-2015 02:43 AM thanks Han versión 1 in tibasic ti68k Code: version 2 in tibasic ti68k Code: version3 in tibasic ti68k with solve cmd Code: 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 RE: "dec": All decimal digits of reals in CAS - Joe Horn - 04-12-2015 03:15 AM (04-11-2015 02:43 AM)compsystems Wrote: dec(e) => "2.7182818284590410939927096478641033172607421875" 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". RE: "dec": All decimal digits of reals in CAS - Joe Horn - 04-22-2015 03:33 AM (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 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 RE: "dec": All decimal digits of reals in CAS - Alberto Candel - 11-02-2016 10:05 PM I wonder what has changed since 10637, but running this as dec(pi) results on non-stop "Running non recursive evaluator" Any suggestions? Thanks RE: "dec": All decimal digits of reals in CAS - Joe Horn - 11-06-2016 04:04 AM (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" Strange; I get "3.1415926535897824578569270670413970947265625" on my rev 10637 Prime after about 0.885 seconds. Are you sure your listing is correct? RE: "dec": All decimal digits of reals in CAS - Alberto Candel - 11-06-2016 05:44 AM 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. RE: "dec": All decimal digits of reals in CAS - Joe Horn - 11-07-2016 04:16 AM (11-06-2016 05:44 AM)Alberto Candel Wrote: Thank you for looking into it. But yes, I have 10637. 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. RE: "dec": All decimal digits of reals in CAS - Alberto Candel - 11-07-2016 03:05 PM 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! |