Board index » delphi » Checking for a whole number

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!!
--
Jimb...@Hotmail.com
-
it wasn't easy, but nothing never is, no.  -BLUR

Damons rule, Hill and Albarn, Jordan and BLUR

 

Re:Checking for a whole number


Quote
Jimbahb wrote:
> 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

Re: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

Re: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<}}

Re:Checking for a whole number


In article <01be868e$ddf43640$LocalHost@home>,
  "Jimbahb" <Jimb...@hotmailNOSPAM.com> wrote:

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.

.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Checking for a whole number


Quote
netn...@altavista.net wrote:

|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.

Or, perhaps you should use (abs(frac(x))< 1.2 x 10^4932      

Phil

|-----------== Posted via Deja News, The Discussion Network ==----------
|http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Checking for a whole number


In article <tWIR2.1124$4I2.6...@newsfeed.slurp.net>,

Quote
  pgra...@dy{*word*193}.net wrote:
> netn...@altavista.net wrote:

> |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.

> Or, perhaps you should use (abs(frac(x))< 1.2 x 10^4932

not.

1)  1.2 x 10^4932 is not a valid IEEE 754 number

2)  if the number you gave were valid, your expression would
    always evaluate to TRUE

3)  frac(x)=0.0 is faster, simpler, and returns TRUE if and only if
    the internal representation of the number contains no fractional part
    (which presumably is what the questioner wanted)

.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Checking for a whole number


Quote
In article <7f5483$oi...@nnrp1.dejanews.com>,  <netn...@altavista.net> wrote:
>In article <01be868e$ddf43640$LocalHost@home>,
>  "Jimbahb" <Jimb...@hotmailNOSPAM.com> wrote:
>> 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
program we are talking about?

Osmo

Re:Checking for a whole number


Quote
phadr...@iname.com wrote:

|In article <tWIR2.1124$4I2.6...@newsfeed.slurp.net>,
Quote
|  pgra...@dy{*word*193}.net wrote:
|> netn...@altavista.net wrote:

|>
|> |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.
|>
|> Or, perhaps you should use (abs(frac(x))< 1.2 x 10^4932

|not.

Some days, it just doesn't pay to get out of bed.

|1)  1.2 x 10^4932 is not a valid IEEE 754 number

Should have said 1.0 x ......  But, of course, what I meant was
3.5 x -4932   approaching the smallest, not largest extended number

|2)  if the number you gave were valid, your expression would
|    always evaluate to TRUE

See above

|3)  frac(x)=0.0 is faster, simpler, and returns TRUE if and only if
|    the internal representation of the number contains no fractional part
|    (which presumably is what the questioner wanted)

That depends!  is (1/n)n a whole number?  

If you answer yes, then try the code that follows.  If you answer no,
your definition is a bit narrower than mine.

Phil

var
  t:extended;
  i:integer;

begin
  for i:=1 to 1000 do
  begin
     t:=1/i;
     t:=t*i;
     if  frac(t)<>0.0 then
     begin
        writeln('Error on ',i);
        readln;
     end;
  end;
end.

|.

|-----------== Posted via Deja News, The Discussion Network ==----------
|http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Checking for a whole number


In article <Ro1S2.913$R93.11...@newsfeed.slurp.net>,

Quote
  pgra...@dy{*word*193}.net wrote:
> phadr...@iname.com wrote:

> |In article <tWIR2.1124$4I2.6...@newsfeed.slurp.net>,
> |  pgra...@dy{*word*193}.net wrote:
> |> netn...@altavista.net wrote:
> |>
> |> |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.
> |>
> |> Or, perhaps you should use (abs(frac(x))< 1.2 x 10^4932

> |not.

> Some days, it just doesn't pay to get out of bed.

> |1)  1.2 x 10^4932 is not a valid IEEE 754 number

> Should have said 1.0 x ......

or rather 1.18973149535723E+4932  (approximately)

Quote
> But, of course, what I meant was
> 3.5 x -4932   approaching the smallest, not largest extended number

Phil,

the problem with your suggestion is that you have given no theoretical
foundation for it.

why 3.5e-4932? why pick this arbitrary value?

why not 3.36210314311209e-4932, the smallest normal number?

why not 3.36210314311208e-4932, the largest denormal number?

why not 1.0e-4933 or 1.0e-4934 or 1.0e-4935 etc?

Quote

> |2)  if the number you gave were valid, your expression would
> |    always evaluate to TRUE

> See above

> |3)  frac(x)=0.0 is faster, simpler, and returns TRUE if and only if
> |    the internal representation of the number contains no fractional part
> |    (which presumably is what the questioner wanted)

> That depends!

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.

Quote
> 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.

Quote

> If you answer yes, then try the code that follows.  If you answer no,
> your definition is a bit narrower than mine.

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

- Show quoted text -

Quote

> Phil

> var
>   t:extended;
>   i:integer;

> begin
>   for i:=1 to 1000 do
>   begin
>      t:=1/i;
>      t:=t*i;
>      if  frac(t)<>0.0 then
>      begin
>         writeln('Error on ',i);
>         readln;
>      end;
>   end;
> end.

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);
     readln;
   end;
end.

.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Checking for a whole number


Quote
netn...@altavista.net wrote:

|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);
|     readln;
|   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

Re:Checking for a whole number


In article <3MmS2.2397$R93.22...@newsfeed.slurp.net>,

Quote
  pgra...@dy{*word*193}.net wrote:
> netn...@altavista.net wrote:

> |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.

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_.  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.

Quote

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

Your example above simply means that your cheap desk calculator does
not yield a "whole number" (no fractional part) when you use it to
evaluate the expression (1/41)*41.

Quote

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

> Has no fractional part within the limits of the process!

The final "limits of the process" differ depending on the computation
performed. Assigning a fixed arbitrary limit without any theoretical
foundation is, sorry, sloppy programming.

- Show quoted text -

Quote

> |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);
> |     readln;
> |   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.

Maybe not in your book, but certainly in your computer. :-)

Try this Phil:

x := 6e-4932;

Is x a whole number?   why or why not?

How about if we now divide x by 2:

x := x/2.0;

Is x a whole number now?   why or why not?

.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Checking for a whole number


Quote
netn...@altavista.net wrote:

|In article <3MmS2.2397$R93.22...@newsfeed.slurp.net>,

|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
your answer.

Phil

Re:Checking for a whole number


In article <LrFS2.3511$R93.35...@newsfeed.slurp.net>,

Quote
  pgra...@dy{*word*193}.net wrote:
> netn...@altavista.net wrote:

> |In article <3MmS2.2397$R93.22...@newsfeed.slurp.net>,

> |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.

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

the solution you suggested, abs(frac(x))<3.5e-4932, will fail miserably
for this case, and many others.  try it.  then you will see what "netnews" is
trying to explain to you about inserting an arbitrary fudge factor without a
theoretical understanding.

.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Checking for a whole number


Quote
phadr...@iname.com wrote:

|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

Go to page: [1] [2]

Other Threads