[Insight-developers] New Floating Point Exception code uncovers nasty bug

Bill Lorensen bill.lorensen at gmail.com
Tue Oct 19 15:55:00 EDT 2010


Kent,

In testing your excellent floating point exception code:
http://review.source.kitware.com/#change,130

I enabled the exceptions:
//Enable Floating Point Exceptions for Testing
ITK_USE_FLOATINGPOINTEXCEPTIONS:BOOL=ON

and ran ctest in Testing/Common

The test itkColorTableTest failed. Admittedly  this is not a
particularly popular class, but it should not have a floating point
exception.

This section of code:

  typename NumericTraits< TPixel >::RealType range =
    NumericTraits< TPixel >::max() - NumericTraits< TPixel >::NonpositiveMin();
  typename NumericTraits< TPixel >::RealType delta = range / ( n - 1 );
  TPixel gray;
  for ( i = 0; i < n; i++ )
    {
    gray = NumericTraits< TPixel >::NonpositiveMin()
           + static_cast< TPixel >( i * delta );
    m_Color[i].Set(gray, gray, gray);
    sprintf( m_ColorName[i], "Gray%.02f", static_cast< float >( gray ) );
    }

is the culprit. For signed short pixels, the (i* delta) will overflow.
    gray = NumericTraits< TPixel >::NonpositiveMin()
           + static_cast< TPixel >( i * delta );

should be:
    gray = static_cast<TPixel>(
      NumericTraits< TPixel >::NonpositiveMin() + ( i * delta ));

Looking at a portion of the output of the test before:
5: Gray ShortColors
5: ColorTable (0x8f23a30)
5:   RTTI typeinfo:   itk::ColorTable<short>
5:   Reference Count: 1
5:   Modified Time: 2
5:   Debug: Off
5:   Observers:
5:     none
5:   m_NumberOfColors = 16
5:   m_Color = 0x8f23a70
5:   m_ColorName[0] = Gray-32768.00
5:   m_ColorName[1] = Gray-28399.00
5:   m_ColorName[2] = Gray-24030.00
5:   m_ColorName[3] = Gray-19661.00
5:   m_ColorName[4] = Gray-15292.00
5:   m_ColorName[5] = Gray-10923.00
5:   m_ColorName[6] = Gray-6554.00
5:   m_ColorName[7] = Gray-2185.00
5:   m_ColorName[8] = Gray0.00
5:   m_ColorName[9] = Gray0.00
5:   m_ColorName[10] = Gray0.00
5:   m_ColorName[11] = Gray0.00
5:   m_ColorName[12] = Gray0.00
5:   m_ColorName[13] = Gray0.00
5:   m_ColorName[14] = Gray0.00
5:   m_ColorName[15] = Gray0.00
5:

and after
5: Gray ShortColors
5: ColorTable (0x9fe5c30)
5:   RTTI typeinfo:   itk::ColorTable<short>
5:   Reference Count: 1
5:   Modified Time: 2
5:   Debug: Off
5:   Observers:
5:     none
5:   m_NumberOfColors = 16
5:   m_Color = 0x9fe5c70
5:   m_ColorName[0] = Gray-32768.00
5:   m_ColorName[1] = Gray-28399.00
5:   m_ColorName[2] = Gray-24030.00
5:   m_ColorName[3] = Gray-19661.00
5:   m_ColorName[4] = Gray-15292.00
5:   m_ColorName[5] = Gray-10923.00
5:   m_ColorName[6] = Gray-6554.00
5:   m_ColorName[7] = Gray-2185.00
5:   m_ColorName[8] = Gray2184.00
5:   m_ColorName[9] = Gray6553.00
5:   m_ColorName[10] = Gray10922.00
5:   m_ColorName[11] = Gray15291.00
5:   m_ColorName[12] = Gray19660.00
5:   m_ColorName[13] = Gray24029.00
5:   m_ColorName[14] = Gray28398.00
5:   m_ColorName[15] = Gray32767.00
5:

I wonder what other floating point evil lurks in itk...

Bill


More information about the Insight-developers mailing list