Re: Number of digits in a number [82]


[Prev][Next][Index][Thread]

Re: Number of digits in a number [82]



>   mtm@maurano.com (Matt Maurano) writes:
>  I need to know how to find the number of digits in a number. For
>  example:
>
>  Number:      Digits:
>  .1234   4
>  1.23     3
>  9          1
>
>  For those interested, this is for the second flag passed to the round(
>  function. Its for a convert program I've been working on, and it would
>  help if I could round the answer to the number of significant digits.
>
>  Please E-Mail responses. Thanks in advance.
>
>  P.S.: Right now, the program is ~<1k and does standard to and from
>  metric, as well as time conversions. If anyone wants me to post it,
>  just E-Mail me.
>  ---
>  Matt Maurano
>  mtm@maurano.com
>
>>>>
Here's a simple program to count the significant digits in
variable X. I've included both TI-82 and TI-83 versions:


\START82\
\NAME=SIG82
:0\->\N
:If X\<>\0
:Then
:X*\10^\\(-)\int log abs X\->\Z
:While Z-round(Z,N) and N<9
:N+1\->\N
:End
:N+1\->\N
:End
:Disp X,N
\STOP82\


\start83\
\name=SIG83
0\->\N
If X\!=\0
Then
X*\10\^(\(-)\int(log(abs(X))))\->\Z
While Z-round(Z,N) and N<9
N+1\->\N
End
N+1\->\N
End
Disp X,N
\stop83\


It uses variable Z to store a copy of X shifted so that there
is exactly one digit to the left of the decimal point. Then it
just tries rounding to 0, 1, 2,... up to 9 decimal places until
the result is equal to Z. It adds 1 for the digit to the left of
the decimal point, giving the total significant digits, up to 10.


The effect of the function round(Z,N) is to eliminate all
significant digits beyond the Nth digit to the right of the
decimal point, rounding the Nth digit up if the (N+1)th digit was
5 or more. If N is 0, the value is rounded to an integer. Any
trailing zeros resulting from Fix mode are still displayed.


Float mode displays *all* significant digits, up to a total of
10, with *no* trailing zeros. Using Float mode is the only simple
way of showing all the visible significant digits in a number
without any extra zeros on the right.


The TI-82 and TI-83 actually use up to 13 or 14 significant
digits internally. For example, pi is 3.1415926535898, and e is
2.7182818284590. The regression equation variable, RegEQ, when
calculated in Float mode, will display up to 14 significant
digits, and selected values in tables will display up to 12.
The extra digits of precision help to ensure that displayed
values will usually be quite accurate.


The function round(X), without a second argument, returns a
value equal to what is actually visible in Float mode. It does
this by eliminating any significant digits after the tenth.


It might be nice if a function were provided to convert numbers
into strings, but since a number with a sign, a decimal point,
and an exponent can take up to 14 character positions on a
16-character line, it wouldn't really be very useful. In general,
it's probably best to display numbers on a line by themselves
except in very special cases.


                                Harry


References: