Re: checking if a floating point number is equal in value to an integer
- From: ejko123@xxxxxxxxx
- Date: 29 Jan 2007 18:50:03 -0800
On Jan 29, 6:56 pm, analys...@xxxxxxxxxxx wrote:
I am using float(nint(a)) == a as the test. I'd appreciate any
comments and/or better suggestions.
Also - I am interested in a NINT function that will use a random
number to decide what to do when the argument is an exact half-integer
(my idea is to test if 2.0*argument is an exact integer).
As far as I know, FLOAT is archaic at best and nonstandard at worst.
It can fail if A is a sufficiently large double-precision number,
because
the rounded result need not fit into a default integer word (on a
typical
machine, A has 53 bits in the significand and NINT returns a 32-bit
integer value).
A more portable test is mod(a,real(1,kind(a))) == 0, which
should give reliable results for any floating-point type as long as
the
arithmetic is correctly rounded. (Of course, if A is single precision
and you're on an IEEE-format machine, then A is always an integer
if its absolute value is greater than 2**24.)
Under correctly rounded arithmetic, the test mod(a,1.0) == 0.5
should reliably report exact half-integer values when A is of
default real kind.
--Eric
.
- Follow-Ups:
- References:
- Prev by Date: checking if a floating point number is equal in value to an integer
- Next by Date: Re: checking if a floating point number is equal in value to an integer
- Previous by thread: checking if a floating point number is equal in value to an integer
- Next by thread: Re: checking if a floating point number is equal in value to an integer
- Index(es):