Re: checking if a floating point number is equal in value to an integer
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 30 Jan 2007 04:41:01 +0000 (UTC)
analyst41@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).
nint(a) has integer type, and many (most) floating point values
don't have a valid integer nearby.
The aint() function has real type, giving the truncated value
as type real (of the appropriate kind for versions that support
kind). So I would suggest aint(a)==a as the test over
float(nint(a))==a.
For machines with non base 2 floating point (such as base 10 or 16)
you can't reliably test with 2.0*argument, as the fraction might
be renormalized after the multiply. Consider a three digit decimal
machine with 8.51e0, mutliplied by two gives 1.70e1.
I believe that abs(aint(a)-a)==0.5 should work, though.
-- glen
.
- Follow-Ups:
- References:
- Prev by Date: Re: Declaration to get 8-bit (or 16-bit) integer?
- Next by Date: Re: Declaration to get 8-bit (or 16-bit) integer?
- Previous by thread: Re: 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):