// ------------------------------------------------------------ BigInt BigInt::operator+(const BigInt &bi) const { // Perform the addition in columns from LSB to MSB int result[mySize]; int carry = 0; for (int i = mySize - 1; i >= 0; --i) { int add = myVec[i] + bi.myVec[i] + carry; result[i] = add % 10; carry = add / 10; } // At the end of the computation, if 'carry' is non-zero, // it means we need another digit to store the result // (aka, overflow) if (carry > 0) { overflow_error("overflow"); } // Convert vector back to a string ostringstream buf; for (auto i: result) { buf << i; } // And return a new BigInt based on the string return BigInt(buf.str()); }
void FibonacciGen::GetNext(vector<unsigned long long>& seq, unsigned long long const n) { unsigned long long idx = 0; vector<unsigned long long> seqBfr; // Add the seeds into buffer seqBfr.push_back(seqHist[0]); seqBfr.push_back(seqHist[1]); // Generate n more numbers in the sequence while (idx < n) { #ifdef FibonacciGen_OVFL_CHECK try { if (seqBfr[idx + 1] > ULLONG_MAX - seqBfr[idx]) { stringstream strStrm; strStrm << seqBfr[idx] << " + " << seqBfr[idx + 1]; throw overflow_error(strStrm.str()); } seqBfr.push_back(seqBfr[idx] + seqBfr[idx + 1]); } catch (exception& exc) { cerr << "Generator overflows at " << exc.what() << "." << endl; exit(EXIT_FAILURE); } #else seqBfr.push_back(seqBfr[idx] + seqBfr[idx + 1]); #endif ++idx; } // Copy the first n numbers from buffer to output seq.insert(seq.end(), seqBfr.begin(), seqBfr.end()-2); // Save the remaining 2 numbers for the next batch seqHist.clear(); seqHist.insert(seqHist.end(), seqBfr.end()-2, seqBfr.end()); }
bignum& bignum::sum(const bignum& value, bool same_sign) { if(same_sign) { if(value_ == value.value_) { value_.reset(); sign_ = false; } else if(value_ < value.value_) { sign_ = !sign_; value_ = substract(value.value_, value_); } else value_ = substract(value_, value.value_); } else { if(value_.check_adding_overflow(value.value_)) throw_with_nested(overflow_error("number is too big")); value_ = add(value_, value.value_); } return *this; }
void Stack<Type>:: push(const value_type& x){ if(top_of_stack==stack_size-1){ throw overflow_error("Stack overflow"); } data[++top_of_stack]=x; return; }
bignum& bignum::operator++() { if(!sign_ && value_.all()) throw_with_nested(overflow_error("number is too big")); if(value_.none()) sign_ = false; value_.flip_until(sign_); return *this; }
void CBitWriter::WriteUInt32(uint32 value, uint32 size) { if (size > 32) { throw overflow_error("CBitWriter::WriteUInt32(): size"); } if (value >= (1LL << size)) { throw overflow_error("CBitWriter::WriteUInt32(): value"); } uint64 _value = value; _value <<= m_position; const uint8* pData = reinterpret_cast<const uint8*>(&_value); // the first step: we should write the first byte of data m_data |= (*pData); uint32 written = min(cDataSize - m_position, size); m_position += written; ++pData; size -= written; DoFlushIfNeeded(); // the second step: we should write full bytes of data if (size >= cDataSize) { const uint32 count = size / cDataSize; m_buffer.WriteRaw(pData, count); pData += count; size -= count * cDataSize; } // the third step: we should write remained byte of data m_data |= (*pData); m_position += size; assert(m_position < cDataSize); }
inline _LIBCPP_INLINE_VISIBILITY typename enable_if<_Sz == 8, void>::type __check_for_overflow(size_t N) { #ifndef _LIBCPP_NO_EXCEPTIONS if (N > 0xFFFFFFFFFFFFFFC5ull) throw overflow_error("__next_prime overflow"); #else (void)N; #endif }
inline void Undoable::makeUndoRecord(void *start, int size) { ImageDelta d; if (size > ID_MAXSIZE) throw overflow_error("Undoable::makeUndoRecord(): size > max"); d.start = start; d.size = size; memcpy(d.data, start, size); undoRecords.push_back(d); }
/** * @brief This function will be called before the walker enters an element * node. */ void Listener::enterElement () { Key key{ parents.top ().getName (), KEY_END }; if (indices.top () >= UINTMAX_MAX) throw overflow_error ("Unable to increase array index for array “" + key.getName () + "”"); key.addBaseName (indexToArrayBaseName (indices.top ())); uintmax_t index = indices.top (); indices.pop (); index++; indices.push (index); parents.top ().setMeta ("array", key.getBaseName ()); parents.push (key); }
bignum& bignum::operator/=(const bignum& divisor) { auto& divisor_value = divisor.value_; if(divisor_value.none()) throw_with_nested(overflow_error("divisor is 0")); if(value_ < divisor_value) value_.reset(); else value_ = move(bits_divide(value_, divisor_value).first); join_sign(divisor.sign_); return *this; }
void CBitWriter::WriteInt32(__int32 value, unsigned __int32 size) { if (size > 32) { throw overflow_error("CBitWriter::WriteInt32(): size"); } if (value >= 0) { WriteBool(false); WriteUInt32(static_cast<uint32>(value), size - 1); } else { WriteBool(true); WriteUInt32(static_cast<uint32>(-(1 + value)), size - 1); } }
bits_value bignum::multiply(const bits_value& left, const bits_value& right) { bits_value result; bits_value current(left); bits_size_t prev = 0; for(decltype(prev) i = prev; i < capacity; ++i) { if(right.test(i)) { auto shift = i - prev; prev = i; current.left_shift_within_limit(shift); if(result.check_adding_overflow(current)) throw_with_nested(overflow_error("number is too big")); result = add(result, current); } } return move(result); }
void __throw_overflow_error(const char* s) { throw overflow_error(s); }
_UCXXEXPORT void __throw_overflow_error( const char * message){ if(message == 0){ throw overflow_error(); } throw overflow_error(message); }