473,464 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Strange result with math.atan2()

I think that this results must be the same:

In [3]: math.atan2(-0.0,-1)
Out[3]: -3.1415926535897931

In [4]: math.atan2(-0,-1)
Out[4]: 3.1415926535897931

In [5]: -0 == -0.0
Out[5]: True
This is python 2.4.4c0 on Debian GNU/Linux.
Regards,

Vedran Furač
May 2 '06 #1
5 5344
Vedran Fura? wrote:
I think that this results must be the same:

In [3]: math.atan2(-0.0,-1)
Out[3]: -3.1415926535897931

In [4]: math.atan2(-0,-1)
Out[4]: 3.1415926535897931

In [5]: -0 == -0.0
Out[5]: True


Glimpsing at the hardware formats:
struct.pack("d", 0.0) '\x00\x00\x00\x00\x00\x00\x00\x00' struct.pack("d", -0.0) '\x00\x00\x00\x00\x00\x00\x00\x80' struct.pack("i", -0) '\x00\x00\x00\x00' struct.pack("i", 0)

'\x00\x00\x00\x00'

-0 and +0 integers are identical while 0.0 and -0.0 floats are not. The
negative sign is therefore lost before the int --> float conversion takes
place.

Peter

May 2 '06 #2
Vedran Furač wrote:
I think that this results must be the same:
In [3]: math.atan2(-0.0,-1)
Out[3]: -3.1415926535897931
In [4]: math.atan2(-0,-1)
Out[4]: 3.1415926535897931
-0 is converted to 0, then to 0.0 for calculation, losing the sign. You
might as well write 0.0 instead of -0

The behaviour of atan2 conforms to the ISO C99 standard (Python is
implemented in C). Changing the sign of the first argument changes the
sign of the output, with no special treatment for zero.
In [5]: -0 == -0.0
Out[5]: True


The constant -0 is an integer and is immediately converted to 0 (an
integer). Two's-complement integers have no separate sign bit, and only
one representation for zero. The integer -0 is identical in all respects
to the integer 0

The constant -0.0 is a float, stored as an IEEE 754 bit pattern, and has
a bit used to represent sign. The constants 0.0 and -0.0 compare equal,
and both compare equal to 0, yet 0.0 and -0.0 have distinct bit patterns.

Because Python uses the C implementation of atan2, both arguments are
converted to floats before calculation. -0 is converted to 0, then to 0.0

The behaviour of atan2 is mandated by ISO C99 (ISO/IEC 9899:1999). See
the manuals of some implementers who tabulate these special values:

http://developer.apple.com/documenta...3/atan2.3.html
http://www.ugcs.caltech.edu/manuals/...0/mpfr_22.html

--
Ben Caradoc-Davies <be*@wintersun.org>
http://wintersun.org/
"Those who deny freedom to others deserve it not for themselves."
- Abraham Lincoln
May 2 '06 #3
Ben Caradoc-Davies wrote:
Vedran Furač wrote:
I think that this results must be the same:
In [3]: math.atan2(-0.0,-1)
Out[3]: -3.1415926535897931
In [4]: math.atan2(-0,-1)
Out[4]: 3.1415926535897931


-0 is converted to 0, then to 0.0 for calculation, losing the sign. You
might as well write 0.0 instead of -0

The behaviour of atan2 conforms to the ISO C99 standard (Python is
implemented in C). Changing the sign of the first argument changes the
sign of the output, with no special treatment for zero.

http://www.ugcs.caltech.edu/manuals/...0/mpfr_22.html


Well, here I can read:

Special values are currently handled as described in the ISO C99 standard
for the atan2 function (note this may change in future versions):

* atan2(+0, -0) returns +Pi.
* atan2(-0, -0) returns -Pi. /* wrong too */
* atan2(+0, +0) returns +0.
* atan2(-0, +0) returns -0. /* wrong too */
* atan2(+0, x) returns +Pi for x < 0.
* atan2(-0, x) returns -Pi for x < 0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And the formula (also from that site):
if x < 0, atan2(y, x) = sign(y)*(PI - atan (abs(y/x)))
^^^^^^^

