[Prev][Next][Index][Thread]

Representing percentage



Hi,

I'm trying to find a simple way to represent percentage. I thought of doing
it like this:

    define constant <percentage> = limited(<integer>, min: 0, max: 100);

But this is no good if I want to make a generic function that specializes on
both <integer> (for absolute values, say, the number of elements in in a
collection passed as another argument) and <percentage> (the relative size
of the other argument) -

    define method foo(c :: <collection>, n :: <integer>)
        return n elements of c;
    end method;

    define method foo(c :: <collection>, p :: <percentage>)
        return p% of the size of c;
    end method;

This problem is solved if I do it this way:

    define class <percentage> (<number>)
        slot p :: limited(<integer>, min: 0, max: 100);
    end class;

but it doesn't seem very efficient. Is there a better way to do it?

Thanks,
Nimi Peleg





Follow-Ups: