POV-Ray : Newsgroups : povray.documentation.inbuilt : Re: I have a problem with atan2() Server Time10 Aug 2024 09:35:58 EDT (-0400)
 Re: I have a problem with atan2() (Message 1 to 3 of 3)
 From: Cousin Ricky Subject: Re: I have a problem with atan2() Date: 17 Oct 2021 17:46:33 Message: <616c99b9@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#616c99b9%40news.povray.org",
"headline": "Re: I have a problem with atan2()",
"dateCreated": "2021-10-17T21:46:33+00:00",
"datePublished": "2021-10-17T21:46:33+00:00",
"author": {
"@type": "Person",
"name": "Cousin Ricky"
}
}
On 2021-10-17 6:40 AM (-4), kurtz le pirate wrote:
>
> I have a problem with  atan2(a,b) when b = zero.
>
> The documentation on the page
> <http://www.povray.org/documentation/view/3.7.1/228/> says :
>
> Arc-tangent of (A/B). Returns the angle, measured in radians, whose
> tangent is (A/B). **Returns appropriate value even if B is zero**
>
> In my case, i get : Parse Error: Domain error in atan2!

Is your value for A also zero?

> So it's up to me to test if b=zero and return the appropriate value ?

It seems that some people are confused about atan2().

Of course, the inverse tangent of A/0 is undefined, because A/0 is
undefined.  But a simple limits exercise, as well as the very definition
of the tangent of an angle, show that these undefined tangents
correspond to the angles pi/2 and -pi/2, and it is therefore useful to
pretend that they are valid.

The purpose of atan2() is to handle these special cases, by converting a
naive atan()'s argument into an ordered pair, thus allowing the would-be
inverse tangent of A/0 to be computed without having to divide by 0.  A
simple scene file shows that the *only* time atan2() fails with a domain
error is when *both* A and B are zero.  Of course, (0, 0) is an
indeterminate case, for which returning a value would be nonsensical.

----------[BEGIN CODE]----------
#version 3.7;
global_settings { assumed_gamma 1 }
#declare Args = array [9]
{ <0, 1>, <1, 1>, <1, 0>, <1, -1>, <0, -1>,
<-1, -1>, <-1, 0>, <-1, 1>, <0, 0>,
}
#for (I, 0, 8)
#debug concat ("atan2 (", vstr (2, Args[I], ", ", 0, 0), ") = ")
#debug concat (str (atan2 (Args[I].x, Args[I].y), 0, 6), "\n")
#end
-----------[END CODE]-----------

This scene runs fine with the 3rd and 7th arguments; it does not halt
with a parse error until it gets to the last argument.

Perhaps "unless both A and B are zero" could be added to the documentation.
```
 From: kurtz le pirate Subject: Re: I have a problem with atan2() Date: 18 Oct 2021 11:42:41 Message: <616d95f1\$1@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#616d95f1%241%40news.povray.org",
"headline": "Re: I have a problem with atan2()",
"dateCreated": "2021-10-18T15:42:41+00:00",
"datePublished": "2021-10-18T15:42:41+00:00",
"author": {
"@type": "Person",
"name": "kurtz le pirate"
}
}
On 17/10/2021 23:46, Cousin Ricky wrote:

> ...
> Of course, the inverse tangent of A/0 is undefined, because A/0 is
> undefined.  But a simple limits exercise, as well as the very definition
> of the tangent of an angle, show that these undefined tangents
> correspond to the angles pi/2 and -pi/2, and it is therefore useful to
> pretend that they are valid.
>
> The purpose of atan2() is to handle these special cases, by converting a
> naive atan()'s argument into an ordered pair, thus allowing the would-be
> inverse tangent of A/0 to be computed without having to divide by 0.  A
> simple scene file shows that the *only* time atan2() fails with a domain
> error is when *both* A and B are zero.  Of course, (0, 0) is an
> indeterminate case, for which returning a value would be nonsensical.
> ...

Cousin Ricky is right about everything!

And therefore, my problem is when A AND B are equal to zero.

Lot of datas to deal with, the debugging will be long...
(i'm working on the visualization of complex functions with domain coloring)

--
Kurtz le pirate
Compagnie de la Banquise
```
 From: Alain Martel Subject: Re: I have a problem with atan2() Date: 18 Oct 2021 12:25:17 Message: <616d9fed\$1@news.povray.org>
```
{
"@context": "https://schema.org",
"@type": "DiscussionForumPosting",
"@id": "#616d9fed%241%40news.povray.org",
"headline": "Re: I have a problem with atan2()",
"dateCreated": "2021-10-18T16:25:17+00:00",
"datePublished": "2021-10-18T16:25:17+00:00",
"author": {
"@type": "Person",
"name": "Alain Martel"
}
}
Le 2021-10-18 à 11:42, kurtz le pirate a écrit :
> On 17/10/2021 23:46, Cousin Ricky wrote:
>
>> ... Of course, the inverse tangent of A/0 is undefined, because A/0 is
>> undefined.  But a simple limits exercise, as well as the very definition
>> of the tangent of an angle, show that these undefined tangents
>> correspond to the angles pi/2 and -pi/2, and it is therefore useful to
>> pretend that they are valid.
>>
>> The purpose of atan2() is to handle these special cases, by converting a
>> naive atan()'s argument into an ordered pair, thus allowing the would-be
>> inverse tangent of A/0 to be computed without having to divide by 0.  A
>> simple scene file shows that the *only* time atan2() fails with a domain
>> error is when *both* A and B are zero.  Of course, (0, 0) is an
>> indeterminate case, for which returning a value would be nonsensical.
>> ...
>
> Cousin Ricky is right about everything!
>
> And therefore, my problem is when A AND B are equal to zero.
>
> Lot of datas to deal with, the debugging will be long...
> (i'm working on the visualization of complex functions with domain
> coloring)
>
>
>
Add a test to see if both A and B are zero, and replace that with some
small value for B. It could be something like the following just before
the call to atan2 :

#if (A=0 ! B=0) #declare B= 0.001; #end
... atan2(A,B) ...
```