@@ -688,7 +688,32 @@ diskio::prepare_stream(std::ostream& f)
688688
689689 return cell_width;
690690 }
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 ) ) );
691701
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+ }
692717
693718
694719
@@ -935,16 +960,16 @@ diskio::save_csv_ascii(const Mat<eT>& x, std::ostream& f, const char separator)
935960 uword x_n_rows = x.n_rows ;
936961 uword x_n_cols = x.n_cols ;
937962
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>( );
940965
941966 for (uword row=0 ; row < x_n_rows; ++row)
942967 {
943968 for (uword col=0 ; col < x_n_cols; ++col)
944969 {
945970 const eT val = x.at (row,col);
946971
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);
948973
949974 (is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
950975
@@ -977,8 +1002,8 @@ diskio::save_csv_ascii(const Mat< std::complex<T> >& x, std::ostream& f, const c
9771002
9781003 diskio::prepare_stream<eT>(f);
9791004
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>( );
9821007
9831008 uword x_n_rows = x.n_rows ;
9841009 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
9941019 const T abs_i = (val_i < T (0 )) ? T (-val_i) : T (val_i);
9951020 const char sgn_i = (val_i < T (0 )) ? char (' -' ) : char (' +' );
9961021
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);
9991024
10001025 (val_r_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_r)) : arma_ostream::raw_print_elem (f, val_r);
10011026
@@ -1061,9 +1086,9 @@ diskio::save_coord_ascii(const Mat<eT>& x, std::ostream& f)
10611086
10621087 diskio::prepare_stream<eT>(f);
10631088
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>( );
10671092
10681093 for (uword col=0 ; col < x.n_cols ; ++col)
10691094 for (uword row=0 ; row < x.n_rows ; ++row)
@@ -1075,7 +1100,7 @@ diskio::save_coord_ascii(const Mat<eT>& x, std::ostream& f)
10751100 f << row; f.put (' ' );
10761101 f << col; f.put (' ' );
10771102
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);
10791104
10801105 (is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
10811106
@@ -1116,9 +1141,9 @@ diskio::save_coord_ascii(const Mat< std::complex<T> >& x, std::ostream& f)
11161141
11171142 diskio::prepare_stream<eT>(f);
11181143
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>( );
11221147
11231148 for (uword col=0 ; col < x.n_cols ; ++col)
11241149 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)
11331158 const T val_r = std::real (val);
11341159 const T val_i = std::imag (val);
11351160
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);
11381163
11391164 (val_r_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_r)) : arma_ostream::raw_print_elem (f, val_r);
11401165
@@ -2966,9 +2991,9 @@ diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f, const char separator
29662991 uword x_n_rows = x.n_rows ;
29672992 uword x_n_cols = x.n_cols ;
29682993
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>( );
29722997
29732998 for (uword row=0 ; row < x_n_rows; ++row)
29742999 {
@@ -2982,7 +3007,7 @@ diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f, const char separator
29823007 }
29833008 else
29843009 {
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);
29863011
29873012 (is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
29883013 }
@@ -3064,8 +3089,8 @@ diskio::save_coord_ascii(const SpMat<eT>& x, std::ostream& f)
30643089
30653090 diskio::prepare_stream<eT>(f);
30663091
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>( );
30693094
30703095 typename SpMat<eT>::const_iterator iter = x.begin ();
30713096 typename SpMat<eT>::const_iterator iter_end = x.end ();
@@ -3077,7 +3102,7 @@ diskio::save_coord_ascii(const SpMat<eT>& x, std::ostream& f)
30773102
30783103 const eT val = (*iter);
30793104
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);
30813106
30823107 (is_real_int) ? arma_ostream::raw_print_elem (f, int (val)) : arma_ostream::raw_print_elem (f, val);
30833108
@@ -3120,8 +3145,8 @@ diskio::save_coord_ascii(const SpMat< std::complex<T> >& x, std::ostream& f)
31203145
31213146 diskio::prepare_stream<eT>(f);
31223147
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>( );
31253150
31263151 typename SpMat<eT>::const_iterator iter = x.begin ();
31273152 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)
31363161 const T val_r = std::real (val);
31373162 const T val_i = std::imag (val);
31383163
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);
31413166
31423167 (val_r_is_real_int) ? arma_ostream::raw_print_elem (f, int (val_r)) : arma_ostream::raw_print_elem (f, val_r);
31433168
0 commit comments