[cctbxbb] Trapping NaN or not trapping NaN

Luc Bourhis luc_j_bourhis at mac.com
Mon Apr 11 01:24:54 PDT 2016

> On 11 Apr 2016, at 10:11, Graeme.Winter at diamond.ac.uk wrote:
> Will also help with graphics programs etc too! Often get crashes in matplotlib from this...

This is indeed another very good point. We’ve had issues with wxPython as well and I remember having to sandwich some call calling into wxPython with an enabling/disabling of traps. Note that I did write a long time ago a nice utility which can be used both in C++ and Python:

In Python, it uses a context:

      >>> import boost.python
      >>> from scitbx.array_family import flex
      >>> a = flex.double((0, 0, 0))
      >>> with boost.python.trapping(division_by_zero=False):
      >>>   b = 1/a
      >>> tuple(b)
      (inf, inf, inf)
      >>> 1/a
      ... CRASH ...

In C++, it uses the guard pattern:

#include <boost_adaptbx/floating_point_exception.h>

	exception_trapping guard(exception_trapping::division_by_zero | exception_trapping::invalid);
	// the computations in this scope will now trap division-by-zero and NaN
	// when leaving the scope, trapping will go back to what it was before entering the scope

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://phenix-online.org/pipermail/cctbxbb/attachments/20160411/f01de4f6/attachment.htm>

More information about the cctbxbb mailing list