Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

bounds<> traits class  bounds<> traits 類

Introduction 簡介
traits class bounds<N>  traits 類 bounds<N>
Examples 例子

To determine the ranges of numeric types with std::numeric_limits [18.2.1], different syntax have to be used depending on numeric type. Specifically, numeric_limits<T>::min() for integral types returns the minimum finite value, whereas for floating point types it returns the minimum positive normalized value. The difference in semantics makes client code unnecessarily complex and error prone.
std::numeric_limits [18.2.1] 來判斷數字類型的範圍時,必須根據數字類型使用不同的語法。特別地,對於整型類型,numeric_limits<T>::min() 返回最小限值,而對於浮點類型則返回正的最小正規值。這種語義上的差異使得用戶代碼變得複雜和易錯。

boost::numeric::bounds<> provides a consistent interface for retrieving the maximum finite value, the minimum finite value and the minimum positive normalized value (0 for integral types) for numeric types. The selection of implementation is performed at compile time, so there is no runtime overhead.
boost::numeric::bounds<> 提供了一致的接口來獲得數字類型的最大限值、最小限值和正的最小正規值(對於整型類型為0)。具體實現的選擇是在編譯期執行,所以沒有運行期的開銷。

template<class N>
struct bounds
{
    static N lowest  () { return implementation_defined; }
    static N highest () { return implementation_defined; }
    static N smallest() { return implementation_defined; }
};
Members 成員

lowest()

Returns the minimum finite value, equivalent to numeric_limits<T>::min() when T is an integral type, and to -numeric_limits<T>::max() when T is a floating point type.
返回最小限值,如果 T 是一個整型類型則等同於 numeric_limits<T>::min(),如果 T 是一個浮點類型則等同於 -numeric_limits<T>::max()

highest()

Returns the maximum finite value, equivalent to numeric_limits<T>::max().
返回最大限值,等同於 numeric_limits<T>::max().

smallest()

Returns the smallest positive normalized value for floating point types with denormalization, or returns 0 for integral types.
對於反規格化的浮點類型,返回正的最小正規值,對於整型類型返回0。

The following example demonstrates the use of numeric::bounds<> and the equivalent code using numeric_limits:
以下例子示範了 numeric::bounds<> 的使用,以及使用 numeric_limits 的等效代碼:

#include <iostream>

#include <boost/numeric/conversion/bounds.hpp>
#include <boost/limits.hpp>

int main() {

    std::cout << "numeric::bounds versus numeric_limits example.\n";

    std::cout << "The maximum value for float:\n";
    std::cout << boost::numeric::bounds<float>::highest() << "\n";
    std::cout << std::numeric_limits<float>::max() << "\n";

    std::cout << "The minimum value for float:\n";
    std::cout << boost::numeric::bounds<float>::lowest() << "\n";
    std::cout << -std::numeric_limits<float>::max() << "\n";

    std::cout << "The smallest positive value for float:\n";
    std::cout << boost::numeric::bounds<float>::smallest() << "\n";
    std::cout << std::numeric_limits<float>::min() << "\n";

    return 0;
}

PrevUpHomeNext