V
V
Vyacheslav2015-02-27 12:22:14
Delphi
Vyacheslav, 2015-02-27 12:22:14

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;

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
mayorovp, 2015-02-27
@mayorovp

There is such a construction - try ... catch ... end. Read about her.

K
kalapanga, 2015-02-27
@kalapanga

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.

A
Anton Fedoryan, 2015-02-27
@AnnTHony

Add the condition you need:
instead y1:=ln(x);, you can write

if ln(x) < 0 then
  y1 := 0; //или чему-то еще
else 
  y1:=ln(x)

Although this is not python, it would also be good form to format the code.

M
Maxim Moseychuk, 2015-02-28
@fshp

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 question

Ask a Question

731 491 924 answers to any question