void randomCalculate(int size, char ch) { mutex.lock(); // enter critical section for (int i = 0; i < size; ++i) { int numerator = rand() % 10; int denominator = rand() % 10; if (denominator == 0) { throw DivisionByZeroException(); } float quotient = static_cast<float>(numerator) / denominator; printf(" %c%i/%i=%.2f%c ", ch, numerator, denominator, quotient, ch); } printf("\n\n"); mutex.unlock(); // exit critical section }
/*static*/ float Calculator::_PerformOperation(float left, float right, OPERATION_TYPE operation) { if (operation == ADDITION) { return left + right; } if (operation == SUBSTRACTION) { return left - right; } if (operation == MULTIPLICATION) { return left * right; } if (operation == DIVISION) { if (right == 0) throw DivisionByZeroException(); return left / right; } throw 2; }
/** * std::lock_guard<M> implements RAII idiom on mutex resource. * * Note though that the lock_guard object does not manage * the lifetime of the mutex object in any way: the duration of the mutex object * shall extend at least until the destruction of the lock_guard that locks it. * */ void randomCalculate(int size, char ch) { try { std::lock_guard<std::mutex> lock(mutex); // enter critical section for (int i = 0; i < size; ++i) { int numerator = rand() % 10; int denominator = rand() % 10; if (denominator == 0) { throw DivisionByZeroException(); } float quotient = static_cast<float>(numerator) / denominator; printf(" %c%i/%i=%.2f%c ", ch, numerator, denominator, quotient, ch); } printf("\n\n"); } catch (DivisionByZeroException e) { // exit critical section (dtor ~std::lock_guard<>() called) ERR("Exception in thread: %c", ch); } }
/** * std::unique_lock<M> implements RAII idiom on mutex resource. * * Note though that the unique_lock object does not manage * the lifetime of the mutex object in any way: the duration of the mutex object * shall extend at least until the destruction of the lock_guard that locks it. * * std::unique_lock<M> constructor: http://www.cplusplus.com/reference/mutex/unique_lock/unique_lock/ * * See the difference between std::lock_guard<M> and std::unique_lock<M> here: * https://geidav.wordpress.com/2014/01/09/mutex-lock-guards-in-c11/ */ void randomCalculate(int size, char ch) { try { std::unique_lock<std::mutex> lock(mutex, std::defer_lock); // defer locking mutex lock.lock(); // enter critical section // TODO[Quiz]: use std::lock() instead of std::unique_lock<M>::lock() method calling for (int i = 0; i < size; ++i) { int numerator = rand() % 10; int denominator = rand() % 10; if (denominator == 0) { throw DivisionByZeroException(); } float quotient = static_cast<float>(numerator) / denominator; printf(" %c%i/%i=%.2f%c ", ch, numerator, denominator, quotient, ch); } printf("\n\n"); } catch (DivisionByZeroException e) { // exit critical section (dtor ~std::lock_guard<>() called) ERR("Exception in thread: %c", ch); } }
T PostfixExprEvaluator::evalBinaryOp(const T &operand1, const T &operand2, const OpBinary &operation) { T result(0); switch (operation) { case OpBinary::OP_ADD: result = operand1 + operand2; break; case OpBinary::OP_SUBSTRACT: result = operand1 - operand2; break; case OpBinary::OP_MULTIPLY: result = operand1 * operand2; break; case OpBinary::OP_DIVIDE: if (operand2 == 0) { throw DivisionByZeroException(); } result = operand1 / operand2; break; case OpBinary::OP_MODULO: if (typeid(operand1) == typeid(float)) { throw UnsupportedOperationForTypeException(); } else { result = static_cast<int>(operand1) % static_cast<int>(operand2); } break; case OpBinary::OP_POWER: result = pow(operand1, operand2); break; } return result; }
// Function to normalize the vector Vector Vector::normalize(void) { // I create a new vector to store the results Vector result; // Calculating the length using the the formula "square root of ( x^2 + y^2 + z^2 )", a short cut is multiplying the entire vector with itself float magnitude = this->getMagnitude(); // Checking that I am not about to divide by zero... if (magnitude != 0) { // ...if magnitude isn't, I go ahead with normalization result.set(0, this->get(0) / magnitude); result.set(1, this->get(1) / magnitude); result.set(2, this->get(2) / magnitude); } else { // ...if i is, I throw an exception to be handle by whoever is calling this function and are smart enough to use try and catch throw DivisionByZeroException(); } // I return the result return result; }
Fraction::Fraction(int n, int d) throw (DivisionByZeroException) : n(n / gcd(n, d)), d(d / gcd(n, d)) { if (d == 0) throw DivisionByZeroException(); }
Fraction::Fraction(int numerator, int denominator){ if(denominator == 0) throw DivisionByZeroException(); n = numerator; d = denominator; }
Fraction Fraction::operator /(const Fraction & other) const{ if(other.n == 0) throw DivisionByZeroException(); return Fraction(n * other.d, d * other.n); }