PDA

View Full Version : Remainder



Voltrox
03-03-2007, 11:42 PM
Hello,

I want to divide in Delphi but get the remainders...

Could someone please help me?

Dan
04-03-2007, 12:02 AM
5 mod 2 = 1

Traveler
04-03-2007, 12:02 AM
Like so?
10 / 5

or so?

10 div 5

edit, or like how Dan said :)

Voltrox
04-03-2007, 12:17 AM
What I meant is how do I divide and get the remainder in a separate variable?

Traveler
04-03-2007, 12:40 AM
In that case, using mod is what you want

yourVarName := 5 mod 2;

Voltrox
04-03-2007, 03:03 AM
unit BINACONV;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, XPMan;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
XPManifest1: TXPManifest;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
V_Remainder: Double;
V_NOTODIVIDE: Double;
V_DIVRESULT: Double;
V_FINAL: string;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
V_NOTODIVIDE := StrToInt(Edit1.Text);
V_NOTODIVIDE := V_NOTODIVIDE / 2;
V_Remainder := V_NOTODIVIDE mod 2;
V_FINAL := V_FINAL + (IntToStr(V_Remainder));

Memo1.Lines.Add(V_FINAL);


end;

end.



Ok, thanks, but, it gives me the following errors:

[Pascal Error] BINACONV.pas(36): E2015 Operator not applicable to this operand type

[Pascal Error] BINACONV.pas(37): E2250 There is no overloaded version of 'IntToStr' that can be called with these arguments

Help?

Sly
04-03-2007, 03:08 AM
You're trying to pass an double to IntToStr. Try not to get integers and floats mixed up.



var
Form1: TForm1;
V_Remainder: Integer;
V_NOTODIVIDE: Integer;
V_DIVRESULT: Integer;
V_FINAL: String;

implementation

procedure TForm1.Button1Click(Sender: TObject);
begin
V_NOTODIVIDE := StrToInt(Edit1.Text);
V_NOTODIVIDE := V_NOTODIVIDE div 2;
V_Remainder := V_NOTODIVIDE mod 2;
V_FINAL := V_FINAL + IntToStr(V_Remainder);
Memo1.Lines.Add(V_FINAL);
end;

3_of_8
04-03-2007, 10:07 AM
div and mod are the same operation for the CPU.


asm
mov eax, 42
cdq
mov ecx, 9
div ecx
end;


This assembler code uses the div-command to divide 42 by 9. The quotient is then in eax and the remainder in edx. So you can see, div and mod are basically the same, they just take the result from different registers.

div is only available in the CPU for integer types. The FPU, which is used for floating point types, knows a division, but NOT an integer division.

So you can't get the integer quotient or the remainder of a float with Delphi operators. You could do it with an iterative subtraction, but this can last quite long with big values. For example 1E900/1E-900. You can also do it by dividing the dividend by the divisor, then take the integer part of the quotient and take the fraction part of the quotient multiplied with the dividend as remainder. This would be way more efficient.

But calculating with floats always takes more time than the same thing with integers. So, if you only have integer values or if you can do it somehow with integer values, use them. Avoid floats if possible.

EDIT: By the way, upper case identifiers are normally reserved for constants. I wouldn't use them as variables or fields. This can make your code way harder to understand for others.

Chebmaster
20-03-2007, 04:52 PM
[Pascal Error] BINACONV.pas(36): E2015 Operator not applicable to this operand type

Double is a floating-point type.
AFAIR, mod operator, as well as its counterpart div, only works with integer operands.