Reputation: 2593
How can i find out which number is closer? say my value is "1" and i have two var, A:= 1.6 and b:=1.001
currently looking at a few numbers and taking a 0.1% +/- difference and a +/- 0.6 difference.. i just need to see which answer is closer to the starting value.. code so far..
Also nothing to big, the code is just to stop me from doing them all manually :D
procedure TForm1.Button1Click(Sender: TObject);
var
winlimit,test6high,test6low,test6,test1high,test1low,test1,value: double;
begin
value := 1.0;
while value < 1048567 do
begin
test6high := value + 0.6 ;
test6low := value - 0.6 ;
test1high := (-0.1 * value)/100;
test1high := value - test1high;
test1low := (0.1 * value)/100;
test1low := value - test1low;
memo1.Lines.Add('value is '+floattostr(value)+': 1% High:'+floattostr(Test1high)+' 1% Low:'+floattostr(Test1low));
memo1.Lines.Add('value is '+floattostr(value)+': 0.6 +/- '+floattostr(Test6high)+' 0.6 Low:'+floattostr(Test6low));
memo1.Lines.Add(' ');
value := value*2;
end
end;
Upvotes: 1
Views: 438
Reputation: 612954
I think you mean a function like this:
function ClosestTo(const Target, Value1, Value2: Double): Double;
begin
if abs(Target-Value1)<abs(Target-Value2) then
Result := Value1
else
Result := Value2;
end;
If you use IfThen
from the Math
unit you can write it more concisely:
function ClosestTo(const Target, Value1, Value2: Double): Double;
begin
Result := IfThen(abs(Target-Value1)<abs(Target-Value2), Value1, Value2);
end;
Upvotes: 4