Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-02

Large Integer errors

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dave Coventry

Hi,

I'm using the following code:

function mycalc( num1,num2: real):integer;
begin
  Result:=trunc(num1*num2);
end;

How can I protect from the product of the two numbers being too big?

If it's too big I want to discard it anyway.

Reply by email

Howard Page-Clark
to Dave Coventry
Show quoted text 8 lines
function mycalc( num1,num2: real):integer; begin Result:=trunc(num1*num2); end; How can I protect from the product of the two numbers being too big? If it's too big I want to discard it anyway.
function mycalc( num1,num2: real):integer;
 begin
   Result:=trunc(num1*num2);
  if result > maxint then result := 0;
 end;

Would this do what you want?

Howard Page-Clark

Reply by email

Dave Coventry
to Howard Page-Clark

Yes, great, thanks.

Would there be any error or would Lazarus just continue as if nothing had happened?

2009/2/14 Howard Page-Clark :

Show quoted text 27 lines
On Sat, 14 Feb 2009 12:30:55 +0200 Dave Coventry wrote: > function mycalc( num1,num2: real):integer; > begin > Result:=trunc(num1*num2); > end; > > How can I protect from the product of the two numbers being too big? > > If it's too big I want to discard it anyway. function mycalc( num1,num2: real):integer; begin Result:=trunc(num1*num2); if result > maxint then result := 0; end; Would this do what you want? Howard Page-Clark _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Howard Page-Clark
to Dave Coventry
Show quoted text 27 lines
Yes, great, thanks. Would there be any error or would Lazarus just continue as if nothing had happened? 2009/2/14 Howard Page-Clark : > On Sat, 14 Feb 2009 12:30:55 +0200 > Dave Coventry wrote: > >> function mycalc( num1,num2: real):integer; >> begin >> Result:=trunc(num1*num2); >> end; >> >> How can I protect from the product of the two numbers being too >> big? >> >> If it's too big I want to discard it anyway. > > function mycalc( num1,num2: real):integer; > begin > Result:=trunc(num1*num2); > if result > maxint then result := 0; > end; > > Would this do what you want?

Actually the code I fired off is useless - since result is an integer it is never going to be more than maxint! What you need is

function mycalc( num1,num2: real):integer;
var tmp: int64;
 begin
   tmp :=trunc(num1*num2);
   if (tmp <= maxint) then
    result := tmp
  else result := 0;
 end;

Errors will only be flagged up if you compile with -Co or tick the appropriate overflow check box in the compiler options dialog, Code tab, "Checks" panel.

----
Howard Page-Clark

Reply by email

Mehmet Erol Sanliturk
to Dave Coventry: Yes, great, thanks. Would there be any error or would Lazarus just continue as if nothing…
pm Dave Coventry wrote 37 lines
Yes, great, thanks. Would there be any error or would Lazarus just continue as if nothing had happened? 2009/2/14 Howard Page-Clark : > On Sat, 14 Feb 2009 12:30:55 +0200 > > Dave Coventry wrote: >> function mycalc( num1,num2: real):integer; >> begin >> Result:=trunc(num1*num2); >> end; >> >> How can I protect from the product of the two numbers being too big? >> >> If it's too big I want to discard it anyway. > > function mycalc( num1,num2: real):integer; > begin > Result:=trunc(num1*num2); > if result > maxint then result := 0; > end; > > Would this do what you want? > > > Howard Page-Clark > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

My application is to use first REAL arithmetic :

Var V : Extended  ;
 or
Var V : Real ;
or
Var V : Double ;

...

  V := num1 ;
  V := V * num2 ;

If ( V < Your_Maximum_Allowed_Value )
Then Result := trunc ( num1 * num2 )
Else Message ... about overflow ...

If you compare run1 * run2 directly maxint ,
multiplication may give overflow when result of multiplication is theoretically large than maxint .

Thank you very much .

Mehmet Erol Sanliturk

Reply by email

Dave Coventry
to Howard Page-Clark: On Sat, 14 Feb 2009 17:56:50 +0000 Actually the code I fired off is useless - since…

Thanks Howard.

If the result is never going to be more than maxint anyway, then I don't have to do anything at all, that will be perfectly acceptable to me as it would already be beyond what is usable.

What would screw me up would be if I just lose the topmost bit and it all resets to zero again.

2009/2/14 Howard Page-Clark :

Show quoted text 54 lines
On Sat, 14 Feb 2009 17:56:50 +0000 Dave Coventry wrote: > Yes, great, thanks. > > Would there be any error or would Lazarus just continue as if nothing > had happened? > > > 2009/2/14 Howard Page-Clark : > > On Sat, 14 Feb 2009 12:30:55 +0200 > > Dave Coventry wrote: > > > >> function mycalc( num1,num2: real):integer; > >> begin > >> Result:=trunc(num1*num2); > >> end; > >> > >> How can I protect from the product of the two numbers being too > >> big? > >> > >> If it's too big I want to discard it anyway. > > > > function mycalc( num1,num2: real):integer; > > begin > > Result:=trunc(num1*num2); > > if result > maxint then result := 0; > > end; > > > > Would this do what you want? Actually the code I fired off is useless - since result is an integer it is never going to be more than maxint! What you need is function mycalc( num1,num2: real):integer; var tmp: int64; begin tmp :=trunc(num1*num2); if (tmp <= maxint) then result := tmp else result := 0; end; Errors will only be flagged up if you compile with -Co or tick the appropriate overflow check box in the compiler options dialog, Code tab, "Checks" panel. ---- Howard Page-Clark _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email