[Lazarus] Large Integer errors

Howard Page-Clark hdpc at talktalk.net
Sat Feb 14 20:19:28 CET 2009


On Sat, 14 Feb 2009 17:56:50 +0000
Dave Coventry <dgcoventry at gmail.com> 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 <hdpc at talktalk.net>:
> > On Sat, 14 Feb 2009 12:30:55 +0200
> > Dave Coventry <dgcoventry at gmail.com> 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 <hdpc at talktalk.net>



More information about the Lazarus mailing list