Delphi 负数和正数的比较
我有一个比较函数,对于我要做的事情来说,它变得太复杂了。我会收到一张双份的,可以是正面的,也可以是负面的。我将存储一个需要最小值的全局变量。就比较而言,这意味着它是最小的正数,或者如果传入值为负数,则最小的全局值可能为负数 我的代码正在变成一堆Delphi 负数和正数的比较,delphi,comparison,Delphi,Comparison,我有一个比较函数,对于我要做的事情来说,它变得太复杂了。我会收到一张双份的,可以是正面的,也可以是负面的。我将存储一个需要最小值的全局变量。就比较而言,这意味着它是最小的正数,或者如果传入值为负数,则最小的全局值可能为负数 我的代码正在变成一堆if语句,处理传入的是正还是负。然后,如果当前全局值为正或负,我需要进行不同的比较 有没有更好的方法简单地比较两个数字,并从中找出最低的一个 部分示例代码: if NestObject.RectangularScrap <> 0 then be
if
语句,处理传入的是正还是负。然后,如果当前全局值为正或负,我需要进行不同的比较
有没有更好的方法简单地比较两个数字,并从中找出最低的一个
部分示例代码:
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;
如果NestObject.RectangularScrap为0,则开始
如果NestObject.RectangularScrap>0,则开始
//正矩形废料
如果(NestObject.RectangularScrapGWorstRect),则开始
GWorstRect:=NestObject.RectangularScrap;
终止
终止
否则开始
//负矩形废料
如果GBestRect>0,则开始
//全球价值目前为正值
GBestRect:=NestObject.RectangularScrap;
终止
否则开始
//全局值当前为负值,请将这两个值更改为正值以进行比较
如果(-1*NestObject.RectangularScrap)<-1*GBestRect),则开始
GBestRect:=NestObject.RectangularScrap;
终止
如果(-1*NestObject.RectangularScrap)>GWorstRect),则开始
GWorstRect:=NestObject.RectangularScrap;
终止
终止
终止
终止
您是否尝试过min功能
没错。看,我知道有一个干净的方法可以做到这一点,我只是不知道该搜索哪个词。谢谢。为什么你认为你需要进行不同的比较?
运算符处理所有数字,而不仅仅是正数。此外,要更改数字的符号,只需对其应用-
运算符;没有必要乘以-1。