This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Problems with <limit> and nans


There are problems with the <limits> header and nans. Running the executable
generated by compiling the file tl.C by gcc 3.1 extracted from the file
lib/config/limits_test.cpp from boost version 1.21.2 shows that the
implementation of the <limits> header file is not standard conforming
(there is also a comment in the header stressing this fact). Are there
any plans of adding this functionality in the near future? 

STLport 4.1b6 provides a conforming implementation.  

Source file tl.C 

/* boost limits_test.cpp   test your <limits> file for important
 *
 * Copyright Jens Maurer 2000
 * Permission to use, copy, modify, sell, and distribute this software
 * is hereby granted without free provided that the above copyright notice
 * appears in all copies and that both that copyright notice and this
 * permission notice appear in supporting documentation,
 *
 * Jens Maurer makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 */
#include <iostream>
#include <limits>

int test_tools_errors;


#define BOOST_TEST(exp) ((exp) ? static_cast<void>(0) : report_error(#exp,__FILE__,__LINE__))

void report_error( const char * msg, const char * file, int line )
  {
    ++test_tools_errors;
    std::cout << "\n**** test failed: " << msg << ", file: " << file
              << ", line: " << line << std::endl;
  }

template <class T>
void print_hex_val(T t, const char* name)
{
  const unsigned char* p = (const unsigned char*)&t;
  std::cout << "hex value of " << name << " is: ";
  for (unsigned int i = 0; i < sizeof(T); ++i) {
    if(p[i] <= 0xF)
      std::cout << "0";
    std::cout << std::hex << (int)p[i];
  }
  std::cout << std::dec << std::endl;
}

template<class T>
void test_float_limits(const T &, const char * msg)
{
  std::cout << "\nTesting " << msg << std::endl;
  typedef std::numeric_limits<T> lim;

  BOOST_TEST(lim::is_specialized);
  BOOST_TEST(!lim::is_modulo);
  BOOST_TEST(!lim::is_integer);
  BOOST_TEST(lim::is_signed);
  
  BOOST_TEST(lim::epsilon() > 0);

  BOOST_TEST(lim::has_infinity);
  BOOST_TEST(lim::has_quiet_NaN);
  BOOST_TEST(lim::has_signaling_NaN);
  
  const T infinity = lim::infinity();
  const T qnan = lim::quiet_NaN();
  const T snan = lim::signaling_NaN();

  // make sure those values are not 0 or similar nonsense

  std::cout << "IEEE-compatible: " << lim::is_iec559
	    << ", traps: " << lim::traps
	    << ", bounded: " << lim::is_bounded
	    << ", exact: " << lim::is_exact << '\n'
	    << "min: " << lim::min() << ", max: " << lim::max() << '\n'
	    << "infinity: " << infinity << ", QNaN: " << qnan << '\n';
  print_hex_val(lim::max(), "max");
  print_hex_val(infinity, "infinity");
  print_hex_val(qnan, "qnan");
  print_hex_val(snan, "snan");

  // infinity is beyond the representable range
  BOOST_TEST(lim::max() > 1000);
  BOOST_TEST(lim::infinity() > lim::max());
  BOOST_TEST(-lim::infinity() < -lim::max());
  BOOST_TEST(lim::min() < 0.001);
  BOOST_TEST(lim::min() > 0);

  // NaNs shall always compare "false" when compared for equality
  // If one of these fail, your compiler may be optimizing incorrectly
  BOOST_TEST(! (qnan == 42));
  BOOST_TEST(! (qnan == qnan));
  BOOST_TEST(qnan != 42);
  BOOST_TEST(qnan != qnan);
}


int main()
{
  test_float_limits(double(), "double");
  std::cout << test_tools_errors << " errors detected." << std::endl;
}



libstdc++
Testing double

**** test failed: lim::has_infinity, file: tl.C, line: 54

**** test failed: lim::has_quiet_NaN, file: tl.C, line: 55

**** test failed: lim::has_signaling_NaN, file: tl.C, line: 56
IEEE-compatible: 0, traps: 0, bounded: 1, exact: 0
min: 2.22507e-308, max: 1.79769e+308
infinity: 0, QNaN: 0
hex value of max is: ffffffffffffef7f
hex value of infinity is: 0000000000000000
hex value of qnan is: 0000000000000000
hex value of snan is: 0000000000000000

**** test failed: lim::infinity() > lim::max(), file: tl.C, line: 77

**** test failed: -lim::infinity() < -lim::max(), file: tl.C, line: 78

**** test failed: ! (qnan == qnan), file: tl.C, line: 85

**** test failed: qnan != qnan, file: tl.C, line: 87
7 errors detected.

STLport-4.1b6
Testing double
IEEE-compatible: 1, traps: 1, bounded: 1, exact: 0
min: 2.22507e-308, max: 1.79769e+308
infinity: inf, QNaN: nan
hex value of max is: ffffffffffffef7f
hex value of infinity is: 000000000000f07f
hex value of qnan is: 000000000000fc7f
hex value of snan is: 000000000000f87f
0 errors detected.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]