Can someone explain to me why overflow always occurs when the last and second last carry bit are different? - binary

overflow = c,n āŠ• c,nāˆ’1
I tried it with all four possible cases
c,n c,n-1
-7+2 1001+0010 0 0
7+2 0111+0010 0 1
-7+(-2) 1001+1110 1 0
7+(-2) 0111+1110 1 1
and it seems to work, but can someone explain or prove why?

When adding two numbers with n bits, the result can have n+1 bits. This means that by using n bits for result we cannot represent all of them.
Now, what is an overflow? If we consider a signed number, it will mean that we can extend the number by adding sign bit (MSB) above MSB, and they will be equal. So, when this does not hold (next bit after MSB is not equal to MSB, something that can be detected before truncating the result to n bits), then we say it's overflow.
As is your example, we say that the overflow occurs when after adding two positive number we obtained a negative one (or two negative that gave positive result).
Also, check this answer by Thomas Pornin, I think he explained it wery well.

It's been so long, I have only a vague understanding of what the notation is saying. I'd assume that 'c' is a carry flag, and 'n' is a negative flag. But then what is 'n-1'?
Anyway, I'm guessing your answer pertains to overflow occurring in either direction: from a negative number wrapping over into a positive, and a positive wrapping over into a negative.

Related

Why is over flow occuring in this example?

This is the statement I read online.
If x and y have opposite signs (one is negative, the other is
non-negative), then the sum will never overflow.
But when adding -6 (Binary after two's compliment: 11010) and +9 (Binary : 01001), the answer is overflow: 100011. Which means I was adding two numbers in 5 bits but answer is in 6 bits.
According to the above statement the two numbers with opposite signs will not overflow so then why is this answer an overflow?
Edit: Another question that comes to my mind is that if i am using 5-bits to add -6 and +9 in binary, and the answer I am getting is in 6-bits, then is that an overflow? How can it be detected?

Question marks appear in Distance (similarity measure object)

I am trying to use KLDivergence for measuring similarity between text data. But, although other similarity measures work fine, KLDivergence returns question marks in the result. What can cause this problem?
If any of the attributes has a value of zero, the KLdivergence will produce the missing result (the ?). This is probably because of a division by zero.

How to make randomized numbers but if they are too close, change it

I want to make randomized numbers but if they are too close, I want to make it a reroll or make it a number further away from the the other number or try to make it a little more spread out.
I guess I am not sure what exactly I want :/
Thanks in advance
EDIT: So the reason i am making randomized numbers is for spawning positions for units on sides of the screen so i want units to not be too close to each other essentially to make it look better.
I want to make randomized numbers but if they are too close, I want to
make it a reroll or make it a number further away from the the other
number or try to make it a little more spread out.
A random number generator sometimes produces numbers that are near each other.
If you don't want them so close together, maybe what you need is fixed numbers each with a small random perturbation.
Let's say you choose numbers 10, 20, 30, 40, and 50. Then, you run a random number generator that gives you a number between -3 and +3. You add this random number to each one in turn. This produces numbers that aren't uniform, but they're not too close to one another.
If a re-roll would be sufficient, you could just save the previous value, and then on the next iteration, if the difference between the two numbers is too small, call Math.rand() again. Maybe your sample space is too small?
Get random position
If it's not good (too close to some other or what ever) go to step 1.
There you go! You got valid position. :)
...
Or...pre-calculate all valid positions, place them in some array and use rnd function to get array indexes - what array elements will use. That should be the faster solution (if you have huge number of units)...

How to divide tiny double precision numbers correctly without precision errors?

