# Usage of the faulty number type

• 04-17-2009
twister86
Usage of the faulty number type
I have the following problem that is quite complex and don't have a clue how to solve it:

---

Let's assume that we have got a class that defines the new type called FaultyUnsignedLongLong, which is similar to standard unsigned long long, but in which some arithmetic operations on numbers in this type are returning false values or are causing errors.

The problem is to create a function FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt) that is calculating the product (fromInt)*(fromInt+1)*...*(toInt-1)*(toInt), where fromInt and toInt are integers such that 0 <= fromInt <= toInt <= 600000.

The type FaultyUnsignedLongLong is implementing some arithmetic and logical operators. Unfortunately there is always a risk that these operators will returne false results. Arithmetic operations can cause the following exceptions:

- class ErrorCalculation {}; - operation caused an error and will not return the result,
- class ErrorPrevCalculation {}; - previous arithmetic operation was calculated in the incorrect way, so the result that was returned by this operations has been incorrect,
- class ErrorPrevCalculaionsIncreasedByOne : public ErrorPrevCalculation {}; - means that the previous arithmetic operation returned a result that was larger by one than it's true solution,
- class SystemError {}; - means that operation has cause an overall reset (values of all variables that were defined as FaultyUnsignedLongLong are change to 0). Such reset could be also cause on purpose - you have to use a static method reset().

All of the exceptions can occur in a random manner.

The file in which we are implementing the function FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt) must have the following properties:

-it should include only the file FaultyUnsignedLongLong.h,
-using #define inside the file is forbidden,
-inside the file one can't use or declare any variables of the type other then FaultyUnsignedLongLong (particullary one can't useconst_cast, type_of, class, struct or any other keywords like bool, char, long, float etc.; the int keyword can be only used twice in the definition of the function FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt),
-there should be only a definition of one function: FaultyUnsignedLongLong cprod(const int &fromInt, const int &toInt) that is calculating the product (fromInt)*(fromInt+1)*...*(toInt-1)*(toInt),
-each of the variables fromInt, toInt could be used only 4 times in the file.

A declaration of the class FaultyUnsignedLongLong looks like that:
Code:

```class FaultyUnsignedLongLong {  private:   unsigned long long int value;  public:   FaultyUnsignedLongLong(const unsigned long long int &i = 0);   FaultyUnsignedLongLong(const FaultyUnsignedLongLong &i);   ~FaultyUnsignedLongLong();   static void reset();   friend FaultyUnsignedLongLong operator+(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j)                 throw(ErrorCalculation, ErrorPrevCalculation, SystemError);   friend FaultyUnsignedLongLong operator-(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j)                 throw(ErrorCalculation, ErrorPrevCalculation, SystemError);   friend FaultyUnsignedLongLong operator*(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j)                 throw(ErrorCalculation, ErrorPrevCalculation, SystemError);   friend FaultyUnsignedLongLong operator/(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j)                 throw(ErrorCalculation, ErrorPrevCalculation, SystemError);   friend FaultyUnsignedLongLong operator%(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j)                 throw(ErrorCalculation, ErrorPrevCalculation, SystemError);   friend bool operator==(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);   friend bool operator!=(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);   friend bool operator<=(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j);   friend bool operator>=(const FaultyUnsignedLongLong &i, const FaultyUnsignedLongLong &j); };```
and it's source code has the following includes:

Code:

```#include <map> #include <set> #include <iostream> #include <cstdlib> #include <climits> #include <iosfwd>```
---

I will be very glad if any of you could give me some hints or possible solutions of this problem.
• 04-17-2009
grumpy
It's not that hard.

As to the various properties ..... they're coding constraints. All you need to do is identify some method (eg an algorithm) that will compute the required result, and take recovery action whenever an exception is thrown. Once you have such an algorithm described, implementing it within the coding constraints should be fairly simple.

And, no, I'm not going to show sample code ..... I assume the whole point of the exercise is for you to learn by thinking through and doing it yourself.
• 04-18-2009
twister86
I have an algorithm that is using lists of elements of the type FaultyUnsignedLongLong and it can skip the problems of all of the exceptions except for the SystemError thing. :(
• 04-18-2009
grumpy
When a SystemError occurs, restart from the beginning.