develooper Front page | perl.perl5.porters | Postings from February 2001

Re: IV preservation (was Re: [PATCH 5.7.0] compiling on OS/2)

From:
nick
Date:
February 16, 2001 14:33
Subject:
Re: IV preservation (was Re: [PATCH 5.7.0] compiling on OS/2)
Message ID:
E14TtNd-0004bF-00@roam1
Ilya Zakharevich <ilya@math.ohio-state.edu> writes:
>On Fri, Feb 16, 2001 at 09:28:32PM +0000, nick@ing-simmons.net wrote:
>> >Etc etc etc.  Checking that a float is an integer is
>> >a non-trivial and a very expensive operation.
>> 
>> No it isn't (at least for IEEE). It is a simple matter of comparing 
>> the (binary) exponent with the (known, constant) number of bits in the mantissa.
>> and then seeing if there are any non-zero bits to the right of the radix point.
>> (am I missing something here?).
>
>AFAIK, there is no API to extract the exponent and the mantissa from
>a double.  Did I miss it?

My copy of IEEE-754 is in a dusty cuboard at work, so I will not try 
and remember the SHALLs and MUSTs vs SHOULDs. I probably have the 
code that implemented them round here-at-home somewhere. 

However most UNIXs have 

SYNOPSIS
       #include <math.h>
 
       double frexp(double x, int *exp);
 
DESCRIPTION
       The frexp() function is used to split the number x into  a
       normalized  fraction  and  an  exponent which is stored in
       exp.              

               
       double modf(double x, double *iptr);
 
DESCRIPTION
       The modf() function breaks the argument x into an integral
       part  and  a  fractional  part, each of which has the same
       sign as x.  The integral part is stored in iptr.
 
RETURN VALUE
       The modf() function returns the fractional part of x.       

Both are "easy" to do by bit twiddling for IEEE format.
(As is ldexp())

modf() is the one you want for "is integer" testing.

if (modf(value,&ipart) == 0.0 && MIN_INT <= ipart && ipart <= MAX_INT)

That modf actually does rather more than is necessary in this case
as it re-formats the fraction bits of mantissa as a valid double,
which one does not need to do for testing against zero.

The main pain with doing bit-twiddling in software version of IEEE
is the "hidden" MS bit of mantissa.


-- 
Nick Ing-Simmons




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About