Forum > Operating Systems
Inconsistency between Windows and Linux compiling?
Alex Kidd:
I began experimenting with Lazarus, using Moraz Abdel Azeem's Start programming using Object Pascal.
I stumbled upon this issue compiling this simple program (excerpt):
--- Code: ---var
x,y:integer;
res:double;
begin
x:=5;
y:=0;
try
[color=red] res:=x/y;[/color]
except on e:exception do
writeln('Exception:',e.message); [actually, I'm using a Memo]
end;
writeln('Res:',floattostr(res)); [actually, I'm using a Memo]
end;
--- End code ---
If I compile and run this code in Windows, a DivideByZero exception is raised, which is correct. :D
On the other hand, if I compile and run this code in Linux, no exception is raised: the variable res is set to "+inf" value. :o
Is there a particular compiler flag I have to set in Linux to obtain the correct behaviour?
Please, let me know!
eny:
--- Quote from: Alex Kidd on June 11, 2011, 06:13:26 pm ---If I compile and run this code in Windows, a DivideByZero exception is raised, which is correct. :D
--- End quote ---
What Lazarus and FPC version?
When I do this the program crashes horribly.
(BTW: dividing through zero is not the best of ideas...)
Alex Kidd:
--- Quote from: eny on June 11, 2011, 07:00:51 pm ---
--- Quote from: Alex Kidd on June 11, 2011, 06:13:26 pm ---If I compile and run this code in Windows, a DivideByZero exception is raised, which is correct. :D
--- End quote ---
What Lazarus and FPC version?
When I do this the program crashes horribly.
(BTW: dividing through zero is not the best of ideas...)
--- End quote ---
Lazarus 0.9.30 and FPC 2.4.2.
I agree "divide by zero" is not the best of ideas ;), I was just trying the samples in the book and make a comparison between Linux Mint 10 and Windows XP SP3 to verify the behaviour is exactly the same.
Apparently they differ: WinXP raises the exception, while Linux says the result is +Infinite.
This means in this particular case I cannot write the code once and compile everywhere. I think this is normal when coding using DLLs or OS specific features, but not when using simple math functions.
Zoran:
Alex, what output do you get from this program if exception doesn't get raised?
I have just tried and compiled this example on Linux (Ubuntu 11.04). The exception gets raised as expected.
When I enter zero for y, I receive the message "An error occurred: Division by zero", which means that the execution fell into "except" branch of try-except block.
Further, if I change the writeln to
--- Code: ---Writeln('An error occurred: ', e.ClassName, ' - ', e.message);
--- End code ---
(that is, add e.ClassName to output), I can see that EDivByZero got raised (I get: "An error occurred: EDivByZero - Division by zero").
Blaazen:
Here I also get:
--- Code: ---Res:+Inf
--- End code ---
I think it is correct because "/" is used and "res" is double.
If I replace "/" with "div" project gives SIGFPE - invalid floating point operation.
It is also correct because result of "div" cannot be +Inf and therefore there is no result which can be assigned to "Res".
Navigation
[0] Message Index
[#] Next page