how to determine overflow if c(n-2) is not accessible - cpu-architecture

given an n-bit 2's complement adder/subtractor what is the overflow logic. i don't have access to internal circuit design. Only 2 n-bit numbers, sum, add/sub, carry(in), carry(out)

There are a few rules for determining overflow in this case. (Note: this only applies to addition, but treating A - B as A + (-B), you can figure out the rules for subtraction as well).
If the two inputs have different signs, then there's no possibility of overflow.
If they have the same sign, but the output has a different sign, then you have an overflow. For example, if you add two positive numbers but get a negative result, you know it's an overflow (since two positives can't sum to a negative).
In other words, Overflow = (Sign(A) == Sign(B)) and (Sign(Sum) != Sign(A)).

Related

How to determine an Overflow in a 4 bit ripple-carry adder-substractor?

What function that depends by following variablesc (First Operand's sign[0/1], Second Operand's sign[0/1], Result's sign[0/1] and Operation sign[0/1]) can identify an overflow in the 4-bit ripple-carry adder/substractor?
P.S.
An overflow occurs only if:
the sum of two positive numbers yields a negative result, the sum has overflowed.
the sum of two negative numbers yields a positive result, the sum has overflowed.
I only know the method with checking the 2 last carries but it seems that there's another method.
Your PS already contains the correct logic formula written in prose (for addition). Remember that a number is "positive" if its sign bit is zero and the number is negative if its sign bit is one.1 This means you can translate "yields a negative result" to "the operation sign is 1". You can translate the other statements about operands or results to logic conditions in the same way, to finally derive a general boolean formula.
1: I know that zero is neither positive nor negative, but treating zero as positive does no harm in this case.

Floating Point Number Overflow [duplicate]

This question already has an answer here:
How to deal with overflow and underflow?
(1 answer)
Closed 7 years ago.
I have to create a function where I need to square values. I am told that even if the initial value is not too big or is not too small, the value squared might still overflow (returns inf) or underflow (returns 0), and I need to figure out how to prevent that.
My problem is I do not even understand what numbers can cause an overflow when squared when the number itself is not too large.
I thought it might be for the not too small numbers, e.g. 1/3 with repeating decimal numbers, but MATLAB just turns that into 0.3333.
Can someone give me an example of such a number or explain what kind of numbers would cause this?
For underflow, let's consider Planck's constant: 6.626070040e-34
sqrt(6.626070040e-34)
ans =
2.5741e-17
Well, that's apparently not small enough, let's go smaller:
sqrt(6.626070040e-340)
ans =
0
There's your underflow.
Overflow can be seen the same way, just use big numbers:
sqrt(6.626070040e34)
ans =
2.5741e+17
sqrt(6.626070040e340)
ans =
Inf
Underflow means the numbers are too small for MATLAB to handle, overflow means they are too big for MATLAB to handle.
Thanks to #obchardon here are the numbers on my MATLAB R2012a 64bits system:
realmax('double') %//largest allowed double
ans =
1.7977e+308
realmin('double') %//smallest allowed double
ans =
2.2251e-308
Now that we know what the largest possible value is that MATLAB can handle, let's try going below that and square it:
(realmax('double')-10).^2
ans =
Inf
so the number we tried to square here (realmax('double')-10) is allowable by MATLAB, but not squarable.

To find an element with nearest lower and greater value in MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have two sorted matrices A and B. For all values in column 1 of A, how do I find nearest lower and greater value in matrice B? (no treshold)
I would use interp1, but in the opposite way it's normally used. Consider your B matrix a look-up table. You're trying to look up the index of an element given it's value. For example:
% Sample data
B = sort(rand(10,1));
A = sort(rand(5,1));
idx = interp1(B, 1:size(B), A, 'linear', 'extrap');
idx will be a double-precision value that shows the location of each element of A in B. 2.2, for instance, says that the value is between element 2 and element 3. In fact, it's 20% of the way from element 2 to element 3. So floor(idx) is the lower element, and ceil(idx) is the higher.
Caveats: duplicate elements in B will create a problem. And the edge conditions might be messy. You'll have to work those out yourself. See what happens with an A element that's outside the range of B.

What are the pros and cons of starting indices at 1 or another value [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
When I have to work on a portion of an array (elements n to n+m of array A), I never know arrays that I produce from this portion of array A (lets call it B) should start at 1 or n. I could either make B 1) range from elements 1 to m-n or 2) range from n to n+m. On a couple of occasions bugs have been produced from me getting this confused.
If memory is a constraint, then 2) wastes elements 1 to n. On the other hand, it is harder to process A and B together if I do 1) and end up needing to use different indices.
What are the benefits of each method when programming in MatLab?
There are benefits to always start array indexes with zero. While it's a matter of convention, it is a convention that in my experience minimizes error, especially when used in conjunction with closed-below, open-above intervals like [a, b). The length of such an interval is b - a. Since no 1s are involved in the expression, I can't forget to put them in. The interval [0, length) is the whole array: again, no need for 1s. Offsets like [x, x + sublen) also don't need any 1s. With 1-based indexing, combining offsets like x1, x2, x3 requires careful handling of 1s.
While some of these features can be obtained in 1-based indexing with closed-above intervals, not all of them can. When an entire framework adopts the zero-based index, closed-below, open-above convention, like most of C, Python, Java, etc., then you can happily avoid thinking about missing + 1 and - 1 errors in all of your function calls, which is a big help.
I remember having the choice to use any base for an index in Fortran (0, 1, -5, whatever). In the few cases where it was helpful (usually for making indexes symmetric around 0), the same effect could have been achieved by wrapping the array retreival in a function call. Since those cases almost always involved modelling a continuous variable by a grid, I often wanted to make the grid spacing different from 1 and interpolate between the points, too, and for that I absolutely needed to wrap it in a function call anyway.
I didn't know that Matlab gave you a choice (you're talking about Matlab, right?), but it would make sense, given Matlab's relationship to Fortran. The above argument has nothing to do with optimization, but if I'm understanding you right and Matlab fills in unused indexes with some kind of placeholder, then zero-based indexing would avoid that, too (as well as bugs associated with unintentionally using the placeholder as though it were real).

