[linux-audio-dev] Traps in floating point code

Mike Rawes mike_rawes at yahoo.co.uk
Wed Jun 30 15:37:59 UTC 2004


--- Pete Bessman <ninjadroid at gazuga.net> wrote: 
> At Mon, 28 Jun 2004 19:56:50 +1000,
> Erik de Castro Lopo wrote:
> > 
> >     double fractional = 0.0, increment = 0.1;
> >     int integer = 0;
> > 
> >     for (;;)
> >     {   
> >         /* Bunch of other code. */
> > 
> > 	fractional += increment ;
> >         integer += lrint (floor (fractional));
> >         fractional -= floor (fractional);
> >         }
> 
> I don't understand this lrint (floor ()) stuff.  floor() returns a
> truncated integer value, so what's the point of calling lrint() on
> that?  

The lrint converts the float value to an int (a lot) faster than a simple cast.

The floor is to get around the rounding rule used by the FPU when the float
value is exactly halfway. It rounds to even numbers, at least it does on an
AthlonXP:

lrintf (1.5) = 2
lrintf (2.5) = 2
lrintf (3.5) = 4
lrintf (4.5) = 4 etc.

which is a royal pain if you use that int for something like a table lookup.

- 
Myk



	
	
		
___________________________________________________________ALL-NEW Yahoo! Messenger - sooooo many all-new ways to express yourself http://uk.messenger.yahoo.com



More information about the Linux-audio-dev mailing list