Re: Basic Proggraming Question


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

Re: Basic Proggraming Question



John <johnm@IPEG.COM> writes:

>  I am creating a program for my science class, but one important  part of
>  the proggram I can't seem to make. I need to know how to get the number
>  of decimal places, and the number of other places. For example...
>  if i put in 12.453 I could get like 3->D and 2->I.
>
>  Thanks!

It is easy enough to determine the _magnitude_ of a number (whether we are
talking about tens, hundreds, etc. -- or maybe tenths) using base-ten
logarithms. You don't say what calculator you are using, but on a TI-83, say,
use int(log(abs(N))) to get 0 for units, 1 for tens, and so forth.

Finding the _precision_ of an input quantity is much, much trickier, partly
because this concept is not really strictly defined (or well thought out) even
in pencil-and-paper communication. Presumably the intended precision in an
expression like 5.40 is "to the nearest hundredth", but what is the intended
precision in an expression like 54000 ?

Leaving this question (and other quibbles like it) aside, the real problem is
that your input to the calculator is not a _number_, but
a _representation_ of a number -- in fact a _decimal_ representation. Normally
the calculator changes this to a _binary_ representation for internal storage.
The stored value may differ slightly from the value that the inputter had in
mind, and the question of what precision was intended by the communication is
very hard to address when you are looking at only the binary value in hand.

One strategy would be to start with a maximum number of places, say 6.  If
int(1000000*N) is within 0.0001, say, of 1000000*N, then one
might assume that the intended precision of the input did not exceed 6 places.
Use a loop to step back to 5 places, etc.  Eventually you will discover a
discrepancy, and realize that there is a significant digit in a certain place,
and you are home.  You would be correct in most cases.

A totally different strategy would be to accept the input as a _string_
(possible on the TI-85, 83 and 86) and analyze the string for the presence of a
decimal point, then count the number of digits input following the decimal
point.  You'd have to get somewhat familiar with the string functions to follow
this approach, but it would probably give you more reliable results.  If you
were writing this program for fame and fortune, this would be the way to go.
If it's just a quick homework project, then analyzing the value as described
above might be your better choice.

RWW Taylor
National Technical Institute for the Deaf
Rochester Institute of Technology
Rochester NY 14623

>>>> The plural of mongoose begins with p. <<<<


Follow-Ups: