Answer the question
In order to leave comments, you need to log in
How to put limit on ln in edit?
Good day!
I wanted to make a program for tabulating two functions, but I ran into a problem. If the user enters data for the beginning or end of the segment in negative values, then it gives a fatal error stating that ln cannot be less than zero. How to make it so that a message is displayed that the input data does not satisfy the condition, but the program continues?
Here is the code
procedure TForm1.Button2Click(Sender: TObject);
var x,y,y1,xn,xk,H: real;
begin
Memo1.ScrollBars:=ssVertical;
xn:=StrToFloat(Edit1.Text);
xk:=StrToFloat(Edit2.Text);
H:=StrToFloat(Edit3.Text);
Memo1.Lines.Add('Первая функция Вторая функция');
Memo1.Lines.Add('X Y X Y ');
x:=xn;
while x<=xk do
begin
y:=cos(x);
y1:=ln(x);
Memo1.Lines.Add(FloatToStrF(x,ffFixed,5,3)+' '
+FloatToStrF(y,fffixed,5,3)+' '
+FloatToStrF(x,fffixed,5,3)+' '
+FloatToStrF(y1,fffixed,5,3));
x:=x+H;
end;
end;
Answer the question
In order to leave comments, you need to log in
There is such a construction - try ... catch ... end. Read about her.
It's a bad idea to ignore errors. Your programs with this approach will behave unpredictably. If you can't enter negative numbers, then you can't! Check this before starting calculations, issue a warning to the user, and only with correct input start calculating your tables.
Add the condition you need:
instead y1:=ln(x);
, you can write
if ln(x) < 0 then
y1 := 0; //или чему-то еще
else
y1:=ln(x)
The logarithm may well be less than zero, its argument cannot be zero or less.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question