Checking for a whole number
Author Message
Checking for a whole number

hi,
How can I check to find out if something entered is a whole number (i.e.
13) or a fraction (ie13.4)?

Ps. ITS SNOWING!!
--

-
it wasn't easy, but nothing never is, no.  -BLUR

Damons rule, Hill and Albarn, Jordan and BLUR

Wed, 18 Jun 1902 08:00:00 GMT
Checking for a whole number

Quote:

> hi,
> How can I check to find out if something entered is a whole number
> (i.e.
> 13) or a fraction (ie13.4)?

Depends on whether you are reading it in as a string or as a real
variable.  If as a string, I would check for the decimal point.  If as a
real, I would check to see if it is equal
to the integer nearest to it (I'm assuming you know the Standard Pascal
function that gives
you this, right?).

Bob Schor
Pascal Enthusiast

Wed, 18 Jun 1902 08:00:00 GMT
Checking for a whole number

Quote:

>      Depends on whether you are reading it in as a string or as a real
> variable.  If as a string, I would check for the decimal point.  If as a
> real, I would check to see if it is equal
> to the integer nearest to it (I'm assuming you know the Standard Pascal
> function that gives
> you this, right?).

> Bob Schor
> Pascal Enthusiast

o, ye, Haha, I didn't think of checking for a dp in the string, DOH!!

I've been messing about converting the thing and everything.  Thanks a lot.

John

Wed, 18 Jun 1902 08:00:00 GMT
Checking for a whole number

Quote:
> > How can I check to find out if something entered is a whole number
> > (i.e. 13) or a fraction (ie13.4)?

>      Depends on whether you are reading it in as a string or as a real
> variable.  If as a string, I would check for the decimal point.  If as a
> real, I would check to see if it is equal
> to the integer nearest to it (I'm assuming you know the Standard Pascal
> function that gives you this, right?).

I think it's a little more than that.  For example, a valid whole
number could be negative.  Or a Val of a Real variable could/would work
with a whole number, but there are ways to test or convert it to integer,
etc.  All sorts of things one _might_ want or need to do, depending on
the application's requirements.
My point being, the original posted question is pretty vague, and
there could easily be more factors than we stated, right?  8<}}

Wed, 18 Jun 1902 08:00:00 GMT
Checking for a whole number

Quote:

>> hi,
>> How can I check to find out if something entered is a whole number (i.e.
>> 13) or a fraction (ie13.4)?

>Checking for the existence of a decimal point won't work if you want to
>consider 13.0 or 1.3e1 to be "whole" numbers.  In that case, given a
>floating-point variable x,  the expression (frac(x)=0.0) will return TRUE if
>x is a "whole" number and FALSE if it is not.

I would want to know what is the actual problem. I.e. what kind of a

Osmo

Wed, 18 Jun 1902 08:00:00 GMT
Checking for a whole number

|The statement as worded is unambiguously true in all cases:
|frac(x)=0.0 returns TRUE if and only if the internal representation of the
|number contains no fractional part.  See the modification to your example
|program below to illustrate this.

|> is (1/n)n a whole number?

|analytically, yes (for n>0).  numerically, not always.
|(t/41.0)*41.0 for example does not yield a "whole number" (no fractional part)
|for IEEE 754 80-bit extended floats.

But the reason it doesn't yield a whole number is because of
limitations in the process.  Just because the hardware cannot compute
the result correctly does not change the fact that (1/41)*41 IS 1.

My cheap desk calculator says the result is 0.9999982  Does that make
it so?

|What is your definition, if not that a whole number has no fractional part?

Has no fractional part within the limits of the process!

|Try this modification to your program and you will see
|what is really happening:

|var
|t:extended;
|i:integer;

|begin
| for i:=1 to 1000 do
|   begin
|     t:=1/i;
|     t:=t*i;
|     writeln('i=',i:4,' (t-1.0)=',t-1.0,'  whole number? ',frac(t)=0);
|   end;
|end.

Because this code yields:

i=  41 (t-1.0)=-5.42101086242752E-0020  whole number? FALSE

This means that (1/41)*41 does not equal 1?  Not in my book.

Phil

Wed, 18 Jun 1902 08:00:00 GMT
Checking for a whole number

|I detect a fundamental misunderstanding here.  The question is not what
|(1/41)*41 _ought_ to be, but rather what (1/41)*41 actually _is_.

(1/41)*41 is 1.  This is the fact.

No  arbitraily-approximate piece of software-assisted hardware can
change that.  If the result is not 1, you are using the hardware
beyond its limits.  Results are unpredictable.

|  The fact
|is, on an Intel machine using extended-precision 80-bit floating-point
|arithmetic, (1/41)*41 is *not* equal to 1, no matter how much you would like
|it to be so. Introducing an arbitrary fudge factor, without any theoretical
|foundation, is asking for trouble.  It may seem to give the "correct" answer
|in some finite set of examples you try, but it's likely to fail at some edge
|condition when you least expect it.

Perhaps this is the root of our disagreement (rather than my
misunderstanding).  Using the floating-point processor, which is not
(and is not intended to be) precise to judge an edge condition is
probably not the best way to solve the problem.  If you are going to
do it anyway, your solution will be flawed in some way.  Which flaws
you accept are a matter of choice, and may depend on the type of
problem you are solving, the range of your data, and the purpose of

Phil

Wed, 18 Jun 1902 08:00:00 GMT
Checking for a whole number

|you are extraordinarily stubborn.  if you would stop being so defensive and
|think about it, you might learn something.

So educate me!

|the solution you suggested, abs(frac(x))<3.5e-4932, will fail miserably
|for this case, and many others.

I agree.

What I was looking for was a way to say that the number is a whole
number if the fractional portion approaches the limits of the 80-bit
resolution, after allowing for the bits used by the integer portion.
My solution didn't do that.

|  try it.  then you will see what "netnews" is
|trying to explain to you about inserting an arbitrary fudge factor without a
|theoretical understanding.

My theoretical understanding is lacking - I will admit that.  That
doesn't change the fact that the "netnews" solution, (frac(x)=0.0),
also has flaws.  It may be the best answer, but I don't think so.

Phil

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 2 [ 16 post ] Go to page: [1] [2]

Relevant Pages