Recent

Author Topic: Math: Incorrect output being returned  (Read 564 times)

hymcode

  • New Member
  • *
  • Posts: 13
Math: Incorrect output being returned
« 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.
« Last Edit: August 17, 2020, 05:39:56 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8729
  • FPC developer.
Re: Math: Incorrect output being returned
« Reply #1 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.

hymcode

  • New Member
  • *
  • Posts: 13
Re: Math: Incorrect output being returned
« Reply #2 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.
Don’t wish it were easier; wish you were better. – Jim Rohn

wp

  • Hero Member
  • *****
  • Posts: 7548
Re: Math: Incorrect output being returned
« Reply #3 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.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

winni

  • Hero Member
  • *****
  • Posts: 1806
Re: Math: Incorrect output being returned
« Reply #4 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

PascalDragon

  • Hero Member
  • *****
  • Posts: 2135
  • Compiler Developer
Re: Math: Incorrect output being returned
« Reply #5 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