quinta-feira, 4 de março de 2010

How to find the larger of two numbers

A simple requirement like finding the larger of 2 numbers can make us think when the operators at hand are limited to basic + - * / DIV MOD SQRT.
Maintaining formula in database view V_TC25 is such a case. The formula builder of V_TC25 isn't as rich as expected and does not allow us to use logical conditions or ABSOLUTE values. Hence, the need of converting the logical condition:

IF A > B.
C = A
ELSE.
C = B.
ENDIF.

into an arithmetic expression using simple operators + - * / SQRT.

The arithmetic expression to get the larger of the two numbers is as follows:

(A+B)-(((A+B)-ABS(A-B))/2)

Since ABS is not available, but SQRT is, we convert that too into a simple arithmetic expression as follows:

ABS(A) = SQRT(A*A)

Hence the final expression to find the larger number becomes:

(A+B)-(((A+B)-SQRT((A-B)*(A-B))/2)

Sem comentários:

Enviar um comentário