PrintCtrl.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <cmath>
00019
00020 #include <iostream>
00021 #include <fstream>
00022
00023 #include "PrintCtrl.h"
00024
00025 using namespace std;
00026
00027 namespace Cantera {
00028
00029
00030
00031 PrintCtrl::CROP_TYPE_GLOBAL PrintCtrl::GlobalCrop = GCT_NOPREF;
00032
00033
00034 PrintCtrl::PrintCtrl(std::ostream &coutProxy, int Ndec,
00035 CROP_TYPE ctlocal) :
00036 m_cout(coutProxy),
00037 m_Ndec(Ndec),
00038 m_precision(12),
00039 m_wMin(9),
00040 m_wMax(19),
00041 m_cropCntrl(ctlocal)
00042 {
00043
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 void PrintCtrl::pr_de_c10(const double din, int p, const int wMin,
00059 const int wMax) {
00060 double d = cropAbs10(din, m_Ndec);
00061 pr_de(d, p, wMin, wMax);
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 void PrintCtrl::pr_de(const double d, int sigDigIn, const int wMinIn,
00075 const int wMaxIn) {
00076 int p = m_precision;
00077 if (sigDigIn != -1) {
00078 p = sigDigIn-1;
00079 if (p < 0) p = 0;
00080 }
00081
00082 int wMin = m_wMin;
00083 if (wMinIn != -1) {
00084 wMin = wMinIn;
00085 if (wMin < 1) wMin = 1;
00086 }
00087
00088 int wMax = m_wMax;
00089 if (wMaxIn != -1) {
00090 wMax = wMaxIn;
00091 if (wMax < 1) wMax = 1;
00092 }
00093
00094 if (wMin > wMax) wMax = wMin;
00095
00096
00097
00098 double dfabs = fabs(d);
00099
00100
00101 int requestedLength = 6 + p;
00102 if (d < 0.0) {
00103 requestedLength++;
00104 }
00105 if (dfabs < 9.9999999999E-99) {
00106 requestedLength++;
00107 }
00108 if (dfabs > 9.9999999999E99) {
00109 requestedLength++;
00110 }
00111 if (requestedLength > wMax) {
00112 p -= (requestedLength - wMax);
00113 if (p < 0) p = 0;
00114 }
00115
00116
00117 m_cout.setf(ios_base::scientific | ios_base::uppercase);
00118 int wold = m_cout.width(wMin);
00119 int pold = m_cout.precision(p);
00120
00121 m_cout << d;
00122
00123 m_cout.precision(pold);
00124 m_cout.unsetf(ios_base::scientific);
00125
00126
00127 m_cout.width(wold);
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 double PrintCtrl::cropAbs10(const double d, int Ndec) const {
00149 if (!doCrop()) {
00150 return d;
00151 }
00152 if (Ndec < -301 || Ndec > 301) {
00153 return d;
00154 }
00155 double sgn = 1.0;
00156 if (d < 0.0) sgn = -1.0;
00157 double dfabs = fabs(d);
00158 double pdec = pow(10.0, (double) Ndec);
00159 if (dfabs < pdec) {
00160 return 0.0;
00161 }
00162 double dl10 = log10(dfabs);
00163 int N10 = (int) dl10;
00164 if (dl10 > -0.0) {
00165 N10 += 1;
00166 }
00167 int nsig = N10 - Ndec;
00168 double retn = cropSigDigits(d, nsig);
00169 return retn;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 double PrintCtrl::cropSigDigits(const double d, int nSig) const {
00184 if (!doCrop()) {
00185 return d;
00186 }
00187 if (nSig <=0) nSig = 1;
00188 if (nSig >=9) nSig = 9;
00189 double sgn = 1.0;
00190 if (d < 0.0) sgn = -1.0;
00191 double dfabs = fabs(d);
00192 double dl10 = log10(dfabs);
00193 int N10 = (int) dl10;
00194 if (dl10 > -0.0) {
00195 N10 += 1;
00196 }
00197 int E10 = -N10 + nSig ;
00198 double pfabs = dfabs * pow(10.0, (double) E10);
00199 pfabs *= (1.0 + 1.0E-14);
00200 long int nfabs = (long int) pfabs;
00201 double remainder = pfabs - nfabs;
00202 if (remainder > 0.5) {
00203 nfabs++;
00204 }
00205 double paltabs = (double) nfabs;
00206 double daltabs = paltabs * pow(10.0, (double) -E10);
00207 return (sgn * daltabs);
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 int PrintCtrl::setNdec(int Ndec) {
00217 int nold = m_Ndec;
00218 m_Ndec = Ndec;
00219 return nold;
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 int PrintCtrl::setSigDigits(int nSigDigits) {
00229 int nold = m_precision + 1;
00230 m_precision = nSigDigits - 1;
00231 if (m_precision < 0) m_precision = 0;
00232 return nold;
00233 }
00234
00235
00236
00237
00238
00239
00240
00241 int PrintCtrl::setWmin(int wmin) {
00242 int nold = m_wMin;
00243 m_wMin = wmin;
00244 return nold;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254 int PrintCtrl::setWmax(int wmax) {
00255 int nold = m_wMax;
00256 m_wMax = wmax;
00257 return nold;
00258 }
00259
00260 bool PrintCtrl::doCrop() const {
00261 bool retn = ((m_cropCntrl == CT_ON) || (m_cropCntrl == CT_ON_GLOBALOBEY));
00262 if (m_cropCntrl == CT_ON_GLOBALOBEY) {
00263 if (GlobalCrop == GCT_NOCROP) {
00264 retn = false;
00265 }
00266 } else if (m_cropCntrl == CT_OFF_GLOBALOBEY) {
00267 if (GlobalCrop == GCT_CROP) {
00268 retn = true;
00269 }
00270 }
00271 return retn;
00272 }
00273
00274 void PrintCtrl:: setCropCntrl(CROP_TYPE ctlocal) {
00275 m_cropCntrl = ctlocal;
00276 }
00277 }