Lazarus

Free Pascal => Beginners => Topic started by: hymcode on June 29, 2020, 05:44:03 pm

Title: Math: Incorrect output being returned
Post by: hymcode on June 29, 2020, 05:44:03 pm
Hello, everyone, I'm new to Pascal programming and I'm trying to improve my competency of the language, by creating a calculator program.

The issue I'm having at the moment is when two large numbers are added together the result is incorrect. The calculation I am trying to test is -2147483647 + -2147483647 = -4,294,967,294, but instead the output I get is 2 for some reason.

Code: Pascal  [Select][+][-]
  1. procedure addition;
  2. {local declaration(s)}
  3. var
  4.   val1, val2, sum : Integer;
  5. begin
  6.   {clear previous screen before displaying anything new}
  7.   clrscr;
  8.  
  9.   {initialise variables}
  10.   val1 := 0;
  11.   val2 := 0;
  12.   sum := 0;
  13.  
  14.   writeln('This is the Addition screen');
  15.  
  16.   repeat
  17.     repeat
  18.       writeln('Please enter your first value: ');
  19.       readln(val1);
  20.       if ((val1 < -2147483647) or (val1 > 2147483646)) then
  21.       begin
  22.         error;
  23.       end
  24.     until ((val1 >= -2147483647) and (val1 <= 2147483646));
  25.  
  26.     repeat
  27.       writeln('Please enter your second value: ');
  28.       readln(val2);
  29.       if ((val2 < -2147483647) or (val2 > 2147483646)) then
  30.       begin
  31.         error;
  32.       end;
  33.     until ((val2 >= -2147483647) and (val2 <= 2147483646));
  34.   until (((val1 >= -2147483647) or (val1 <= 2147483646)) and ((val2 >= -2147483647) or (val2 <= 2147483646)));
  35.  
  36.   sum := val1 + val2;
  37.  
  38.   writeln('Answer: ', val1, ' + ', val2, ' = ', sum);
  39.  
  40.   readln;
  41. end;
  42.  

I'm not sure how to resolve this problem so any help / advice would be greatly appreciated.
Title: Re: Math: Incorrect output being returned
Post by: marcov on June 29, 2020, 05:52:34 pm
Turn all overflow checking on.

Note that - 2^32 doesn't fit in a 32-bit integer type.
Title: Re: Math: Incorrect output being returned
Post by: hymcode on June 29, 2020, 06:46:55 pm
Turn all overflow checking on.

Note that - 2^32 doesn't fit in a 32-bit integer type.

Ah yes, silly me, I've just noticed that my 'sum' variable is also declared as an 'Integer' which means it's limited to 32 bit. So this will need to be changed to an 'Int64' data type.

I'm not sure what you mean by 'Turn overflow checking on'. After a quick google, it's something to do with processor directives, I've only briefly read about these, so have no actual experience using them. Based on one example I'd looked at it seem like I just add  {$OVERFLOWCHECKS ON} at the top of my code. Please correct me if im wrong.
Title: Re: Math: Incorrect output being returned
Post by: wp on June 29, 2020, 07:16:36 pm
I'm not sure what you mean by 'Turn overflow checking on'. After a quick google, it's something to do with processor directives, I've only briefly read about these, so have no actual experience using them. Based on one example I'd looked at it seem like I just add  {$OVERFLOWCHECKS ON} at the top of my code. Please correct me if im wrong.
Or:  Go to "Project options" > "Debugging", in "Check and Assertions" check the box "Overflow". This will add checks whether the result of a calculation fits into the range of the data types and creates a runtime error if this happens (in this case: error 215 = "arithmetic overflow"). Run time errors are not nice, but MUCH better than silently presenting a wrong result because it tells you that something is wrong.
Title: Re: Math: Incorrect output being returned
Post by: winni on June 29, 2020, 07:40:48 pm
Hi!


To save typing you can also use

{$Q+}

You don't have to position the overflowCheckOn at the beginning of your code.
If you only want to use it in an special part of the code you can turn it on and off at that position:

Code: Pascal  [Select][+][-]
  1. {$Q+}
  2. Inc(i);
  3. {$Q-}

Winni
Title: Re: Math: Incorrect output being returned
Post by: PascalDragon on June 30, 2020, 09:40:05 am
If you only want to use it in an special part of the code you can turn it on and off at that position:

Code: Pascal  [Select][+][-]
  1. {$Q+}
  2. Inc(i);
  3. {$Q-}

It is safer to use the following:

Code: Pascal  [Select][+][-]
  1. {$push}
  2. {$Q+}
  3. Inc(i);
  4. {$pop}

Otherwise you'll disable overflow checking for the following code if it had been enabled globally.
TinyPortal © 2005-2018