# Precision based floating-point

• 07-15-2006
Mario F.
Precision based floating-point
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?
• 07-16-2006
Mario F.
I take it, this is a useless class :)

Thanks.
• 07-17-2006
VirtualAce
Not useless, but overkill when you can just use a double data type to do the same thing with less code and less overhead.
• 07-17-2006
Mario F.
My problem with that was having the class instancing with a number that was probably not what the user expected.

Code:

```double bar = 1 / 3; CFixedDouble foo(bar, precision);```
• 07-17-2006
whiteflags
I don't think that example best proves the point you were going to make. The division will always happen before the double is initialized, and while you could force the double to some artificial precision from the beginning it's okay to use double's full range; that's what it's there for. If bar were smaller in bytes if it had 0.3334 instead of 0.3 repeating, you'd have a stronger point I'd think, but it's not.