reature request: fixed point

Janice Caron caron800 at googlemail.com
Sat Mar 1 23:16:33 PST 2008


On 02/03/2008, Walter Bright <newshound1 at digitalmars.com> wrote:
> It isn't too hard to do fixed point arithmetic:
>
>  typedef int Fixed;
>  Fixed fixed(int m, int n) { return cast(Fixed)(m * 0x10000 + n); }
>
>  Fixed a = fixed(3,14);
>  Fixed b = fixed(2,3);
>  Fixed c = -fixed(5,3);
>  Fixed d = a * b / c;

That wouldn't work for

    Fixed e  = a * b;

But this would:

    struct Fixed(uint shift)
    {
        uint n;
        Fixed opMul(Fixed rhs)
        {
            return cast(uint)((cast(ulong)n * cast(ulong)rhs.n) >> shift);
        }
    }

    alias Fixed!(uint,16) Fixed32;

Of course, if you want also to do saturation (which digital signal
processing often requires) then there's that to take into account too.
So I still think a library solution would be best.



More information about the Digitalmars-d mailing list