Normalized polarity

Though strikingly obvious to me now, I've often sought a quick (expressive) way to determine the normalized polarity of a number. What I am esoterically calling a normalized polarity is: a function that is 1 for all positive numbers, 0 for 0, and -1 for all negative numbers. I have often found the normalized polarity useful in financial and graphics/layout contexts.

The now obvious solution is to treat zero as identity, otherwise divide the number by the absolute value of itself.

create operator NormalizedPolarity(AX : Decimal) : Integer
begin
result := if AX = 0 then 0 else (AX / Abs(AX)).ToInteger();
end;

Examples (using D4):
select NormalizedPolarity(-2.3); // -1
select NormalizedPolarity(554.1); // 1
select NormalizedPolarity(0); // 0

Sorry if this seems painfully obvious, just thought I would share in case...

Comments

Popular posts from this blog

Don't Repeat Yourself... Really!

Issues raised by polymorphism in relation land

Camtasia Studio Tips