37 #ifndef VIGRA_NUMERICTRAITS_HXX
38 #define VIGRA_NUMERICTRAITS_HXX
44 #include "metaprogramming.hxx"
45 #include "sized_int.hxx"
476 template <
typename s,
typename t>
477 inline static t clamp_integer_to_unsigned(s value, t maximum) {
481 (value >= static_cast<s>(maximum) ? maximum : static_cast<t>(value));
484 template <
typename s,
typename t>
485 inline static t clamp_integer_to_signed(s value, t minimum, t maximum) {
487 value <= static_cast<s>(minimum) ?
489 (value >= static_cast<s>(maximum) ? maximum :
static_cast<t
>(value));
492 template <
typename s,
typename t>
493 inline static t clamp_float_to_unsigned(s value, t maximum) {
497 (value >= static_cast<s>(maximum) ? maximum : static_cast<t>(value + 0.5));
500 template <
typename s,
typename t>
501 inline static t clamp_float_to_signed(s value, t minimum, t maximum) {
503 return value >=
static_cast<s
>(maximum) ? maximum : static_cast<t>(value + 0.5);
505 return value <= static_cast<s>(minimum) ? minimum : static_cast<t>(value - 0.5);
510 struct Error_NumericTraits_not_specialized_for_this_case { };
511 struct Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char { };
516 typedef Error_NumericTraits_not_specialized_for_this_case Type;
517 typedef Error_NumericTraits_not_specialized_for_this_case Promote;
518 typedef Error_NumericTraits_not_specialized_for_this_case UnsignedPromote;
519 typedef Error_NumericTraits_not_specialized_for_this_case RealPromote;
520 typedef Error_NumericTraits_not_specialized_for_this_case ComplexPromote;
521 typedef Error_NumericTraits_not_specialized_for_this_case ValueType;
523 typedef Error_NumericTraits_not_specialized_for_this_case isScalar;
524 typedef Error_NumericTraits_not_specialized_for_this_case isIntegral;
525 typedef Error_NumericTraits_not_specialized_for_this_case isSigned;
526 typedef Error_NumericTraits_not_specialized_for_this_case isOrdered;
527 typedef Error_NumericTraits_not_specialized_for_this_case isComplex;
531 struct NumericTraits<char>
533 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char Type;
534 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char Promote;
535 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char UnsignedPromote;
536 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char RealPromote;
537 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char ComplexPromote;
538 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char ValueType;
540 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isScalar;
541 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isIntegral;
542 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isSigned;
543 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isOrdered;
544 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isComplex;
549 struct NumericTraits<bool>
553 typedef unsigned int UnsignedPromote;
554 typedef double RealPromote;
555 typedef std::complex<RealPromote> ComplexPromote;
556 typedef Type ValueType;
558 typedef VigraTrueType isIntegral;
559 typedef VigraTrueType isScalar;
560 typedef VigraFalseType isSigned;
561 typedef VigraTrueType isOrdered;
562 typedef VigraFalseType isComplex;
564 static bool zero() {
return false; }
565 static bool one() {
return true; }
566 static bool nonZero() {
return true; }
567 static bool min() {
return false; }
568 static bool max() {
return true; }
570 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
571 enum { minConst = false , maxConst =
true };
573 static const bool minConst =
false;
574 static const bool maxConst =
true;
577 static Promote toPromote(
bool v) {
return v ? 1 : 0; }
578 static RealPromote toRealPromote(
bool v) {
return v ? 1.0 : 0.0; }
579 static bool fromPromote(Promote v) {
580 return (v == 0) ?
false :
true;
582 static bool fromRealPromote(RealPromote v) {
583 return (v == 0.0) ?
false :
true;
589 struct NumericTraits<signed char>
591 typedef signed char Type;
593 typedef unsigned int UnsignedPromote;
594 typedef double RealPromote;
595 typedef std::complex<RealPromote> ComplexPromote;
596 typedef Type ValueType;
598 typedef VigraTrueType isIntegral;
599 typedef VigraTrueType isScalar;
600 typedef VigraTrueType isSigned;
601 typedef VigraTrueType isOrdered;
602 typedef VigraFalseType isComplex;
604 static signed char zero() {
return 0; }
605 static signed char one() {
return 1; }
606 static signed char nonZero() {
return 1; }
607 static signed char min() {
return SCHAR_MIN; }
608 static signed char max() {
return SCHAR_MAX; }
610 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
611 enum { minConst = SCHAR_MIN, maxConst = SCHAR_MIN };
613 static const signed char minConst = SCHAR_MIN;
614 static const signed char maxConst = SCHAR_MIN;
617 static Promote toPromote(
signed char v) {
return v; }
618 static RealPromote toRealPromote(
signed char v) {
return v; }
619 static signed char fromPromote(Promote v) {
620 return detail::clamp_integer_to_signed<Promote, signed char>(v, SCHAR_MIN, SCHAR_MAX);
622 static signed char fromRealPromote(RealPromote v) {
623 return detail::clamp_float_to_signed<RealPromote, signed char>(v, SCHAR_MIN, SCHAR_MAX);
628 struct NumericTraits<unsigned char>
630 typedef unsigned char Type;
632 typedef unsigned int UnsignedPromote;
633 typedef double RealPromote;
634 typedef std::complex<RealPromote> ComplexPromote;
635 typedef Type ValueType;
637 typedef VigraTrueType isIntegral;
638 typedef VigraTrueType isScalar;
639 typedef VigraFalseType isSigned;
640 typedef VigraTrueType isOrdered;
641 typedef VigraFalseType isComplex;
643 static unsigned char zero() {
return 0; }
644 static unsigned char one() {
return 1; }
645 static unsigned char nonZero() {
return 1; }
646 static unsigned char min() {
return 0; }
647 static unsigned char max() {
return UCHAR_MAX; }
649 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
650 enum { minConst = 0, maxConst = UCHAR_MAX };
652 static const unsigned char minConst = 0;
653 static const unsigned char maxConst = UCHAR_MAX;
656 static Promote toPromote(
unsigned char v) {
return v; }
657 static RealPromote toRealPromote(
unsigned char v) {
return v; }
658 static unsigned char fromPromote(Promote v) {
659 return detail::clamp_integer_to_unsigned<Promote, unsigned char>(v, UCHAR_MAX);
661 static unsigned char fromRealPromote(RealPromote v) {
662 return detail::clamp_float_to_unsigned<RealPromote, unsigned char>(v, UCHAR_MAX);
667 struct NumericTraits<short int>
669 typedef short int Type;
671 typedef unsigned int UnsignedPromote;
672 typedef double RealPromote;
673 typedef std::complex<RealPromote> ComplexPromote;
674 typedef Type ValueType;
676 typedef VigraTrueType isIntegral;
677 typedef VigraTrueType isScalar;
678 typedef VigraTrueType isSigned;
679 typedef VigraTrueType isOrdered;
680 typedef VigraFalseType isComplex;
682 static short int zero() {
return 0; }
683 static short int one() {
return 1; }
684 static short int nonZero() {
return 1; }
685 static short int min() {
return SHRT_MIN; }
686 static short int max() {
return SHRT_MAX; }
688 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
689 enum { minConst = SHRT_MIN, maxConst = SHRT_MAX };
691 static const short int minConst = SHRT_MIN;
692 static const short int maxConst = SHRT_MAX;
695 static Promote toPromote(
short int v) {
return v; }
696 static RealPromote toRealPromote(
short int v) {
return v; }
697 static short int fromPromote(Promote v) {
698 return detail::clamp_integer_to_signed<Promote, short int>(v, SHRT_MIN, SHRT_MAX);
700 static short int fromRealPromote(RealPromote v) {
701 return detail::clamp_float_to_signed<RealPromote, short int>(v, SHRT_MIN, SHRT_MAX);
706 struct NumericTraits<short unsigned int>
708 typedef short unsigned int Type;
710 typedef unsigned int UnsignedPromote;
711 typedef double RealPromote;
712 typedef std::complex<RealPromote> ComplexPromote;
713 typedef Type ValueType;
715 typedef VigraTrueType isIntegral;
716 typedef VigraTrueType isScalar;
717 typedef VigraFalseType isSigned;
718 typedef VigraTrueType isOrdered;
719 typedef VigraFalseType isComplex;
721 static short unsigned int zero() {
return 0; }
722 static short unsigned int one() {
return 1; }
723 static short unsigned int nonZero() {
return 1; }
724 static short unsigned int min() {
return 0; }
725 static short unsigned int max() {
return USHRT_MAX; }
727 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
728 enum { minConst = 0, maxConst = USHRT_MAX };
730 static const short unsigned int minConst = 0;
731 static const short unsigned int maxConst = USHRT_MAX;
734 static Promote toPromote(
short unsigned int v) {
return v; }
735 static RealPromote toRealPromote(
short unsigned int v) {
return v; }
736 static short unsigned int fromPromote(Promote v) {
737 return detail::clamp_integer_to_unsigned<Promote, short unsigned int>(v, USHRT_MAX);
739 static short unsigned int fromRealPromote(RealPromote v) {
740 return detail::clamp_float_to_unsigned<RealPromote, short unsigned int>(v, USHRT_MAX);
745 struct NumericTraits<int>
749 typedef unsigned int UnsignedPromote;
750 typedef double RealPromote;
751 typedef std::complex<RealPromote> ComplexPromote;
752 typedef Type ValueType;
754 typedef VigraTrueType isIntegral;
755 typedef VigraTrueType isScalar;
756 typedef VigraTrueType isSigned;
757 typedef VigraTrueType isOrdered;
758 typedef VigraFalseType isComplex;
760 static int zero() {
return 0; }
761 static int one() {
return 1; }
762 static int nonZero() {
return 1; }
763 static int min() {
return INT_MIN; }
764 static int max() {
return INT_MAX; }
766 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
767 enum { minConst = INT_MIN, maxConst = INT_MAX };
769 static const int minConst = INT_MIN;
770 static const int maxConst = INT_MAX;
773 static Promote toPromote(
int v) {
return v; }
774 static RealPromote toRealPromote(
int v) {
return v; }
775 static int fromPromote(Promote v) {
return v; }
776 static int fromRealPromote(RealPromote v) {
777 return detail::clamp_float_to_signed<RealPromote, int>(v, INT_MIN, INT_MAX);
782 struct NumericTraits<unsigned int>
784 typedef unsigned int Type;
785 typedef unsigned int Promote;
786 typedef unsigned int UnsignedPromote;
787 typedef double RealPromote;
788 typedef std::complex<RealPromote> ComplexPromote;
789 typedef Type ValueType;
791 typedef VigraTrueType isIntegral;
792 typedef VigraTrueType isScalar;
793 typedef VigraFalseType isSigned;
794 typedef VigraTrueType isOrdered;
795 typedef VigraFalseType isComplex;
797 static unsigned int zero() {
return 0; }
798 static unsigned int one() {
return 1; }
799 static unsigned int nonZero() {
return 1; }
800 static unsigned int min() {
return 0; }
801 static unsigned int max() {
return UINT_MAX; }
803 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
804 enum { minConst = 0, maxConst = UINT_MAX };
806 static const unsigned int minConst = 0;
807 static const unsigned int maxConst = UINT_MAX;
810 static Promote toPromote(
unsigned int v) {
return v; }
811 static RealPromote toRealPromote(
unsigned int v) {
return v; }
812 static unsigned int fromPromote(Promote v) {
return v; }
813 static unsigned int fromRealPromote(RealPromote v) {
814 return detail::clamp_float_to_unsigned<RealPromote, unsigned int>(v, UINT_MAX);
819 struct NumericTraits<long>
822 typedef long Promote;
823 typedef unsigned long UnsignedPromote;
824 typedef double RealPromote;
825 typedef std::complex<RealPromote> ComplexPromote;
826 typedef Type ValueType;
828 typedef VigraTrueType isIntegral;
829 typedef VigraTrueType isScalar;
830 typedef VigraTrueType isSigned;
831 typedef VigraTrueType isOrdered;
832 typedef VigraFalseType isComplex;
834 static long zero() {
return 0; }
835 static long one() {
return 1; }
836 static long nonZero() {
return 1; }
837 static long min() {
return LONG_MIN; }
838 static long max() {
return LONG_MAX; }
840 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
841 enum { minConst = LONG_MIN, maxConst = LONG_MAX };
843 static const long minConst = LONG_MIN;
844 static const long maxConst = LONG_MAX;
847 static Promote toPromote(
long v) {
return v; }
848 static RealPromote toRealPromote(
long v) {
return static_cast<RealPromote
>(v); }
849 static long fromPromote(Promote v) {
return v; }
850 static long fromRealPromote(RealPromote v) {
851 return detail::clamp_float_to_signed<RealPromote, long>(v, LONG_MIN, LONG_MAX);
856 struct NumericTraits<unsigned long>
858 typedef unsigned long Type;
859 typedef unsigned long Promote;
860 typedef unsigned long UnsignedPromote;
861 typedef double RealPromote;
862 typedef std::complex<RealPromote> ComplexPromote;
863 typedef Type ValueType;
865 typedef VigraTrueType isIntegral;
866 typedef VigraTrueType isScalar;
867 typedef VigraFalseType isSigned;
868 typedef VigraTrueType isOrdered;
869 typedef VigraFalseType isComplex;
871 static unsigned long zero() {
return 0; }
872 static unsigned long one() {
return 1; }
873 static unsigned long nonZero() {
return 1; }
874 static unsigned long min() {
return 0; }
875 static unsigned long max() {
return ULONG_MAX; }
877 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
878 enum { minConst = 0, maxConst = ULONG_MAX };
880 static const unsigned long minConst = 0;
881 static const unsigned long maxConst = ULONG_MAX;
884 static Promote toPromote(
unsigned long v) {
return v; }
885 static RealPromote toRealPromote(
unsigned long v) {
return static_cast<RealPromote
>(v); }
886 static unsigned long fromPromote(Promote v) {
return v; }
887 static unsigned long fromRealPromote(RealPromote v) {
888 return detail::clamp_float_to_unsigned<RealPromote, unsigned long>(v, ULONG_MAX);
894 struct NumericTraits<long long>
896 typedef long long Type;
897 typedef long long Promote;
898 typedef unsigned long long UnsignedPromote;
899 typedef double RealPromote;
900 typedef std::complex<RealPromote> ComplexPromote;
901 typedef Type ValueType;
903 typedef VigraTrueType isIntegral;
904 typedef VigraTrueType isScalar;
905 typedef VigraTrueType isSigned;
906 typedef VigraTrueType isOrdered;
907 typedef VigraFalseType isComplex;
909 static long long zero() {
return 0; }
910 static long long one() {
return 1; }
911 static long long nonZero() {
return 1; }
912 static long long min() {
return LLONG_MIN; }
913 static long long max() {
return LLONG_MAX; }
915 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
916 enum { minConst = LLONG_MIN, maxConst = LLONG_MAX };
918 static const long long minConst = LLONG_MIN;
919 static const long long maxConst = LLONG_MAX;
922 static Promote toPromote(
long long v) {
return v; }
923 static RealPromote toRealPromote(
long long v) {
return (RealPromote)v; }
924 static long long fromPromote(Promote v) {
return v; }
925 static long long fromRealPromote(RealPromote v) {
926 return detail::clamp_float_to_signed<RealPromote, long long>(v, LLONG_MIN, LLONG_MAX);
931 struct NumericTraits<unsigned long long>
933 typedef unsigned long long Type;
934 typedef unsigned long long Promote;
935 typedef unsigned long long UnsignedPromote;
936 typedef double RealPromote;
937 typedef std::complex<RealPromote> ComplexPromote;
938 typedef Type ValueType;
940 typedef VigraTrueType isIntegral;
941 typedef VigraTrueType isScalar;
942 typedef VigraFalseType isSigned;
943 typedef VigraTrueType isOrdered;
944 typedef VigraFalseType isComplex;
946 static unsigned long long zero() {
return 0; }
947 static unsigned long long one() {
return 1; }
948 static unsigned long long nonZero() {
return 1; }
949 static unsigned long long min() {
return 0; }
950 static unsigned long long max() {
return ULLONG_MAX; }
952 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
953 enum { minConst = 0, maxConst = ULLONG_MAX };
955 static const unsigned long long minConst = 0;
956 static const unsigned long long maxConst = ULLONG_MAX;
959 static Promote toPromote(
unsigned long long v) {
return v; }
960 static RealPromote toRealPromote(
unsigned long long v) {
return (RealPromote)v; }
961 static unsigned long long fromPromote(Promote v) {
return v; }
962 static unsigned long long fromRealPromote(RealPromote v) {
963 return detail::clamp_float_to_unsigned<RealPromote, unsigned long long>(v, ULLONG_MAX);
969 struct NumericTraits<float>
972 typedef float Promote;
973 typedef float UnsignedPromote;
974 typedef float RealPromote;
975 typedef std::complex<RealPromote> ComplexPromote;
976 typedef Type ValueType;
978 typedef VigraFalseType isIntegral;
979 typedef VigraTrueType isScalar;
980 typedef VigraTrueType isSigned;
981 typedef VigraTrueType isOrdered;
982 typedef VigraFalseType isComplex;
984 static float zero() {
return 0.0; }
985 static float one() {
return 1.0; }
986 static float nonZero() {
return 1.0; }
987 static float epsilon() {
return FLT_EPSILON; }
988 static float smallestPositive() {
return FLT_MIN; }
989 static float min() {
return -FLT_MAX; }
990 static float max() {
return FLT_MAX; }
992 static Promote toPromote(
float v) {
return v; }
993 static RealPromote toRealPromote(
float v) {
return v; }
994 static float fromPromote(Promote v) {
return v; }
995 static float fromRealPromote(RealPromote v) {
return v; }
999 struct NumericTraits<double>
1001 typedef double Type;
1002 typedef double Promote;
1003 typedef double UnsignedPromote;
1004 typedef double RealPromote;
1005 typedef std::complex<RealPromote> ComplexPromote;
1006 typedef Type ValueType;
1008 typedef VigraFalseType isIntegral;
1009 typedef VigraTrueType isScalar;
1010 typedef VigraTrueType isSigned;
1011 typedef VigraTrueType isOrdered;
1012 typedef VigraFalseType isComplex;
1014 static double zero() {
return 0.0; }
1015 static double one() {
return 1.0; }
1016 static double nonZero() {
return 1.0; }
1017 static double epsilon() {
return DBL_EPSILON; }
1018 static double smallestPositive() {
return DBL_MIN; }
1019 static double min() {
return -DBL_MAX; }
1020 static double max() {
return DBL_MAX; }
1022 static Promote toPromote(
double v) {
return v; }
1023 static RealPromote toRealPromote(
double v) {
return v; }
1024 static double fromPromote(Promote v) {
return v; }
1025 static double fromRealPromote(RealPromote v) {
return v; }
1029 struct NumericTraits<long double>
1031 typedef long double Type;
1032 typedef long double Promote;
1033 typedef long double UnsignedPromote;
1034 typedef long double RealPromote;
1035 typedef std::complex<RealPromote> ComplexPromote;
1036 typedef Type ValueType;
1038 typedef VigraFalseType isIntegral;
1039 typedef VigraTrueType isScalar;
1040 typedef VigraTrueType isSigned;
1041 typedef VigraTrueType isOrdered;
1042 typedef VigraFalseType isComplex;
1044 static long double zero() {
return 0.0; }
1045 static long double one() {
return 1.0; }
1046 static long double nonZero() {
return 1.0; }
1047 static long double epsilon() {
return LDBL_EPSILON; }
1048 static long double smallestPositive() {
return LDBL_MIN; }
1049 static long double min() {
return -LDBL_MAX; }
1050 static long double max() {
return LDBL_MAX; }
1052 static Promote toPromote(
long double v) {
return v; }
1053 static RealPromote toRealPromote(
long double v) {
return v; }
1054 static long double fromPromote(Promote v) {
return v; }
1055 static long double fromRealPromote(RealPromote v) {
return v; }
1058 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
1061 struct NumericTraits<std::complex<T> >
1063 typedef std::complex<T> Type;
1064 typedef std::complex<typename NumericTraits<T>::Promote> Promote;
1065 typedef std::complex<typename NumericTraits<T>::UnsignedPromote> UnsignedPromote;
1066 typedef std::complex<typename NumericTraits<T>::RealPromote> RealPromote;
1067 typedef std::complex<RealPromote> ComplexPromote;
1068 typedef T ValueType;
1070 typedef VigraFalseType isIntegral;
1071 typedef VigraFalseType isScalar;
1072 typedef typename NumericTraits<T>::isSigned isSigned;
1073 typedef VigraFalseType isOrdered;
1074 typedef VigraTrueType isComplex;
1076 static Type zero() {
return Type(0.0); }
1077 static Type one() {
return Type(1.0); }
1078 static Type nonZero() {
return one(); }
1079 static Type epsilon() {
return Type(NumericTraits<T>::epsilon()); }
1080 static Type smallestPositive() {
return Type(NumericTraits<T>::smallestPositive()); }
1082 static Promote toPromote(Type
const & v) {
return v; }
1083 static Type fromPromote(Promote
const & v) {
return v; }
1084 static Type fromRealPromote(RealPromote v) {
return Type(v); }
1087 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
1096 struct SquareRootTraits
1099 typedef typename NumericTraits<T>::RealPromote SquareRootResult;
1100 typedef typename NumericTraits<T>::RealPromote SquareRootArgument;
1110 struct Error_NormTraits_not_specialized_for_this_case { };
1116 typedef Error_NormTraits_not_specialized_for_this_case SquaredNormType;
1117 typedef Error_NormTraits_not_specialized_for_this_case NormType;
1120 #define VIGRA_DEFINE_NORM_TRAITS(T) \
1121 template <> struct NormTraits<T> { \
1123 typedef NumericTraits<T>::Promote SquaredNormType; \
1124 typedef T NormType; \
1127 VIGRA_DEFINE_NORM_TRAITS(
bool)
1128 VIGRA_DEFINE_NORM_TRAITS(
signed char)
1129 VIGRA_DEFINE_NORM_TRAITS(
unsigned char)
1130 VIGRA_DEFINE_NORM_TRAITS(
short)
1131 VIGRA_DEFINE_NORM_TRAITS(
unsigned short)
1132 VIGRA_DEFINE_NORM_TRAITS(
int)
1133 VIGRA_DEFINE_NORM_TRAITS(
unsigned int)
1134 VIGRA_DEFINE_NORM_TRAITS(
long)
1135 VIGRA_DEFINE_NORM_TRAITS(
unsigned long)
1136 VIGRA_DEFINE_NORM_TRAITS(
float)
1137 VIGRA_DEFINE_NORM_TRAITS(
double)
1138 VIGRA_DEFINE_NORM_TRAITS(
long double)
1141 VIGRA_DEFINE_NORM_TRAITS(
long long)
1142 VIGRA_DEFINE_NORM_TRAITS(
unsigned long long)
1145 #undef VIGRA_DEFINE_NORM_TRAITS
1147 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
1150 struct NormTraits<std::complex<T> >
1152 typedef std::complex<T> Type;
1153 typedef typename NormTraits<T>::SquaredNormType SquaredNormType;
1154 typedef typename SquareRootTraits<SquaredNormType>::SquareRootResult NormType;
1157 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
1167 template <
class T,
class U>
1173 typedef typename SizeToType<sizeof(*typeToSize(t() + u()))>::result Promote;
1174 static Promote toPromote(T t) {
return Promote(t); }
1175 static Promote toPromote(U u) {
return Promote(u); }
1180 struct PromoteType<T, T>
1184 typedef typename SizeToType<sizeof(*typeToSize(t() + t()))>::result Promote;
1185 static Promote toPromote(T t) {
return Promote(t); }
1190 struct Error_PromoteTraits_not_specialized_for_this_case { };
1192 template<
class A,
class B>
1193 struct PromoteTraits
1195 typedef Error_PromoteTraits_not_specialized_for_this_case Promote;
1198 #include "promote_traits.hxx"
1200 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
1203 struct PromoteTraits<std::complex<T>, std::complex<T> >
1205 typedef std::complex<typename PromoteTraits<T, T>::Promote> Promote;
1206 static Promote toPromote(std::complex<T>
const & v) {
return v; }
1209 template <
class T1,
class T2>
1210 struct PromoteTraits<std::complex<T1>, std::complex<T2> >
1212 typedef std::complex<typename PromoteTraits<T1, T2>::Promote> Promote;
1213 static Promote toPromote(std::complex<T1>
const & v) {
return v; }
1214 static Promote toPromote(std::complex<T2>
const & v) {
return v; }
1217 template <
class T1,
class T2>
1218 struct PromoteTraits<std::complex<T1>, T2 >
1220 typedef std::complex<typename PromoteTraits<T1, T2>::Promote> Promote;
1221 static Promote toPromote(std::complex<T1>
const & v) {
return v; }
1222 static Promote toPromote(T2
const & v) {
return Promote(v); }
1225 template <
class T1,
class T2>
1226 struct PromoteTraits<T1, std::complex<T2> >
1228 typedef std::complex<typename PromoteTraits<T1, T2>::Promote> Promote;
1229 static Promote toPromote(T1
const & v) {
return Promote(v); }
1230 static Promote toPromote(std::complex<T2>
const & v) {
return v; }
1238 struct RequiresExplicitCast {
1240 static U
const & cast(U
const & v)
1244 #if !defined(_MSC_VER) || _MSC_VER >= 1300
1245 # define VIGRA_SPECIALIZED_CAST(type) \
1247 struct RequiresExplicitCast<type> { \
1248 static type cast(float v) \
1249 { return NumericTraits<type>::fromRealPromote(v); } \
1250 static type cast(double v) \
1251 { return NumericTraits<type>::fromRealPromote(v); } \
1252 static type cast(type v) \
1254 template <class U> \
1255 static type cast(U v) \
1256 { return static_cast<type>(v); } \
1260 # define VIGRA_SPECIALIZED_CAST(type) \
1262 struct RequiresExplicitCast<type> { \
1263 static type cast(float v) \
1264 { return NumericTraits<type>::fromRealPromote(v); } \
1265 static type cast(double v) \
1266 { return NumericTraits<type>::fromRealPromote(v); } \
1267 static type cast(signed char v) \
1269 static type cast(unsigned char v) \
1271 static type cast(short v) \
1273 static type cast(unsigned short v) \
1275 static type cast(int v) \
1277 static type cast(unsigned int v) \
1279 static type cast(long v) \
1281 static type cast(unsigned long v) \
1287 VIGRA_SPECIALIZED_CAST(
signed char)
1288 VIGRA_SPECIALIZED_CAST(
unsigned char)
1289 VIGRA_SPECIALIZED_CAST(
short)
1290 VIGRA_SPECIALIZED_CAST(
unsigned short)
1291 VIGRA_SPECIALIZED_CAST(
int)
1292 VIGRA_SPECIALIZED_CAST(
unsigned int)
1293 VIGRA_SPECIALIZED_CAST(
long)
1294 VIGRA_SPECIALIZED_CAST(
unsigned long)
1297 struct RequiresExplicitCast<
bool> {
1299 static bool cast(U v)
1300 {
return v == NumericTraits<U>::zero()
1306 struct RequiresExplicitCast<float> {
1307 static float cast(
int v)
1308 {
return (
float)v; }
1310 static float cast(
unsigned int v)
1311 {
return (
float)v; }
1313 static float cast(
long v)
1314 {
return (
float)v; }
1316 static float cast(
unsigned long v)
1317 {
return (
float)v; }
1319 static float cast(
long long v)
1320 {
return (
float)v; }
1322 static float cast(
unsigned long long v)
1323 {
return (
float)v; }
1325 static float cast(
double v)
1326 {
return (
float)v; }
1328 static float cast(
long double v)
1329 {
return (
float)v; }
1337 struct RequiresExplicitCast<double> {
1338 static double cast(
Int64 v)
1339 {
return (
double)v; }
1341 static double cast(
UInt64 v)
1342 {
return (
double)v; }
1349 #undef VIGRA_SPECIALIZED_CAST
1357 #endif // VIGRA_NUMERICTRAITS_HXX