pelib  2.0.0
src/AlgebraData.cpp
Go to the documentation of this file.
00001 /*
00002  Copyright 2015 Nicolas Melot
00003 
00004  This file is part of Pelib.
00005 
00006  Pelib is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation, either version 3 of the License, or
00009  (at your option) any later version.
00010 
00011  Pelib is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with Pelib. If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 #include <iostream>
00022 #include <math.h>
00023 
00024 #include <pelib/AlgebraData.hpp>
00025 
00026 #if 01
00027 #define debug(var) cout << "[" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << "] " << #var << " = \"" << var << "\"" << endl;
00028 #else
00029 #define debug(var)
00030 #endif
00031 
00032 using namespace std;
00033 
00034 namespace pelib
00035 {
00036         AlgebraData::AlgebraData(const std::string name, precision prec)
00037         {
00038                 this->name = name;
00039                 this->prec = prec;
00040         }
00041 
00042         AlgebraData::AlgebraData(const AlgebraData& data, precision prec)
00043         {
00044                 this->name = data.getName();
00045                 this->prec = prec;
00046         }
00047         
00048         void
00049         AlgebraData::setName(const std::string name)
00050         {
00051                 this->name = name;
00052         }
00053         
00054         const std::string&
00055         AlgebraData::getName() const
00056         {
00057                 return this->name;
00058         }
00059 
00060         AlgebraData::precision
00061         AlgebraData::getPrecision() const
00062         {
00063                 return this->prec;
00064         }
00065 
00066         template<>
00067         float
00068         AlgebraData::fixPrecision(const float &data, std::streamsize precision)
00069         {
00070                 float val = data;
00071                 // Get the decimal part of our value
00072                 float integ = floorf(data);
00073                 float visible_dec = (data - integ) * powf(10, precision);
00074 
00075                 // If there is more digits after the part that will actually be visible, then
00076                 // we increment this digit
00077                 if(visible_dec != floorf(visible_dec))
00078                 {
00079                         visible_dec = 1 / powf(10, precision);
00080                         val += visible_dec;
00081                 }
00082 
00083                 return val;
00084         }
00085 
00086         template<>
00087         double
00088         AlgebraData::fixPrecision<double>(const double &data, std::streamsize precision)
00089         {
00090                 double val = data;
00091                 // Get the decimal part of our value
00092                 double integ = floorf(data);
00093                 double visible_dec = (data - integ) * powf(10, precision);
00094 
00095                 // If there is more digits after the part that will actually be visible, then
00096                 // we increment this digit
00097                 if(visible_dec != floor(visible_dec))
00098                 {
00099                         visible_dec = 1 / pow(10, precision);
00100                         val += visible_dec;
00101                 }
00102 
00103                 return data;
00104         }
00105 
00106         template<>
00107         long double
00108         AlgebraData::fixPrecision<long double>(const long double &data, std::streamsize precision)
00109         {
00110                 long double val = data;
00111                 // Get the decimal part of our value
00112                 long double integ = floorf(data);
00113                 long double visible_dec = (data - integ) * powf(10, precision);
00114 
00115                 // If there is more digits after the part that will actually be visible, then
00116                 // we increment this digit
00117                 if(visible_dec != floorl(visible_dec))
00118                 {
00119                         visible_dec = 1 / powl(10, precision);
00120                         val += visible_dec;
00121                 }
00122 
00123                 return data;
00124         }
00125 }