Reputation: 4008
First day using Delphi Community IDE, I've been able to create a calculator app. I've done mostly Python
and R
programming.
But I'm having trouble with the Division Operation. I've hardcoded 2 values just to make sure the operation is based on float numbers.
I would like to get 2.5
in the panel caption, when dividing 5 to 2.
iAns := 5 / 2.0; yields:
[dcc32 Error] hello_world.pas(189): E2010 Incompatible types: 'Integer' and 'Extended'
[dcc32 Error] hello_world.pas(189): E2010 Incompatible types: 'Integer' and 'Extended'
[dcc32 Fatal Error] Project1.dpr(5): F2063 Could not compile used unit 'hello_world.pas'
This is the function that does the operation based on if else conditionals:
procedure TForm1.btnEqualsClick(Sender: TObject);
begin
iNum2 := StrToInt(edt1.Text);
edt1.Clear;
ShowMessage(IntToStr(iNum1));
ShowMessage(IntToStr(iNum2));
if Operant = '+' then
begin
iAns := iNum1 + iNum2;
pnl1.Caption := IntToStr(iAns);
end
else if Operant = '-' then
begin
iAns := iNum1 - iNum2;
pnl1.Caption := IntToStr(iAns);
end
else if Operant = '*' then
begin
iAns := iNum1 * iNum2;
pnl1.Caption := IntToStr(iAns);
end
else if Operant = '/' then
begin
iAns := 5 / 2.0;
pnl1.Caption := FloatToStr(iAns);
end
end;
end.
UPDATE 1:
var
Form1: TForm1;
iNum1, iNum2, iAns : Integer;
rNum1, rNum2, rAns : Integer;
Operant : String;
Upvotes: 2
Views: 8546
Reputation: 108919
iAns
is declared as an integer. And a variable of integer type cannot hold a non-integral value like 2.5
, which is what you get by 5 / 2.0
. You need to declare iAns
with a floating-point type instead, like double
(or real
, single
, extended
: read the documentation about these).
Some additional hints:
5 / 2
will work just as well as 5 / 2.0
, since the /
operator always performs a floating-point division. You will get 2.5
.div
. 5 div 2
yields the integer 2
.Notice how the compiler actually told you this already. It probably pointed you to the line iAns := 5 / 2.0
telling you that integer
(the type of iAns
) isn't compatible with extended
(the type of the right-hand side).
Upvotes: 5