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

Re: Representing percentage




Neelakantan Krishnaswami wrote in message ...
>On Sun, 28 Jan 2001 15:56:28 +0200, Nimi <nimi@better.tv> wrote:
>>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) -
>>
>> 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?
>
>If you have both percentages and absolute sizes floating around your
>program, you have to discriminate between the two. The type header in
><percentage> does that for you. You can also add an argument to the
>function like


The other problem with dispatching off of a percentage type
that is implemented solely as a limited integer is that other
values that happen to fall between 0 and 100 are ambiguous.
What if you happen to have a slot in a class that represents
a person's age?  For anybody less than 100 years old, their
age would also be a <percentage>, which I am sure you
don't want.

So -- even in a language like Cecil which supports this kind
of thing -- you want to either represent percentages as real,
live classes, or you should just interpret an integer properly
in the right place.  Note that there's no possibility that you
will have more than 100 different percentages, right?  If
you make <percentage> be immutable (the setter for p
being #f), you can add a 'make' method that chooses from
1 of the 101 possible percentage objects, and then share
among them.  (I think the design pattern weenies would
say they are using a flyweight coupled with some sort of
instance factory...)  This is a more portable solution than
the admittedly cool Gwydion hack.






Follow-Ups: References: