• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[/
2    Boost.Optional
3
4    Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
5
6    Distributed under the Boost Software License, Version 1.0.
7    (See accompanying file LICENSE_1_0.txt or copy at
8    http://www.boost.org/LICENSE_1_0.txt)
9]
10
11[section bounds<> traits class]
12
13[section Introduction]
14
15To determine the ranges of numeric types with `std::numeric_limits` \[18.2.1\],
16different syntax have to be used depending on numeric type. Specifically,
17`numeric_limits<T>::min()` for integral types returns the minimum finite value,
18whereas for floating point types it returns the minimum positive normalized
19value. The difference in semantics makes client code unnecessarily complex
20and error prone.
21
22`boost::numeric::bounds<>` provides a consistent interface for retrieving the
23maximum finite value, the minimum finite value and the minimum positive normalized
24value (0 for integral types) for numeric types. The selection of implementation
25is performed at compile time, so there is no runtime overhead.
26
27[endsect]
28
29[section traits class bounds<N>]
30
31    template<class N>
32    struct bounds
33    {
34        static N lowest  () { return implementation_defined; }
35        static N highest () { return implementation_defined; }
36        static N smallest() { return implementation_defined; }
37    };
38
39[heading Members]
40
41
42[: `lowest()` ]
43
44Returns the minimum finite value, equivalent to `numeric_limits<T>::min()` when `T`
45is an integral type, and to `-numeric_limits<T>::max()` when `T` is a floating point type.
46
47[: `highest()` ]
48
49Returns the maximum finite value, equivalent to `numeric_limits<T>::max()`.
50
51[: `smallest()` ]
52
53Returns the smallest positive normalized value for floating point types with
54denormalization, or returns 0 for integral types.
55
56[endsect]
57
58[section Examples]
59
60The following example demonstrates the use of `numeric::bounds<>` and the
61equivalent code using `numeric_limits`:
62
63    #include <iostream>
64
65    #include <boost/numeric/conversion/bounds.hpp>
66    #include <boost/limits.hpp>
67
68    int main() {
69
70        std::cout << "numeric::bounds versus numeric_limits example.\n";
71
72        std::cout << "The maximum value for float:\n";
73        std::cout << boost::numeric::bounds<float>::highest() << "\n";
74        std::cout << std::numeric_limits<float>::max() << "\n";
75
76        std::cout << "The minimum value for float:\n";
77        std::cout << boost::numeric::bounds<float>::lowest() << "\n";
78        std::cout << -std::numeric_limits<float>::max() << "\n";
79
80        std::cout << "The smallest positive value for float:\n";
81        std::cout << boost::numeric::bounds<float>::smallest() << "\n";
82        std::cout << std::numeric_limits<float>::min() << "\n";
83
84        return 0;
85    }
86
87
88[endsect]
89
90[endsect]
91
92
93
94
95
96
97
98
99
100
101