Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-08

Strange Floating Point Exception problem

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Tom Verhoeff

I have an application (for optimizing decisions in the dice game Yahtzee) that uses Real and runs well with FPC on Mac OS X (PPC), but that fails with a Floating Point Exception (FPE) under Windows (FPC 2.2.0).

What makes things complicated, is that the program has a GUI through Lazarus, and that it is rather biggish, making it difficult to isolate the problem.

The place where it complains is a simple statement

  if x < 0.0 then

where x = -1 exactly. It happens inside a recursive function.

When I have the values of x printed (in bit form: sign, biased exponent, fractional part), this is what I get:

Computing all choices for optimal Yahtzee strategy ... OptExpectedScore(123456TFHSLYC;63-): x = 1 01111111111 0000000000000000000000000000000000000000000000000 OptExpectedScore(_23456TFHSLYC;63-): x = 1 01111111111 0000000000000000000000000000000000000000000000000 OptExpectedScore(__3456TFHSLYC;63-): x = 1 01111111111 0000000000000000000000000000000000000000000000000 OptExpectedScore(___456TFHSLYC;63-): x = 1 01111111111 0000000000000000000000000000000000000000000000000 OptExpectedScore(____56TFHSLYC;63-): x = 1 01111111111 0000000000000000000000000000000000000000000000000 OptExpectedScore(_____6TFHSLYC;63-): x = 1 01111111111 0000000000000000000000000000000000000000000000000 OptExpectedScore(______TFHSLYC;33-): x = 1 01111111111 0000000000000000000000000000000000000000000000000 OptExpectedScore(_______FHSLYC;33-): x = 1 01111111111 0000000000000000000000000000000000000000000000000

This means that the if statement is passed 7 times without problem with x = -1, and on the 8th pass, where x = -1 again, it bails out with an FPE.

How can I get more details about the exception?

What could be causing this?

When I use an ordinary write statement to print the value of x, it also bombs out on the 8th pass.

Note that x is actually a field in a record stored in a high-dimensional array that is dynamically allocated, accessed through a with statement.

Using various levels of optimization does not matter.

I am at a loss here,

Tom

E-MAIL: T.Verhoeff @ TUE.NL | Dept. of Math. & Comp. Science PHONE: +31 40 247 41 25 | Technische Universiteit Eindhoven FAX: +31 40 247 54 04 | PO Box 513, NL-5600 MB Eindhoven http://www.win.tue.nl/~wstomv/ | The Netherlands
Marco van de Voort
to Tom Verhoeff

In our previous episode, Tom Verhoeff said:

This means that the if statement is passed 7 times without problem with x = -1, and on the 8th pass, where x = -1 again, it bails out with an FPE.

Hmm, didn't the x87 copro stack had 7ish registers? And recursive you say? :-)

Marco van de Voort
to Tom Verhoeff: I have an application (for optimizing decisions in the dice game Yahtzee) that uses Real…

In our previous episode, Tom Verhoeff said:

Show quoted text 5 lines
What makes things complicated, is that the program has a GUI through Lazarus, and that it is rather biggish, making it difficult to isolate the problem. The place where it complains is a simple statement

Sorry, forgot to mention a test/remedy if that is it: obviously check the asm code, but play with calling conventions?

Could you post some runnable code?

Vinzent Höfler
to Marco van de Voort: In our previous episode, Tom Verhoeff said: Hmm, didn't the x87 copro stack had 7ish…
Marco van de Voort wrote 6 lines
In our previous episode, Tom Verhoeff said: > This means that the if statement is passed 7 times without problem with > x = -1, and on the 8th pass, where x = -1 again, it bails out with an FPE. Hmm, didn't the x87 copro stack had 7ish registers? And recursive you say? :-)

8-Register-Stack, yes.

Interestingly, I just wanted to give an example and tried the old infamous recursive Fibonacci, which on an older compiler failed for N >= 15 with a FP stack overflow (apart from being darn slow, of course).

Now it seems, Florian's Tail-Recursion-Optimization invalidated this particular example of "don't do this". :D

Vinzent.

Tom Verhoeff
to Tom Verhoeff: I have an application (for optimizing decisions in the dice game Yahtzee) that uses Real…
Tom Verhoeff wrote
I have an application (for optimizing decisions in the dice game Yahtzee) that uses Real and runs well with FPC on Mac OS X (PPC), but that fails with a Floating Point Exception (FPE) under Windows (FPC 2.2.0).

I "played" around some more to resolve this problem.

It now _seems_ to work, after suppressing all FPU exceptions through SetExceptionMask in unit Math.

This is hardly satisfactory, since the place where the program got interrupted (viz. if x < 0) cannot cause an exception, as far as I can tell, since x is confirmed to equal -1 at that point.

So, the program now runs (but might misbehave).

I'll keep you posted.

Tom

E-MAIL: T.Verhoeff @ TUE.NL | Dept. of Math. & Comp. Science PHONE: +31 40 247 41 25 | Technische Universiteit Eindhoven FAX: +31 40 247 54 04 | PO Box 513, NL-5600 MB Eindhoven http://www.win.tue.nl/~wstomv/ | The Netherlands
Alan Krause
to Tom Verhoeff

Tom,

I have some experience with FPEs under Delphi, and what I came to learn is that most of the time, the exception occurs somewhere before what is reported, due to "Imprecise Trapping Modes". In short, "this means the hardware can detect a floating-point exception and deliver an interrupt, but processing may continue while the signal is delivered. As a result, the instruction address register (IAR) is at a different instruction when the interrupt is delivered." (taken from
http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.genprogc/doc/genprogc/floating-point_except.htm).

They can be extremely vexing to solve - good luck!

Alan

Tom Verhoeff wrote 19 lines
On Sat, Aug 23, 2008 at 12:20:48AM +0200, Tom Verhoeff wrote: > I have an application (for optimizing decisions in the dice game Yahtzee) > that uses Real and runs well with FPC on Mac OS X (PPC), but that > fails with a Floating Point Exception (FPE) under Windows (FPC 2.2.0). I "played" around some more to resolve this problem. It now _seems_ to work, after suppressing all FPU exceptions through SetExceptionMask in unit Math. This is hardly satisfactory, since the place where the program got interrupted (viz. if x < 0) cannot cause an exception, as far as I can tell, since x is confirmed to equal -1 at that point. So, the program now runs (but might misbehave). I'll keep you posted. Tom