Kod: |
#ifndef NUM_IO_HPP_
#define NUM_IO_HPP_ #include <limits> #include <iomanip> #include <iostream> // intendirani NumT: float, double, long double template <typename NumT> class PrecFP { NumT _fp; public: static const int prec = std::numeric_limits<NumT>::digits10 + 1; const NumT &fp() const { return _fp; } explicit PrecFP(const NumT &fp) : _fp(fp) {} }; template <typename NumT> inline PrecFP<NumT> precFP(const NumT &fp) { return PrecFP<NumT>(fp); } template <typename Ch, class Tr, typename NumT> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const PrecFP<NumT> &pfp) { const std::ios_base::fmtflags oldFlags = os.flags(std::ios_base::scientific | std::ios_base::showpos | std::ios_base::uppercase); const std::streamsize oldPrec = os.precision(pfp.prec); os << pfp.fp(); // error handling? os.precision(oldPrec); os.flags(oldFlags); return os; } #endif // NUM_IO_HPP_ |
Kod: |
#include "numIO.hpp"
int main() { double dbl = 1.00001; float flt = 1.00001f; std::cout << precFP(dbl) << std::endl; std::cout << precFP(flt) << std::endl; return 0; } |
Kod: |
+1.0000100000000001E+000
+1.0000100E+000 |
output generated using printer-friendly topic mod. Vremenska zona: GMT + 01:00.