Precision based floating-point

This is a discussion on Precision based floating-point within the C++ Programming forums, part of the General Programming Boards category; Well... I couldn't think of something else better to do with my time. So I decided to build a class ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,576

    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?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,576
    I take it, this is a useless class

    Thanks.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Not useless, but overkill when you can just use a double data type to do the same thing with less code and less overhead.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,576
    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);
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 09:14 PM
  3. Floating point #'s, why so much talk about it?
    By scuzzo84 in forum C Programming
    Replies: 5
    Last Post: 09-20-2005, 05:29 PM
  4. Head Banging Floating Point Conversions
    By Davros in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2004, 09:23 AM
  5. floating point exception? what causes these?
    By salvelinus in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2002, 01:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21