Get Type Basic Information#
Synopsis#
Get some basic information about a plain old data (POD) type, in this case the float type.
Results#
Output:
Min: 1.17549e-38
Max: 3.40282e+38
Zero: 0
ZeroValue: 0
Is -1 negative? 1
Is 1 negative? 0
One: 1
Epsilon: 1.19209e-07
Infinity: inf
Good
Code#
Python#
#!/usr/bin/env python
import itk
MyType = itk.F
print("Min: " + str(itk.NumericTraits[MyType].min()))
print("Max: " + str(itk.NumericTraits[MyType].max()))
print("ZeroValue: " + str(itk.NumericTraits[MyType].ZeroValue()))
print("Is -1 negative? " + str(itk.NumericTraits[MyType].IsNegative(-1)))
print("Is 1 negative? " + str(itk.NumericTraits[MyType].IsNegative(1)))
print("OneValue: " + str(itk.NumericTraits[MyType].OneValue()))
print("Epsilon: " + str(itk.NumericTraits[MyType].epsilon()))
print("Infinity: " + str(itk.NumericTraits[MyType].infinity()))
if 0 == itk.NumericTraits[MyType].infinity():
print(" 0 == inf!")
exit(1)
else:
print("Good")
exit(0)
C++#
#include <iostream>
#include "itkNumericTraits.h"
int
main()
{
using MyType = float;
std::cout << "Min: " << itk::NumericTraits<MyType>::min() << std::endl;
std::cout << "Max: " << itk::NumericTraits<MyType>::max() << std::endl;
std::cout << "Zero: " << itk::NumericTraits<MyType>::Zero << std::endl;
std::cout << "ZeroValue: " << itk::NumericTraits<MyType>::ZeroValue() << std::endl;
std::cout << "Is -1 negative? " << itk::NumericTraits<MyType>::IsNegative(-1) << std::endl;
std::cout << "Is 1 negative? " << itk::NumericTraits<MyType>::IsNegative(1) << std::endl;
std::cout << "One: " << itk::NumericTraits<MyType>::One << std::endl;
std::cout << "Epsilon: " << itk::NumericTraits<MyType>::epsilon() << std::endl;
std::cout << "Infinity: " << itk::NumericTraits<MyType>::infinity() << std::endl;
if (0 == itk::NumericTraits<MyType>::infinity())
{
std::cout << " 0 == inf!" << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout << "Good" << std::endl;
return EXIT_SUCCESS;
}
}
Classes demonstrated#
-
template<typename T>
class NumericTraits : public std::numeric_limits<T> Define additional traits for native types such as int or float.
Define numeric traits for std::vector.
NumericTraits is used to extend the traits associated with native types such as float, char, int, and so on. These traits are extensions of the standard numeric_limits defined by the C++ compilers. Some of the added traits include minimum and maximum value; accumulation type; etc.
We provide here a generic implementation based on creating types of std::vector whose components are the types of the
NumericTraits from the original std::vector components. This implementation require support for partial specializations, since it is based on the concept that: NumericTraits<std::vector< T > > is defined piecewise by std::vector< NumericTraits< T > >- ITK Sphinx Examples:
- Template Parameters
T
: Component type of std::vector
- Note
The Zero(), One(), min() and max() methods here take references to a pixel as input. This is due to the fact that the length of the std::vector is not known until run-time. Since the most common use of Zero and One is for comparison purposes or initialization of sums etc, this might just as easily be re-written with a pixel passed in as a reference and the length is inferred from this pixel.
- See
NumericTraits