Andrei E.
Andrei E.

Reputation: 61

Delphi : multiply integer with real number(newbie)

I want to multiply an integer with real number. This is my code:

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs;

type
  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
 num ,sumadh,sumadist,corectiah :integer;
 cs,cf,dist,deltahprim,deltah,H:Array of integer;
 corectiatest,cotareper:integer;
 kh  :real;

implementation

{$R *.fmx}

procedure TForm2.FormCreate(Sender: TObject);
var
  i: integer;    
begin
  num := 3;
  SetLength(cs, num);
  SetLength(cf, num);
  SetLength(dist, num);
  SetLength(deltahprim, num);
  SetLength(H, num);

  sumadh := 0;
  sumadist := 0;
  cotareper := StrtoInt(InputBox('Cota reper nivelment',
    'Valoare cota in cm', '0'));
  for i := 1 to num do
  begin
    cs[i] := StrtoInt(InputBox('Niveleul nr' + Inttostr(i),
      'Citire spate (cm)', '0'));

    cf[i] := StrtoInt(InputBox('Niveleul nr' + Inttostr(i),
      'Citire fata(cm)', '0'));

    dist[i] := StrtoInt(InputBox('Niveleul nr' + Inttostr(i),
      'Distanta intre mire(cm)', '0'));

    deltahprim[i] := cs[i] - cf[i];
    sumadh := sumadh + deltahprim[i];
    sumadist := sumadist + dist[i];
  end;

  corectiah := -sumadh;
  kh := corectiah / sumadist;

  ShowMessage('Suma deltah = ' + Inttostr(sumadh));
  ShowMessage('Suma distantelor este = ' + Inttostr(sumadist));
  ShowMessage('Corectia este = ' + Inttostr(corectiah));
  ShowMessage('Cota reper este = ' + Inttostr(cotareper));

  for i := 1 to num do
  begin

    deltah[i] := deltahprim[i] + kh * dist[i];
    ShowMessage('delta h compensat = ' + Inttostr(deltah[i]));

  end;
end;

end.

The variable kh will be something like 0.0005 and it needs to get multiplied with dist[i]. But I cannot run the code. I'm getting the following error message:

incompatible types integer and double.

Is there any integer-to-real function? E.g. something like InttoReal?

Upvotes: 1

Views: 3767

Answers (3)

LU RD
LU RD

Reputation: 34899

incompatible types integer and double

You must convert the multiplication of the integer and the float value to an integer value, when assigning to an integer.

Use

deltah[i] := deltahprim[i] + Round(kh * dist[i]);  // Or Trunc(kh * Dist[i]);

A float multiplied with an integer promotes the integer to a float during the multiplication. The result is a float value, and it is the programmers duty to convert it back to an integer (when assigned to an integer), since the compiler cannot evaluate the intention of the multiplication.

Round() and Trunc() usually covers the conversion needs.


If the intention was to keep as much relevant precision as possible, just declare deltah as an array of double instead (skipping the round/trunc).


Other problems in code:

  • You have to allocate the deltah array with SetLength(). This is the probable cause of your exception as mentioned in comments.

  • All loops must go from zero to num-1, since dynamic arrays starts with zero index.


Turn on range checking and use the debugger to single step your code. The IDE is a resourceful tool to find those errors.

Upvotes: 3

Gianluca Colombo
Gianluca Colombo

Reputation: 819

deltah is an Array of integer and you are trying to get a float calculation, so you have to change the type of the deltah array to array of double

deltah: array of double;

deltah[i]:=(deltahprim[i]+kh*dist[i]);
ShowMessage('delta h compensat = '+FloatToStr(deltah[i]));

Upvotes: 0

Johan
Johan

Reputation: 76567

Very simple, you just multiply.

var
  i: integer;
  r: real;
  p: real;
begin
  p:= r * i;
end;

Just make sure that you store the result in a floating point variable.

If you want to store the result in an integer, you'll have to do some rounding, or truncating.

var
  i: integer;
  r: real;
  pi: integer;
begin
  pi:= round(r * i);
end;

Upvotes: 1

Related Questions