Predicting an overflow when counting combinations

We have the following formula for determining how many combinations C we can pick of size k out of a set of n:
I have written an algorithm which will always give an answer if, of course, the answer falls within the range of the datatype (ulong, in my case), by factorising and cancelling terms on the numerator and denominator during evaluation.
Even though it's quite fast to try to compute C and detect an overflow if the result is too large, it would be better if I could put n and k into a preliminary function which estimates whether the answer will be larger than what ulong can hold. It doesn't have to be exact. If it estimates that a given n and k will not overflow but it does, that's fine - but it should never say this it will overflow if it won't. Ideally this function should be very fast otherwise there is no point in having it - I may as well try and compute C directly and let it overflow.
I was plotting the curve of the nCk for various n's as a function of k to see if I can find a curve which grows at least as fast as C(n, k) but doesn't diverge too far in the range I'm interested in (0..2^64-1) and is computationally easy to evaluate.
I didn't have any luck. Any ideas?
Without seeing the actual code for your algorithm, I can't give you a 100% solution, but your best bet is to develop a heuristic function. By simply finding the smallest value of r for which the final answer to nCr overflows for a variety of n values, you should then be able to analyze the relationship between something like n and the ratio between n and r (n/r), and find a quick to calculate function which would let you know if overflow would occur via regression.
I found that for any n < 68, you should never overflow on the final answer, as 67C33 = 67C34 ~ 1.42x1019 is the largest possible answer, and a ulong holds ~1.84x1019. Similarly, when n > 5000, any r > 5 or n-r < n-5 will certainly overflow. You can tune these cutoffs to your liking, and for all the n values in between them, just calculate n/r and use the regression formula to decide if it will overflow or not.
This might be too much work, but it should at least get you started on the right path.