コード例 #1
0
ファイル: BigInt.cpp プロジェクト: matthynes/CS2510
// ------------------------------------------------------------
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());
}
コード例 #2
0
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());
}
コード例 #3
0
ファイル: bignum.cpp プロジェクト: leoli-lijunhui/Projects
	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;
	}
コード例 #4
0
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;
	}
コード例 #5
0
ファイル: bignum.cpp プロジェクト: leoli-lijunhui/Projects
	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;
	}
コード例 #6
0
ファイル: BitWriter.cpp プロジェクト: ifzz/FDK
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);
}
コード例 #7
0
ファイル: hash.cpp プロジェクト: 0u812/emscripten
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
}
コード例 #8
0
ファイル: undoable.cpp プロジェクト: demonlife/infinidb
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);
}
コード例 #9
0
ファイル: listener.cpp プロジェクト: mpranj/libelektra
/**
 * @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);
}
コード例 #10
0
ファイル: bignum.cpp プロジェクト: leoli-lijunhui/Projects
	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;
	}
コード例 #11
0
ファイル: BitWriter.cpp プロジェクト: ifzz/FDK
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);
	}
}
コード例 #12
0
ファイル: bignum.cpp プロジェクト: leoli-lijunhui/Projects
	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);
	}
コード例 #13
0
ファイル: throw_wrappers.cpp プロジェクト: zmyer/infer
void __throw_overflow_error(const char* s) { throw overflow_error(s); }
コード例 #14
0
_UCXXEXPORT void __throw_overflow_error( const char * message){
	if(message == 0){
		throw overflow_error();
	}
	throw overflow_error(message);
}