limits.hpp

来自「support vector clustering for vc++」· HPP 代码 · 共 450 行 · 第 1/2 页

HPP
450
字号
// Copyright 2001 John Maddock
// Distributed under the Boost Software License, Version 1.0. (See accompany-
// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

/*
 * Copyright (c) 1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */

/* NOTE: This is not portable code.  Parts of numeric_limits<> are
 * inherently machine-dependent, and this file is written for the MIPS
 * architecture and the SGI MIPSpro C++ compiler.  Parts of it (in
 * particular, some of the characteristics of floating-point types)
 * are almost certainly incorrect for any other platform.
 */

/* The above comment is almost certainly out of date. This file works
 * on systems other than SGI MIPSpro C++ now.
 */

/*
 * Revision history:
 * 21 Sep 2001:
 *    Only include <cwchar> if BOOST_NO_CWCHAR is defined. (Darin Adler)
 * 10 Aug 2001:
 *    Added MIPS (big endian) to the big endian family. (Jens Maurer)
 * 13 Apr 2001:
 *    Added powerpc to the big endian family. (Jeremy Siek)
 * 5 Apr 2001:
 *    Added sparc (big endian) processor support (John Maddock).
 * Initial sub:
 *      Modified by Jens Maurer for gcc 2.95 on x86.
 */

#ifndef BOOST_SGI_CPP_LIMITS
#define BOOST_SGI_CPP_LIMITS

#include <climits>
#include <cfloat>
#include <boost/config.hpp>
#include <boost/detail/endian.hpp>

#ifndef BOOST_NO_CWCHAR
#include <cwchar> // for WCHAR_MIN and WCHAR_MAX
#endif

namespace std {

enum float_round_style {
  round_indeterminate       = -1,
  round_toward_zero         =  0,
  round_to_nearest          =  1,
  round_toward_infinity     =  2,
  round_toward_neg_infinity =  3
};

enum float_denorm_style {
  denorm_indeterminate = -1,
  denorm_absent        =  0,
  denorm_present       =  1
};

// The C++ standard (section 18.2.1) requires that some of the members of
// numeric_limits be static const data members that are given constant-
// initializers within the class declaration.  On compilers where the
// BOOST_NO_INCLASS_MEMBER_INITIALIZATION macro is defined, it is impossible to write
// a standard-conforming numeric_limits class.
//
// There are two possible workarounds: either initialize the data
// members outside the class, or change them from data members to
// enums.  Neither workaround is satisfactory: the former makes it
// impossible to use the data members in constant-expressions, and the
// latter means they have the wrong type and that it is impossible to
// take their addresses.  We choose the former workaround.

#ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
# define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
  enum { __mem_name = __mem_value }
#else /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */
# define BOOST_STL_DECLARE_LIMITS_MEMBER(__mem_type, __mem_name, __mem_value) \
  static const __mem_type __mem_name = __mem_value
#endif /* BOOST_NO_INCLASS_MEMBER_INITIALIZATION */

// Base class for all specializations of numeric_limits.
template <class __number>
class _Numeric_limits_base {
public:
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, false);

  static __number min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }
  static __number max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __number(); }

  BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   0);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, 0);

  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  false);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, false);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   false);

  BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 0);

  static __number epsilon() throw()     { return __number(); }
  static __number round_error() throw() { return __number(); }

  BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   0);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, 0);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   0);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, 0);

  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      false);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_quiet_NaN,     false);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_signaling_NaN, false);
  BOOST_STL_DECLARE_LIMITS_MEMBER(float_denorm_style,
                              has_denorm,
                              denorm_absent);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_denorm_loss,   false);

  static __number infinity() throw()      { return __number(); }
  static __number quiet_NaN() throw()     { return __number(); }
  static __number signaling_NaN() throw() { return __number(); }
  static __number denorm_min() throw()    { return __number(); }

  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_iec559,  false);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, false);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo,  false);

  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, traps,            false);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, tinyness_before,  false);
  BOOST_STL_DECLARE_LIMITS_MEMBER(float_round_style,
                              round_style,
                              round_toward_zero);
};

// Base class for integers.

template <class _Int,
          _Int __imin,
          _Int __imax,
          int __idigits = -1>
class _Integer_limits : public _Numeric_limits_base<_Int> 
{
public:
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);

  static _Int min BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imin; }
  static _Int max BOOST_PREVENT_MACRO_SUBSTITUTION () throw() { return __imax; }

  BOOST_STL_DECLARE_LIMITS_MEMBER(int,
                              digits,
                              (__idigits < 0) ? (int)(sizeof(_Int) * CHAR_BIT)
                                                   - (__imin == 0 ? 0 : 1) 
                                              : __idigits);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, (digits * 301) / 1000); 
                                // log 2 = 0.301029995664...

  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed,  __imin != 0);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_integer, true);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_exact,   true);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int,  radix,      2);

  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_bounded, true);
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_modulo, true);
};

#if defined(BOOST_BIG_ENDIAN)

 template<class Number, unsigned int Word>
 struct float_helper{
  static Number get_word() throw() {
    // sizeof(long double) == 16
    const unsigned int _S_word[4] = { Word, 0, 0, 0 };
    return *reinterpret_cast<const Number*>(&_S_word);
  } 
};

#else

 template<class Number, unsigned int Word>
 struct float_helper{
  static Number get_word() throw() {
    // sizeof(long double) == 12, but only 10 bytes significant
    const unsigned int _S_word[4] = { 0, 0, 0, Word };
    return *reinterpret_cast<const Number*>(
        reinterpret_cast<const char *>(&_S_word)+16-
                (sizeof(Number) == 12 ? 10 : sizeof(Number)));
  } 
};

#endif

// Base class for floating-point numbers.
template <class __number,
         int __Digits, int __Digits10,
         int __MinExp, int __MaxExp,
         int __MinExp10, int __MaxExp10,
         unsigned int __InfinityWord,
         unsigned int __QNaNWord, unsigned int __SNaNWord,
         bool __IsIEC559,
         float_round_style __RoundStyle>
class _Floating_limits : public _Numeric_limits_base<__number>
{
public:
  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_specialized, true);

  BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits,   __Digits);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int, digits10, __Digits10);

  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, is_signed, true);

  BOOST_STL_DECLARE_LIMITS_MEMBER(int, radix, 2);

  BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent,   __MinExp);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent,   __MaxExp);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int, min_exponent10, __MinExp10);
  BOOST_STL_DECLARE_LIMITS_MEMBER(int, max_exponent10, __MaxExp10);

  BOOST_STL_DECLARE_LIMITS_MEMBER(bool, has_infinity,      true);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?