LNXdev.com

Pythagoras function

Print length of missing side

This function prints the result on the notification bar on machine view. This function can easily be modified to return the value back to the calling program.

PROC PYTH(REAL valueA,REAL valueB,REAL valueC)

; PYTHAGORAS CALCULATIONS
; V1.0, 14th JUL 2010 Juha Wisakanto
; POT (valueA) + POT(valueB) = POT(valueC)

; Variable definitions

DEF BOOL kill
DEF REAL result
DEF REAL valueMax
DEF REAL valueMin



; Input control

IF (valueA > 0) AND (valueB > 0) AND (valueC > 0)
   GOTOF ERR03
ENDIF

IF valueA + valueB == 0
   kill = TRUE
ENDIF

IF valueB + valueC == 0
   kill = TRUE
ENDIF

IF valueA + valueC == 0
   kill = TRUE
ENDIF

IF kill == TRUE
   GOTOF ERR01
ENDIF

IF valueA < 0 GOTOF ERR02
IF valueB < 0 GOTOF ERR02
IF valueC < 0 GOTOF ERR02

; Arithmetics

; Looking for valueA
IF valueA <= 0
   valueB = POT(valueB)
   valueC = POT(valueC)
   valueMax = MAXVAL(valueB, valueC)
   valueMin = MINVAL(valueB, valueC)
   result = SQRT(valueMax - valueMin)
   MSG("Length of a component in this triangle is "<<result)
   M0
   GOTOF END
ENDIF

; Looking for valueB
IF valueB <= 0
   valueA = POT(valueA)
   valueC = POT(valueC)
   valueMax = MAXVAL(valueA, valueC)
   valueMin = MINVAL(valueA, valueC)
   result = SQRT(valueMax - valueMin)
   MSG("Length of B component in this triangle is "<<result)
   M0
   GOTOF END
ENDIF

; Looking for valueC
IF valueC <= 0
   valueA = POT(valueA)
   valueB = POT(valueB)
   result = SQRT(valueA + valueB)
   MSG("Length of C component in this triangle is "<<result)
   M0
   GOTOF END
ENDIF

; Error handling
ERR01:
   MSG("Two or more components equal zero, cannot calculate")
   M0
   GOTOF END

ERR02:
   MSG("One or more components are less than zero, cannot calculate")
   M0
   GOTOF END

ERR03:
   MSG("One component must be 0, cannot calculate")
   M0
   GOTOF END
; The End
END:
RET