Re: Floating point in kernel

From: Abramo Bagnara (abramo@alsa-project.org)
Date: Thu May 04 2000 - 14:27:53 EST


"Richard B. Johnson" wrote:
>
> On Wed, 3 May 2000, Jeff Garzik wrote:
>
> > "B. James Phillippe" wrote:
> > >
> > > Hello,
> > >
> > > I know this is not legal. However, I'm reviewing some kernel module code
> > > which basically does:
> > >
> > > unsigned long foo = some_other_ulong * 1.234;
> >
> > oof :) Reminds me of some fbdev code I saw a while back IIRC
> >
> > When you really want fractional numbers not literally floating point,
> > you can always multiple both numbers to make them decimal, perform the
> > math op, and then divide back down again.
> >
> > Jeff
> >
>
> Correct. In the cited case, the code could read:
>
> foo = some_other_ulong * 1234 / 1000;
>
> The complier sometimes optimizes the wrong stuff so 1234/1000 might
> get optimized to 1 which is not what you want. Therefore, you can
> force the issue by doing:
>
> foo = some_other_ulong * 1234;
> ^__ sequence point
> foo /= 1000;
>

As far as I know this is not needed at all.
* and / have the same precedence and associativity is specified from
left to right so this is interpreted as:

foo = (some_other_ulong * 1234) / 1000

It's not guaranted that some_other_ulong * 1234 is evaluated before 1000
but we are sure that multiplication is done before division.

If I'm wrong I'd like to know ASAP as I've written bogus C code for 15
years now ;-))

-- 
Abramo Bagnara                       mailto:abramo@alsa-project.org

Opera Unica Via Emilia Interna, 140 Phone: +39.0546.656023 48014 Castel Bolognese (RA) - Italy Fax: +39.0546.656023

ALSA project is http://www.alsa-project.org sponsored by SuSE Linux http://www.suse.com

It sounds good!

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sun May 07 2000 - 21:00:15 EST