Jim Leonard
2012-10-30 15:56:34 UTC
When I write and debug programs, it is always with range/stack/arith
checking on. In my current project, I am seeing Arith overflow {$Q+}
errors when trying to do legal operations with a longint variable as
the destination. Observe the following code:
===begin===
{$Q+,R+,S+}
const
val1=$0123; val2=$4567;
var
l:longint;
w1,w2:word;
begin
w1:=val1; w2:=val2;
l:=w1 * w2; {Throws runtime 215 "arithmetic overflow" if $Q+}
if l<>(val1*val2) then writeln('Error in longint math routines!');
end.
====end====
With $Q+, the second main line throws an overflow error even though
the source variables and values cannot possibly overflow the target.
With $Q-, the third line throws "error in longint math routines" as
the end result is wrong.
What is going on? Is there a bug in the longint math routines, or a
bug in code generation, or something else? If you run the above code
with your copy of TP7, do you see the same behavior?
(The workaround is to do everything "longhand" like this:
l:=w1;
l:=l * w2;
This works, but my code is starting to look ugly.)
checking on. In my current project, I am seeing Arith overflow {$Q+}
errors when trying to do legal operations with a longint variable as
the destination. Observe the following code:
===begin===
{$Q+,R+,S+}
const
val1=$0123; val2=$4567;
var
l:longint;
w1,w2:word;
begin
w1:=val1; w2:=val2;
l:=w1 * w2; {Throws runtime 215 "arithmetic overflow" if $Q+}
if l<>(val1*val2) then writeln('Error in longint math routines!');
end.
====end====
With $Q+, the second main line throws an overflow error even though
the source variables and values cannot possibly overflow the target.
With $Q-, the third line throws "error in longint math routines" as
the end result is wrong.
What is going on? Is there a bug in the longint math routines, or a
bug in code generation, or something else? If you run the above code
with your copy of TP7, do you see the same behavior?
(The workaround is to do everything "longhand" like this:
l:=w1;
l:=l * w2;
This works, but my code is starting to look ugly.)