So, you can convert -0 to 0, but you must multiply the result with sign of
y, which is '-' (minus).

Also, octave:

octave2.9:1> atan2(-0,-1)
ans = -3.1416

or matlab:
atan2(-0,-5)


ans =

-3.1416
May 2 '06 #4
Vedran Furac wrote:
Ben Caradoc-Davies wrote:
Vedran Furac wrote:
I think that this results must be the same:
In [3]: math.atan2(-0.0,-1)
Out[3]: -3.1415926535897931
In [4]: math.atan2(-0,-1)
Out[4]: 3.1415926535897931


-0 is converted to 0, then to 0.0 for calculation, losing the sign. You
might as well write 0.0 instead of -0

The behaviour of atan2 conforms to the ISO C99 standard (Python is
implemented in C). Changing the sign of the first argument changes the
sign of the output, with no special treatment for zero.

http://www.ugcs.caltech.edu/manuals/...0/mpfr_22.html


Well, here I can read:

Special values are currently handled as described in the ISO C99 standard
for the atan2 function (note this may change in future versions):

* atan2(+0, -0) returns +Pi.
* atan2(-0, -0) returns -Pi. /* wrong too */
* atan2(+0, +0) returns +0.
* atan2(-0, +0) returns -0. /* wrong too */
* atan2(+0, x) returns +Pi for x < 0.
* atan2(-0, x) returns -Pi for x < 0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And the formula (also from that site):
if x < 0, atan2(y, x) = sign(y)*(PI - atan (abs(y/x)))
^^^^^^^

So, you can convert -0 to 0, but you must multiply the result with sign of
y, which is '-' (minus).


But you miss the fact that 0 is an *integer*, not a float, and -0
doesn't exist.
Use this code until you stop passing integers to atan2:

from math import atan2 as math_atan2
def atan2(y, x):
if (isinstance(y, int) and y == 0) or (
isinstance(x, int) and x == 0):
raise ValueError("Argument that is an integer zero can \
produce wrong results")
return math_atan2(y, x)

print atan2(-0.0, -0.0)
print atan2(-0, -0)

May 2 '06 #5
Serge Orlov wrote:
So, you can convert -0 to 0, but you must multiply the result with sign of
y, which is '-' (minus).


But you miss the fact that 0 is an *integer*, not a float, and -0
doesn't exist.


Yes, you are right, I completely missed that 0 is integer in python, and I
need a float.
Regards,

Vedran Furač
May 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill Stockwell | last post by:
I have upgraded PHP on our server (a RedHat 7.2 box) to version 4.3.5. However, now code I wrote in PHP before fails to work correctly. I have some code that invokes the math function atan2 in...
5
by: Mark | last post by:
See something weird on this result? 632.35 - 632.35 = -1.13686837722E-013 If anyone has seen something similar, please let me know! this a part of the code: $total_due = abs(...
14
by: Allcomp | last post by:
Hello, I have seen something really strange in VB6 If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive 6? Is this a bug or something really "normal". I can see that if I...
24
by: David | last post by:
hello. when doing the simple following computation, the value put into the variable numMinusOne is NOT the same as what the computation is showed to be in the Watch window!! here is the code:...
8
by: seia0106 | last post by:
Hello, I have a C++ program , which has following two lines of code z=atan2(x,y); z=(float)(fmod(2*pi+z, 2*pi); The comments written by the other programmer says that second line is used to...
0
by: han zhiyang | last post by:
I've just studied the "how to" web service and the async pattern in donnet.I make a test with these knowledges,but I got a strange result. Here is my test. 1.Write a simple "Add" service named...
2
by: Webdiyer | last post by:
Hi, We all know that the return value of Math.Log(8,2) is 3,but how about (int)Math.Log(8,2)? On my machine,the return value of (int)Math.Log(8,2) is strange enough! it's not 3 but 2 ! I've...
11
by: Sambo | last post by:
I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j )
10
by: Bernard Liang | last post by:
Consider the following excerpt below, with the included relevant declarations. Firstly, the lines marked with **, ***, **** at the beginning are not supposed to have those stars at the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.