Results 1 to 9 of 9

Thread: Remainder

  1. #1

    Remainder

    Hello,

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

    Could someone please help me?
    Nicholas.
    <br />
    <br />Please join: http://holzstukka.proboards81.com

  2. #2

  3. #3

    Remainder

    Like so?
    [pascal]10 / 5[/pascal]

    or so?

    [pascal]10 div 5[/pascal]

    edit, or like how Dan said [size=9px][/size]

  4. #4

    Remainder

    What I meant is how do I divide and get the remainder in a separate variable?
    Nicholas.
    <br />
    <br />Please join: http://holzstukka.proboards81.com

  5. #5

    Remainder

    In that case, using mod is what you want

    [pascal]yourVarName := 5 mod 2;[/pascal]

  6. #6

    Remainder

    Code:
    unit BINACONV;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, XPMan;
    
    type
      TForm1 = class&#40;TForm&#41;
        Button1&#58; TButton;
        Edit1&#58; TEdit;
        Memo1&#58; TMemo;
        XPManifest1&#58; TXPManifest;
        procedure Button1Click&#40;Sender&#58; TObject&#41;;
      private
        &#123; Private declarations &#125;
      public
        &#123; Public declarations &#125;
      end;
    
    var
      Form1&#58; TForm1;
      V_Remainder&#58; Double;
      V_NOTODIVIDE&#58; Double;
      V_DIVRESULT&#58; Double;
      V_FINAL&#58; string;
    implementation
    
    &#123;$R *.dfm&#125;
    
    procedure TForm1.Button1Click&#40;Sender&#58; TObject&#41;;
    begin
    V_NOTODIVIDE &#58;= StrToInt&#40;Edit1.Text&#41;;
    V_NOTODIVIDE &#58;= V_NOTODIVIDE / 2;
    V_Remainder &#58;= V_NOTODIVIDE mod 2;
    V_FINAL &#58;= V_FINAL + &#40;IntToStr&#40;V_Remainder&#41;&#41;;
    
    Memo1.Lines.Add&#40;V_FINAL&#41;;
    
    
    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?
    Nicholas.
    <br />
    <br />Please join: http://holzstukka.proboards81.com

  7. #7

    Remainder

    You're trying to pass an double to IntToStr. Try not to get integers and floats mixed up.

    Code:
    var
      Form1&#58; TForm1;
      V_Remainder&#58; Integer;
      V_NOTODIVIDE&#58; Integer;
      V_DIVRESULT&#58; Integer;
      V_FINAL&#58; String;
    
    implementation
    
    procedure TForm1.Button1Click&#40;Sender&#58; TObject&#41;;
    begin
      V_NOTODIVIDE &#58;= StrToInt&#40;Edit1.Text&#41;;
      V_NOTODIVIDE &#58;= V_NOTODIVIDE div 2;
      V_Remainder &#58;= V_NOTODIVIDE mod 2;
      V_FINAL &#58;= V_FINAL + IntToStr&#40;V_Remainder&#41;;
      Memo1.Lines.Add&#40;V_FINAL&#41;;
    end;

  8. #8

    Remainder

    div and mod are the same operation for the CPU.

    [pascal]
    asm
    mov eax, 42
    cdq
    mov ecx, 9
    div ecx
    end;
    [/pascal]

    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.

  9. #9

    Remainder

    [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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •