[Haskell-cafe] fast fractional part of floating point number - modf?

John Meacham john at repetae.net
Tue Jan 15 07:08:03 EST 2008


On Tue, Jan 15, 2008 at 07:40:03AM +0100, Henning Thielemann wrote:
> 
> On Mon, 14 Jan 2008, John Meacham wrote:
> 
> > On Sun, Jan 13, 2008 at 01:08:49AM +0100, Henning Thielemann wrote:
> > > Is there a fast and reliable way to compute the fraction of a floating
> > > point number?
> >
> > no, and this has bothered me to the point I consider it a bug in the
> > language spec that all 'rounding' style functions give back an integral
> > type.
> 
> I find returning an Integral is appropriate. The type expresses perfectly
> what kind of values you can expect. Sure, there should be some
> optimization rule like
>   fromInteger (round x)  =  roundf x  .

it is not that it isn't appropriate, it is that is isn't that useful nor
is it equivalent. 

roundf NaN -> NaN
fromInteger $ round NaN -> error

roundf -0 -> -0
fromInteger $ round -0 -> 0

and how about 

fromInteger $ ceil (the highest represpentable number in a Double) 

you go from a 64 bit primitive value to one that takes up a substantial amount of
memory when expressed as an Integer and back again.

All of these tricky corner cases have been thought out by the IEEE.

more importantly, all these things have well defined meanings in IEEE
floating point specfications and they cannot be followed in haskell due
to the integral constraint. Floating point numbers are odd beasts, we
must be careful with them.

        John

-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-Cafe mailing list