I'm trying to diagnose and fix a bug which boils down to X/Y yielding an unstable result when X and Y are small:
In this case, both cx and patharea increase smoothly. Their ratio is a smooth asymptote at high numbers, but erratic for "small" numbers. The obvious first thought is that we're reaching the limit of floating point accuracy, but the actual numbers themselves are nowhere near it. ActionScript "Number" types are IEE 754 double-precision floats, so should have 15 decimal digits of precision (if I read it right).
Some typical values of the denominator (patharea):
0.0000000002119123
0.0000000002137313
0.0000000002137313
0.0000000002155502
0.0000000002182787
0.0000000002200977
0.0000000002210072
And the numerator (cx):
0.0000000922932995
0.0000000930474444
0.0000000930582124
0.0000000938123574
0.0000000950458711
0.0000000958000159
0.0000000962901528
0.0000000970442977
0.0000000977984426
Each of these increases monotonically, but the ratio is chaotic as seen above.
At larger numbers it settles down to a smooth hyperbola.
So, my question: what's the correct way to deal with very small numbers when you need to divide one by another?
I thought of multiplying numerator and/or denominator by 1000 in advance, but couldn't quite work it out.
The actual code in question is the recalculate() function here. It computes the centroid of a polygon, but when the polygon is tiny, the centroid jumps erratically around the place, and can end up a long distance from the polygon. The data series above are the result of moving one node of the polygon in a consistent direction (by hand, which is why it's not perfectly smooth).
This is Adobe Flex 4.5.
I believe the problem most likely is caused by the following line in your code:
sc = (lx*latp-lon*ly)*paint.map.scalefactor;
If your polygon is very small, then lx and lon are almost the same, as are ly and latp. They are both very large compared to the result, so you are subtracting two numbers that are almost equal.
To get around this, we can make use of the fact that:
x1*y2-x2*y1 = (x2+(x1-x2))*y2 - x2*(y2+(y1-y2))
= x2*y2 + (x1-x2)*y2 - x2*y2 - x2*(y2-y1)
= (x1-x2)*y2 - x2*(y2-y1)
So, try this:
dlon = lx - lon
dlat = ly - latp
sc = (dlon*latp-lon*dlat)*paint.map.scalefactor;
The value is mathematically the same, but the terms are an order of magnitude smaller, so the error should be an order of magnitude smaller as well.
Jeffrey Sax has correctly identified the basic issue - loss of precision from combining terms that are (much) larger than the final result.
The suggested rewriting eliminates part of the problem - apparently sufficient for the actual case, given the happy response.
You may find, however, that if the polygon becomes again (much) smaller and/or farther away from the origin, inaccuracy will show up again. In the rewritten formula the terms are still quite a bit larger than their difference.
Furthermore, there's another 'combining-large&comparable-numbers-with-different-signs'-issue in the algorithm. The various 'sc' values in subsequent cycles of the iteration over the edges of the polygon effectively combine into a final number that is (much) smaller than the individual sc(i) are. (if you have a convex polygon you will find that there is one contiguous sequence of positive values, and one contiguous sequence of negative values, in non-convex polygons the negatives and positives may be intertwined).
What the algorithm is doing, effectively, is computing the area of the polygon by adding areas of triangles spanned by the edges and the origin, where some of the terms are negative (whenever an edge is traversed clockwise, viewing it from the origin) and some positive (anti-clockwise walk over the edge).
You get rid of ALL the loss-of-precision issues by defining the origin at one of the polygon's corners, say (lx,ly) and then adding the triangle-surfaces spanned by the edges and that corner (so: transforming lon to (lon-lx) and latp to (latp-ly) - with the additional bonus that you need to process two triangles less, because obviously the edges that link to the chosen origin-corner yield zero surfaces.
For the area-part that's all. For the centroid-part, you will of course have to "transform back" the result to the original frame, i.e. adding (lx,ly) at the end.

Math.round() wrong calculation in as3?

Can anyone explain this?
what am I doing wrong?
Round is doing the correct thing. 0.285 cannot be exactly represented as a binary floating point value. As you see, when multiplied by 100 it approximates to 28.4999999... which is less than 28.5, so the value is rounded down.
Math.Round(x:Number) rounds x to the nearest integer value. In your case 28 is the nearest integer value for 28.499999999999996. So here the behavior is correct. What is weird is that 0.285 * 100 is not 28.5, but that is a consequence of the precision of the Number class in as3. Here is a little more information about this and a possible solution:
Innacurate math results
Also you can see this SO question:
Very strange number operation issue
Hope this helps.