# Comparison of negative and positive numbers

I have a comparison function that is getting way too complex for what I'm trying to do. I will receive a double that can be positive or negative. I will be storing a global variable that needs to be the smallest value. In terms of comparison, this means it is the smallest positive number, or if the incoming values are negative, the smallest global value could be negative.

My code is turning into a bunch of if statements handling if the incoming is positive or negative. Then i need to compare differently if the current global value is positive or negative.

Is there a better way to simply compare two numbers and get the lowest of the two?

Partial example code:

if NestObject.RectangularScrap <> 0 then begin if NestObject.RectangularScrap > 0 then begin //Positive rect scrap if(NestObject.RectangularScrap < GBestRect) then begin GBestRect := NestObject.RectangularScrap; end; if(NestObject.RectangularScrap > GWorstRect) then begin GWorstRect := NestObject.RectangularScrap; end; end else begin //Negative rect scrap if GBestRect > 0 then begin //Global value is currently positive GBestRect := NestObject.RectangularScrap; end else begin //Global value is currently negative, change both values to postive to compare if((-1*NestObject.RectangularScrap) < (-1*GBestRect)) then begin GBestRect := NestObject.RectangularScrap; end; if((-1*NestObject.RectangularScrap) > GWorstRect) then begin GWorstRect := NestObject.RectangularScrap; end; end; end; end;

## Answers

have you tried the min function ?

http://www.delphibasics.co.uk/RTL.asp?Name=Min