atan2 not consisent
Show older comments
I have the following values that I run through the atan2 function:
a(77:78,:)
ans =
-0.250000000000000 0 -0.950000000000000
-0.260000000000000 0 -0.950000000000000
when I put the above array through the atan2 function, the first value is positive and the second is negative,
atan2(a(77:78,2),a(77:78,3))
ans =
3.141592653589793
-3.141592653589793
If I type in each value of the second row of data by hand, the result is differnt for the atan2 function (it is positive):
atan2(0,-0.95)
ans =
3.141592653589793
If I build a test matrix, manually inputing the values, the atan2 results are both positive.
test = [-0.25, 0 , -0.95; -0.26, 0 , -0.95]
test =
-0.250000000000000 0 -0.950000000000000
-0.260000000000000 0 -0.950000000000000
>> atan2(test(1,2),test(1,3))
ans =
3.141592653589793
>> atan2(test(2,2),test(2,3))
ans =
3.141592653589793
Question: Why are the values of atan2 function positive and negative in the first case but then both positive when I type out the values?
Aside: If done separately:
atan2(a(77,2),a(77,3))
ans =
3.141592653589793
>> atan2(a(78,2),a(78,3))
ans =
-3.141592653589793
Thank you
Accepted Answer
More Answers (2)
John D'Errico
on 24 Nov 2020
0 votes
What you don't understand is that just because MATLAB displays a number represented to 15 digits, does not mean it is EXACTLY the same thing as what you typed in.
In fact, typically when MATLAB shows a number as -0.950000000000000, you should expect there is more to that number than meets the eye. Anyway, a double precision number cannot represent the number you have written exactly. Just as you cannot represent the fraction 2/3 exactly in any finite number of decimal digits, you cannot represent the decimal number 0.95 as an exact binary representation. This number would be represented by the infinitely repeating string...
'11110011001100110011001100110011001100110011001100110...'
So when you typed in 0.95, you certainly were typing in a DIFFERENT number than you havd stored in the array a. And that is why you get a differnt result. It is not atan2, but the numbers you passed to atan2.
10 Comments
Bradley Hughes
on 25 Nov 2020
Edited: Bradley Hughes
on 25 Nov 2020
First, I think this answer comes off as kind of condescending ... "What you don't understand"? Really?
Secondly, I think this answer is nonsensical. When you type a number into Matlab, like -.9500000, that's the number. The fact that a rational number like 2/3 is non-terminating, while -.95000 is not, makes for a false analogy.
By the logic you've presented here, NO function would give the same answers when done over and over again. You could try to do ANY function and keep getting different answers, which doesn't happen.
Richard Bag
on 25 Nov 2020
Walter Roberson
on 25 Nov 2020
MATLAB does not not store fractions in decimal. It uses ieee 754 double precision numbers, which are binary rather than decimal. The number 1/10 decimal cannot be exactly represented in binary floating point systems. The numbers that can be exactly represented are 0, together with some extremely small numbers around 10^-304, together with numbers which can be represented as (1+(integer divided by 2^53)) times 2^integer. The scheme can exactly represent integers up to 2^52 but cannot exactly represent 1/10. It can represent fractions that are an integer divided by a power of 2. So it can represent 102/1024 for example. But 1/10 is not m/2^n for any finite integer n m – if it were then 1/10 = m/2^n implies that 2^n = 10*m but 10 factors to 2*5 so you would have to have a number that is only divisible by 2 somehow also be divisible by 5.
Richard Bag
on 25 Nov 2020
Walter Roberson
on 25 Nov 2020
diff(a(77:78,3))
will give you a nonzero answer, showing you a small numeric difference.
Walter Roberson
on 25 Nov 2020
Edited: Walter Roberson
on 25 Nov 2020
(1+(integer divided by 2^53)) times 2^integer.
The 1 part is talking about the set of "normalized" numbers: numbers whose representation starts with a binary 1, followed by up to 52 bits, and the 2^integer permits the "binary point" (binary equivalent of decimal point) to move along that string of binary digits.
For example, decimal 10 is binary 1010 (8+2), which is binary
1.010 000 000 000 000 000 000 000 000 000 000 000 000 * 2^3
which has an exact representation. Decimal 3.14599609375 is 2^1 + 2^0+1/8+1/64+1/256+1/1024+1/2048 which is binary
1.100 100 101 011 000 000 000 000 000 000 000 000 000 * 2^1
and so has an exact representation.
But 1/10 does not have an exact representation and so 0.95 does not have an exact representation. 0.25 on the other hand does have an exact representation -- but 0.26 does not.
A finite decimal system can represent a (length-limited) subset of (integer)*10^integer such as 95*10^(-2) -> 0.95 . It can exactly represent the fraction A/B only when after being reduced to lowest terms, there is an integer such that B exactly divides 10 to an integer.... which when abs(B) > abs(A) requires that B can be factored into only 2's and 5's. 17/50 for example is fine for decimal as it can be written as 34/100 -> 34 * 10^-2 . But 2/7 cannot be written as a finite decimal.
A finite binary system can represent a (length-limited) subset of (integer)*2^integer such as 1100100101011 * 2^-11 -> 3.14599609375 . It can exactly represent the fraction A/B only when after being reduced to lowest terms, there is a integer such that B exactly divides 2 to the integer... which when abs(B) > abs(A) requires that B can be factor into only 2's. 1/10 cannot be written as finite binary. 2/7 cannot be written as finite binary either.
Richard Bag
on 25 Nov 2020
Walter Roberson
on 25 Nov 2020
Look at
format long
a(144:145,2)
I suspect the values are not exact 0, and that fact is being hidden by your use of format short
Bruno Luong
on 25 Nov 2020
Edited: Bruno Luong
on 25 Nov 2020
For sure
a(145,2)
is negative (but close to 0)
"I suspect the values are not exact 0, and that fact is being hidden by your use of format short"
@Walter Roberson: can you please show a one single example of a non-zero value that is displayed as the single digit "0" (as all of Richard Bag's second-column examples in this thread show). I can't find any:
>> format short
>> [0,eps(0),-eps(0),-0.95]
ans =
0 0.0000 -0.0000 -0.9500
>> format long
>> [0,eps(0),-eps(0),-0.95]
ans =
0 0.000000000000000 -0.000000000000000 -0.950000000000000
Bruno Luong
on 25 Nov 2020
Edited: Bruno Luong
on 25 Nov 2020
Your result
atan2(a(77:78,2),a(77:78,3))
ans =
3.141592653589793
-3.141592653589793
Type single value
a(78,2)
you will see it's actually negative (not exactly 0).
Yes atan2(y,x) has discontinuity at the semi axes { y = 0 } for x < 0.
4 Comments
Walter Roberson
on 25 Nov 2020
Did the file get uploaded? I don't seem to see it?
Bruno Luong
on 25 Nov 2020
Edited: Bruno Luong
on 25 Nov 2020
Got you stephen. Good point
Walter Roberson
on 25 Nov 2020
Ah, matlab does consider the sign of the 0 in deciding quadrant.
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!