@@ -688,7 +688,32 @@ diskio::prepare_stream(std::ostream& f)
688
688
689
689
return cell_width;
690
690
}
691
+
692
+
693
+
694
+ template <typename eT>
695
+ inline
696
+ constexpr
697
+ eT
698
+ diskio::real_as_int_lower_limit ()
699
+ {
700
+ constexpr eT eT_int_accuracy_lower_limit = -( (is_fp16<eT>::value) ? eT (0x800 ) : ( (is_float<eT>::value) ? eT (0x1000000 ) : eT (0x20000000000000 ) ) );
691
701
702
+ return (std::max)( eT (std::numeric_limits<int >::lowest ()), eT_int_accuracy_lower_limit );
703
+ }
704
+
705
+
706
+
707
+ template <typename eT>
708
+ inline
709
+ constexpr
710
+ eT
711
+ diskio::real_as_int_upper_limit ()
712
+ {
713
+ constexpr eT eT_int_accuracy_upper_limit = (is_fp16<eT>::value) ? eT (0x800 ) : ( (is_float<eT>::value) ? eT (0x1000000 ) : eT (0x20000000000000 ) );
714
+
715
+ return (std::min)( eT (std::numeric_limits<int >::max ()), eT_int_accuracy_upper_limit );
716
+ }
692
717
693
718
694
719
@@ -935,16 +960,16 @@ diskio::save_csv_ascii(const Mat<eT>& x, std::ostream& f, const char separator)
935
960
uword x_n_rows = x.n_rows ;
936
961
uword x_n_cols = x.n_cols ;
937
962
938
- const eT eT_int_lowest = eT (std::numeric_limits< int >:: lowest () );
939
- const eT eT_int_max = eT (std::numeric_limits< int >:: max () );
963
+ constexpr eT eT_int_lower = diskio::real_as_int_lower_limit<eT>( );
964
+ constexpr eT eT_int_upper = diskio::real_as_int_upper_limit<eT>( );
940
965
941
966
for (uword row=0 ; row < x_n_rows; ++row)
942
967
{
943
968
for (uword col=0 ; col < x_n_cols; ++col)
944
969
{
945
970
const eT val = x.at (row,col);
946
971
947
- const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lowest ) && (val < eT_int_max ) && (eT (int (val)) == val);
972
+ const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lower ) && (val < eT_int_upper ) && (eT (int (val)) == val);
948
973
949
974
(is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
950
975
@@ -977,8 +1002,8 @@ diskio::save_csv_ascii(const Mat< std::complex<T> >& x, std::ostream& f, const c
977
1002
978
1003
diskio::prepare_stream<eT>(f);
979
1004
980
- const T T_int_lowest = T (std::numeric_limits< int >:: lowest () );
981
- const T T_int_max = T (std::numeric_limits< int >:: max () );
1005
+ constexpr T T_int_lower = diskio::real_as_int_lower_limit<T>( );
1006
+ constexpr T T_int_upper = diskio::real_as_int_upper_limit<T>( );
982
1007
983
1008
uword x_n_rows = x.n_rows ;
984
1009
uword x_n_cols = x.n_cols ;
@@ -994,8 +1019,8 @@ diskio::save_csv_ascii(const Mat< std::complex<T> >& x, std::ostream& f, const c
994
1019
const T abs_i = (val_i < T (0 )) ? T (-val_i) : T (val_i);
995
1020
const char sgn_i = (val_i < T (0 )) ? char (' -' ) : char (' +' );
996
1021
997
- const bool val_r_is_real_int = (is_real<T>::yes) && arma_isfinite (val_r) && (val_r > T_int_lowest ) && (val_r < T_int_max ) && (T (int (val_r)) == val_r);
998
- const bool abs_i_is_real_int = (is_real<T>::yes) && arma_isfinite (abs_i) && (abs_i < T_int_max ) && (T (int (abs_i)) == abs_i);
1022
+ const bool val_r_is_real_int = (is_real<T>::yes) && arma_isfinite (val_r) && (val_r > T_int_lower ) && (val_r < T_int_upper ) && (T (int (val_r)) == val_r);
1023
+ const bool abs_i_is_real_int = (is_real<T>::yes) && arma_isfinite (abs_i) && (abs_i < T_int_upper ) && (T (int (abs_i)) == abs_i);
999
1024
1000
1025
(val_r_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_r)) : arma_ostream::raw_print_elem (f, val_r);
1001
1026
@@ -1061,9 +1086,9 @@ diskio::save_coord_ascii(const Mat<eT>& x, std::ostream& f)
1061
1086
1062
1087
diskio::prepare_stream<eT>(f);
1063
1088
1064
- const eT eT_zero = eT (0 );
1065
- const eT eT_int_lowest = eT (std::numeric_limits< int >:: lowest () );
1066
- const eT eT_int_max = eT (std::numeric_limits< int >:: max () );
1089
+ constexpr eT eT_zero = eT (0 );
1090
+ constexpr eT eT_int_lower = diskio::real_as_int_lower_limit<eT>( );
1091
+ constexpr eT eT_int_upper = diskio::real_as_int_upper_limit<eT>( );
1067
1092
1068
1093
for (uword col=0 ; col < x.n_cols ; ++col)
1069
1094
for (uword row=0 ; row < x.n_rows ; ++row)
@@ -1075,7 +1100,7 @@ diskio::save_coord_ascii(const Mat<eT>& x, std::ostream& f)
1075
1100
f << row; f.put (' ' );
1076
1101
f << col; f.put (' ' );
1077
1102
1078
- const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lowest ) && (val < eT_int_max ) && (eT (int (val)) == val);
1103
+ const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lower ) && (val < eT_int_upper ) && (eT (int (val)) == val);
1079
1104
1080
1105
(is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
1081
1106
@@ -1116,9 +1141,9 @@ diskio::save_coord_ascii(const Mat< std::complex<T> >& x, std::ostream& f)
1116
1141
1117
1142
diskio::prepare_stream<eT>(f);
1118
1143
1119
- const eT eT_zero = eT (0 );
1120
- const T T_int_lowest = T (std::numeric_limits< int >:: lowest () );
1121
- const T T_int_max = T (std::numeric_limits< int >:: max () );
1144
+ constexpr eT eT_zero = eT (0 );
1145
+ constexpr T T_int_lower = diskio::real_as_int_lower_limit<T>( );
1146
+ constexpr T T_int_upper = diskio::real_as_int_upper_limit<T>( );
1122
1147
1123
1148
for (uword col=0 ; col < x.n_cols ; ++col)
1124
1149
for (uword row=0 ; row < x.n_rows ; ++row)
@@ -1133,8 +1158,8 @@ diskio::save_coord_ascii(const Mat< std::complex<T> >& x, std::ostream& f)
1133
1158
const T val_r = std::real (val);
1134
1159
const T val_i = std::imag (val);
1135
1160
1136
- const bool val_r_is_real_int = (is_real<T>::yes) && arma_isfinite (val_r) && (val_r > T_int_lowest ) && (val_r < T_int_max ) && (T (int (val_r)) == val_r);
1137
- const bool val_i_is_real_int = (is_real<T>::yes) && arma_isfinite (val_i) && (val_i > T_int_lowest ) && (val_i < T_int_max ) && (T (int (val_i)) == val_i);
1161
+ const bool val_r_is_real_int = (is_real<T>::yes) && arma_isfinite (val_r) && (val_r > T_int_lower ) && (val_r < T_int_upper ) && (T (int (val_r)) == val_r);
1162
+ const bool val_i_is_real_int = (is_real<T>::yes) && arma_isfinite (val_i) && (val_i > T_int_lower ) && (val_i < T_int_upper ) && (T (int (val_i)) == val_i);
1138
1163
1139
1164
(val_r_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_r)) : arma_ostream::raw_print_elem (f, val_r);
1140
1165
@@ -2966,9 +2991,9 @@ diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f, const char separator
2966
2991
uword x_n_rows = x.n_rows ;
2967
2992
uword x_n_cols = x.n_cols ;
2968
2993
2969
- const eT eT_zero = eT (0 );
2970
- const eT eT_int_lowest = eT (std::numeric_limits< int >:: lowest () );
2971
- const eT eT_int_max = eT (std::numeric_limits< int >:: max () );
2994
+ constexpr eT eT_zero = eT (0 );
2995
+ constexpr eT eT_int_lower = diskio::real_as_int_lower_limit<eT>( );
2996
+ constexpr eT eT_int_upper = diskio::real_as_int_upper_limit<eT>( );
2972
2997
2973
2998
for (uword row=0 ; row < x_n_rows; ++row)
2974
2999
{
@@ -2982,7 +3007,7 @@ diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f, const char separator
2982
3007
}
2983
3008
else
2984
3009
{
2985
- const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lowest ) && (val < eT_int_max ) && (eT (int (val)) == val);
3010
+ const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lower ) && (val < eT_int_upper ) && (eT (int (val)) == val);
2986
3011
2987
3012
(is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
2988
3013
}
@@ -3064,8 +3089,8 @@ diskio::save_coord_ascii(const SpMat<eT>& x, std::ostream& f)
3064
3089
3065
3090
diskio::prepare_stream<eT>(f);
3066
3091
3067
- const eT eT_int_lowest = eT (std::numeric_limits< int >:: lowest () );
3068
- const eT eT_int_max = eT (std::numeric_limits< int >:: max () );
3092
+ constexpr eT eT_int_lower = diskio::real_as_int_lower_limit<eT>( );
3093
+ constexpr eT eT_int_upper = diskio::real_as_int_upper_limit<eT>( );
3069
3094
3070
3095
typename SpMat<eT>::const_iterator iter = x.begin ();
3071
3096
typename SpMat<eT>::const_iterator iter_end = x.end ();
@@ -3077,7 +3102,7 @@ diskio::save_coord_ascii(const SpMat<eT>& x, std::ostream& f)
3077
3102
3078
3103
const eT val = (*iter);
3079
3104
3080
- const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lowest ) && (val < eT_int_max ) && (eT (int (val)) == val);
3105
+ const bool is_real_int = (is_real<eT>::yes) && arma_isfinite (val) && (val > eT_int_lower ) && (val < eT_int_upper ) && (eT (int (val)) == val);
3081
3106
3082
3107
(is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
3083
3108
@@ -3120,8 +3145,8 @@ diskio::save_coord_ascii(const SpMat< std::complex<T> >& x, std::ostream& f)
3120
3145
3121
3146
diskio::prepare_stream<eT>(f);
3122
3147
3123
- const T T_int_lowest = T (std::numeric_limits< int >:: lowest () );
3124
- const T T_int_max = T (std::numeric_limits< int >:: max () );
3148
+ constexpr T T_int_lower = diskio::real_as_int_lower_limit<T>( );
3149
+ constexpr T T_int_upper = diskio::real_as_int_upper_limit<T>( );
3125
3150
3126
3151
typename SpMat<eT>::const_iterator iter = x.begin ();
3127
3152
typename SpMat<eT>::const_iterator iter_end = x.end ();
@@ -3136,8 +3161,8 @@ diskio::save_coord_ascii(const SpMat< std::complex<T> >& x, std::ostream& f)
3136
3161
const T val_r = std::real (val);
3137
3162
const T val_i = std::imag (val);
3138
3163
3139
- const bool val_r_is_real_int = (is_real<T>::yes) && arma_isfinite (val_r) && (val_r > T_int_lowest ) && (val_r < T_int_max ) && (T (int (val_r)) == val_r);
3140
- const bool val_i_is_real_int = (is_real<T>::yes) && arma_isfinite (val_i) && (val_i > T_int_lowest ) && (val_i < T_int_max ) && (T (int (val_i)) == val_i);
3164
+ const bool val_r_is_real_int = (is_real<T>::yes) && arma_isfinite (val_r) && (val_r > T_int_lower ) && (val_r < T_int_upper ) && (T (int (val_r)) == val_r);
3165
+ const bool val_i_is_real_int = (is_real<T>::yes) && arma_isfinite (val_i) && (val_i > T_int_lower ) && (val_i < T_int_upper ) && (T (int (val_i)) == val_i);
3141
3166
3142
3167
(val_r_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_r)) : arma_ostream::raw_print_elem (f, val_r);
3143
3168
0 commit comments