Well... I couldn't think of something else better to do with my time. So I decided to build a class that wraps around the concept of floating-point numbers with an emphasis on precision at the expense of accuracy.
Code:
const int STRDOUBLE_MAX_PRECISION = 15;
class strdouble {
public:
strdouble(): integer(0), frac(0), exp(0), prec(1) {}
explicit strdouble(const std::string&);
strdouble& operator=(const std::string&);
strdouble& operator++();
strdouble& operator--();
strdouble operator++(int);
strdouble operator--(int);
friend bool operator==(const strdouble&, const strdouble&);
friend bool operator<(const strdouble&, const strdouble&);
friend bool operator>(const strdouble&, const strdouble&);
double value() const;
private:
double integer; /// integer portion of fractional number
double frac; /// normalized fractional portion of fractional number
int exp; /// base 10 exponent of fractional portion
int prec; /// overall precision (significant digits)
};
The basic idea is to receive a well formatted string and decompose it into the integer and fractional parts. The fractional part is in fact an integral too. exp is a base 10 exponent to be applied to it. Exp is expected to be always less than 0.
I was planning to also define a constructor accepting a double.
integer + frac * pow(10, exp) will reconstruct the floating-point number.
I have all of the above operator overloads already defined plus some more. Other overloads would eventually be defined of course. Namely the arithmetic operators. Also, some member functions replacements for the arithmetic operators would also be supplied. These would differ in the sense that the user could specify the precision of the result throught either truncation or rounding.
Anyways... I don't think this approach is the best. Each instance is too big, the class will grow to become slow and clumsy. Every operation subject to conversions...
The questions is... do you think this has some use? Or is it best if perhaps I concentrate more on how doubles are stored in memory work from there in an attempt to create a precision based floating-point type?