00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00021 00022 ------------------------------------------------------------------- 00023 */ 00024 #ifndef O2SCL_FORMAT_FLOAT_H 00025 #define O2SCL_FORMAT_FLOAT_H 00026 00027 #include <cstdio> 00028 #include <cstdlib> 00029 #include <iostream> 00030 #include <o2scl/err_hnd.h> 00031 #include <o2scl/file_format.h> 00032 #include <o2scl/misc.h> 00033 #include <o2scl/string_conv.h> 00034 00035 #ifndef DOXYGENP 00036 namespace o2scl { 00037 #endif 00038 00039 /** 00040 \brief Format a floating point number into a Latex or HTML string 00041 00042 This class formats floating point strings into something 00043 useful for HTML or Latex documents. 00044 00045 The base-10 logarithm of the smallest and largest 00046 numbers to be represented without a string of the form 00047 \verbatim 00048 $\times 10^{\mathrm{x}}$ 00049 \endverbatim 00050 can be specified in set_exp_min() and set_exp_max(). The number 00051 of significant figures can be specified with set_sig_figs() (the 00052 default is 5). 00053 00054 \comment 00055 To force convert() to adds zeros to the right side of the 00056 mantissa to guarantee that the requested number of significant 00057 digits is given, use <tt>set_pad_zeros(true)</tt>. 00058 \endcomment 00059 00060 Note that this function does not warn the user if the number 00061 of significant figures requested is larger than the machine 00062 precision. 00063 00064 The format for a normal number is 00065 \verbatim 00066 prefix (sign-string) number suffix 00067 \endverbatim 00068 and in scientific notation is 00069 \verbatim 00070 sci-prefix (sci-sign-string) number times-string 00071 exp-prefix (exp-sign-string) exponent exp-suffix sci-suffix 00072 \endverbatim 00073 00074 \future Implement padding with zeros 00075 \future Separate out decimal point and make it separate. 00076 00077 */ 00078 class format_float { 00079 00080 protected: 00081 00082 /// \name Base text settings 00083 //@{ 00084 /// Prefix (default "") 00085 std::string prefx; 00086 /// Suffix (default "") 00087 std::string suffx; 00088 /// Sign string (default "-") 00089 std::string sgn; 00090 /// Sign string in scientific mode (default "-") 00091 std::string sci_sgn; 00092 /// Sign string for exponent in scientific mode (default "-") 00093 std::string exp_sgn; 00094 /// Prefix in scientific mode (default "") 00095 std::string sci_prefx; 00096 /// Suffix in scientific mode (default "") 00097 std::string sci_suffx; 00098 /// Exponent prefix (default "") 00099 std::string exp_prefx; 00100 /// Exponent suffix (default "") 00101 std::string exp_suffx; 00102 /// Times symbol for scientific mode (default " x ") 00103 std::string tmes; 00104 /// String for numbers which are not finite (default "Nan") 00105 std::string not_finte; 00106 /// String for zeros (default "0") 00107 std::string zeros; 00108 //@} 00109 00110 /// \name Other settings 00111 //@{ 00112 /// Number of significant figures (default 5) 00113 size_t sig_fgs; 00114 /// Lower limit for automatic mode (default -2) 00115 int ex_mn; 00116 /// Upper limit for automatic mode (default -2) 00117 int ex_mx; 00118 /// If true, pad with zeros (default false) 00119 bool pad_zeros; 00120 //@} 00121 00122 public: 00123 00124 format_float() { 00125 prefx=""; 00126 sgn="-"; 00127 suffx=""; 00128 sci_prefx=""; 00129 tmes=" x "; 00130 exp_prefx="^{"; 00131 exp_sgn="-"; 00132 sci_sgn="-"; 00133 exp_suffx="}"; 00134 sci_suffx=""; 00135 not_finte="Nan"; 00136 zeros="0"; 00137 ex_mn=-2; 00138 ex_mx=3; 00139 sig_fgs=5; 00140 pad_zeros=false; 00141 } 00142 00143 /** \brief Set HTML mode 00144 00145 This function is equivalent to the settings: 00146 \code 00147 format_float::set_prefix(""); 00148 format_float::set_sign("-"); 00149 format_float::set_suffix(""); 00150 format_float::set_sci_prefix(""); 00151 format_float::set_times(" × "); 00152 format_float::set_exp_prefix("10<sup>"); 00153 format_float::set_exp_sign("-"); 00154 format_float::set_sci_sign("-"); 00155 format_float::set_exp_suffix("</sup>"); 00156 format_float::set_sci_suffix(""); 00157 format_float::set_not_finite("Nan"); 00158 format_float::set_zero("0"); 00159 \endcode 00160 */ 00161 int html_mode() { 00162 prefx=""; 00163 sgn="-"; 00164 suffx=""; 00165 sci_prefx=""; 00166 tmes=" × "; 00167 exp_prefx="10<sup>"; 00168 exp_sgn="-"; 00169 sci_sgn="-"; 00170 exp_suffx="</sup>"; 00171 sci_suffx=""; 00172 not_finte="Nan"; 00173 zeros="0"; 00174 return 0; 00175 } 00176 00177 /** \brief Set Latex mode 00178 00179 This function is equivalent to the settings: 00180 \code 00181 format_float::set_prefix(""); 00182 format_float::set_sign("$-$"); 00183 format_float::set_suffix(""); 00184 format_float::set_sci_prefix(""); 00185 format_float::set_times(" $\\times "); 00186 format_float::set_exp_prefix("10^{"); 00187 format_float::set_exp_sign("-"); 00188 format_float::set_sci_sign("$-$"); 00189 format_float::set_exp_suffix("}"); 00190 format_float::set_sci_suffix(""); 00191 format_float::set_not_finite("Nan"); 00192 format_float::set_zero("0"); 00193 \endcode 00194 */ 00195 int latex_mode() { 00196 prefx=""; 00197 sgn="$-$"; 00198 suffx=""; 00199 sci_prefx=""; 00200 tmes=" $\\times "; 00201 exp_prefx="10^{"; 00202 sci_sgn="$-$"; 00203 exp_sgn="-"; 00204 exp_suffx="}$"; 00205 sci_suffx=""; 00206 not_finte="Nan"; 00207 zeros="0"; 00208 return 0; 00209 } 00210 00211 /** \name Set text settings 00212 00213 These are modified by the functions html_mode() and latex_mode() 00214 */ 00215 //@{ 00216 /// set prefix 00217 int set_prefix(std::string prefix) { 00218 prefx=prefix; 00219 return 0; 00220 } 00221 00222 /// Set suffix 00223 int set_suffix(std::string suffix) { 00224 suffx=suffix; 00225 return 0; 00226 } 00227 00228 /// Set prefix for scientific notation 00229 int set_sci_prefix(std::string sci_prefix) { 00230 sci_prefix=sci_prefx; 00231 return 0; 00232 } 00233 00234 /// Set suffix for scientific notation 00235 int set_sci_suffix(std::string sci_suffix) { 00236 sci_suffx=sci_suffix; 00237 return 0; 00238 } 00239 00240 /// Set prefix for exponent 00241 int set_exp_prefix(std::string exp_prefix) { 00242 exp_prefx=exp_prefix; 00243 return 0; 00244 } 00245 00246 /// Set suffix for exponent 00247 int set_exp_suffix(std::string exp_suffix) { 00248 exp_suffx=exp_suffix; 00249 return 0; 00250 } 00251 00252 /// Set sign 00253 int set_sign(std::string sign) { 00254 sgn=sign; 00255 return 0; 00256 } 00257 00258 /// Set sign for exponent 00259 int set_exp_sign(std::string exp_sign) { 00260 exp_sgn=exp_sign; 00261 return 0; 00262 } 00263 00264 /// Set sign for scientific notation 00265 int set_sci_sign(std::string sci_sign) { 00266 sci_sgn=sci_sign; 00267 return 0; 00268 } 00269 00270 /// Set times 00271 int set_times(std::string times) { 00272 tmes=times; 00273 return 0; 00274 } 00275 00276 /// Set zero 00277 int set_zero(std::string zero) { 00278 zeros=zero; 00279 return 0; 00280 } 00281 00282 /// Set string for numbers which are not finite 00283 int set_not_finite(std::string not_finite) { 00284 not_finte=not_finite; 00285 return 0; 00286 } 00287 //@} 00288 00289 /** \name Set other settings 00290 00291 These are not modified by the functions html_mode() and latex_mode() 00292 */ 00293 //@{ 00294 /// Set the exponent limits 00295 int set_exp_limits(int min, int max) { 00296 ex_mn=min; 00297 ex_mx=max; 00298 return 0; 00299 } 00300 00301 /// Set the number of significant figures 00302 int set_sig_figs(size_t sig_figs) { 00303 sig_fgs=sig_figs; 00304 return 0; 00305 } 00306 00307 #ifdef O2SCL_NEVER_DEFINED 00308 /// Set pad zeros 00309 int set_pad_zeros(bool pad) { 00310 pad_zeros=pad; 00311 return 0; 00312 } 00313 #endif 00314 //@} 00315 00316 /** \name Get text settings 00317 00318 These are modified by the functions html_mode() and latex_mode() 00319 */ 00320 //@{ 00321 /// Get prefix 00322 std::string get_prefix() { 00323 return prefx; 00324 } 00325 00326 /// Get suffix 00327 std::string get_suffix() { 00328 return suffx; 00329 } 00330 00331 /// Get prefix for scientific notation 00332 std::string get_sci_prefix() { 00333 return sci_prefx; 00334 } 00335 00336 /// Get suffix for scientific notation 00337 std::string get_sci_suffix() { 00338 return sci_suffx; 00339 } 00340 00341 /// Get prefix for exponent 00342 std::string get_exp_prefix() { 00343 return exp_prefx; 00344 } 00345 00346 /// Get suffix for exponent 00347 std::string get_exp_suffix() { 00348 return exp_suffx; 00349 } 00350 00351 /// Get sign 00352 std::string get_sign() { 00353 return sgn; 00354 } 00355 00356 /// Get sign for exponent 00357 std::string get_exp_sign() { 00358 return exp_sgn; 00359 } 00360 00361 /// Get sign for scientific notation 00362 std::string get_sci_sign() { 00363 return sci_sgn; 00364 } 00365 00366 /// Get times 00367 std::string get_times() { 00368 return tmes; 00369 } 00370 00371 /// Get zero 00372 std::string get_zero() { 00373 return zeros; 00374 } 00375 00376 /// Get string for numbers which are not finite 00377 std::string get_not_finite() { 00378 return not_finte; 00379 } 00380 //@} 00381 00382 /** \name Get other settings 00383 00384 These are not modified by the functions html_mode() and latex_mode() 00385 */ 00386 //@{ 00387 /// Get minimum exponent 00388 int get_exp_min() { 00389 return ex_mn; 00390 } 00391 00392 /// Get maximum exponent 00393 int get_exp_max() { 00394 return ex_mx; 00395 } 00396 00397 /// Get sig_figs 00398 size_t get_sig_figs() { 00399 return sig_fgs; 00400 } 00401 00402 #ifdef O2SCL_NEVER_DEFINED 00403 /// Get pad_zeros 00404 bool get_pad_zeros() { 00405 return pad_zeros; 00406 } 00407 #endif 00408 //@} 00409 00410 /// Convert a floating point number to a string 00411 std::string convert(double x) { 00412 00413 // Handle special cases 00414 if (!finite(x)) return not_finte; 00415 if (x==0.0) return zeros; 00416 00417 // Check sign 00418 int sign=1; 00419 if (x<0) { 00420 x=-x; 00421 sign=-1; 00422 } 00423 00424 // Compute exponent and mantissa separately 00425 int expo=((int)log10(x)), itmp; 00426 double mant=x/pow(10.0,expo); 00427 if (mant<1.0) { 00428 mant*=10.0; 00429 expo-=1; 00430 } 00431 itmp=(int)(mant*pow(10.0,sig_fgs)+0.5); 00432 mant=((double)itmp)/pow(10.0,sig_fgs); 00433 00434 // Begin constructing the string for output 00435 std::string s; 00436 00437 if (expo<=ex_mx && expo>=ex_mn) { 00438 00439 // Normal notation 00440 00441 std::string nums=o2scl::dtos(mant*pow(10.0,expo),sig_fgs,true); 00442 if (pad_zeros) { 00443 while (nums.length()<sig_fgs+1) nums+='0'; 00444 } 00445 00446 if (sign==-1) { 00447 s=prefx+sgn+nums+suffx; 00448 } else { 00449 s=prefx+nums+suffx; 00450 } 00451 00452 } else { 00453 00454 // Scientific notation 00455 00456 std::string nums=o2scl::dtos(mant,sig_fgs,true); 00457 if (pad_zeros) { 00458 while (nums.length()<sig_fgs+1) nums+='0'; 00459 } 00460 00461 // Compute sign of exponent 00462 int expo_sign=1; 00463 if (expo<0) { 00464 expo_sign=-1; 00465 expo*=-1; 00466 } 00467 00468 if (sign==-1) { 00469 if (expo_sign<0) { 00470 s=sci_prefx+sci_sgn+nums+tmes+exp_prefx+ 00471 exp_sgn+o2scl::itos(expo)+exp_suffx+sci_suffx; 00472 } else { 00473 s=sci_prefx+sci_sgn+nums+tmes+exp_prefx+ 00474 o2scl::itos(expo)+exp_suffx+sci_suffx; 00475 } 00476 } else { 00477 if (expo_sign<0) { 00478 s=sci_prefx+nums+tmes+exp_prefx+ 00479 exp_sgn+o2scl::itos(expo)+exp_suffx+sci_suffx; 00480 } else { 00481 s=sci_prefx+nums+tmes+exp_prefx+ 00482 o2scl::itos(expo)+exp_suffx+sci_suffx; 00483 } 00484 } 00485 00486 } 00487 00488 return s; 00489 00490 } 00491 00492 00493 }; 00494 00495 #ifndef DOXYGENP 00496 } 00497 #endif 00498 00499 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page