469,963 Members | 1,188 Online

atan2() and fmod()

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 extend the range of variable z from '-pi to pi' to '-2pi to
2pi'.

I dont fully understand why function fmod() is used here. Can someone
please explain its use in second line?
Jul 22 '05 #1
8 5334 "seia0106" <mi*******@yahoo.com> wrote in message
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 extend the range of variable z from '-pi to pi' to '-2pi to
2pi'.
I think it should say

used to change the range of variable z from '-pi to pi' to '0 to 2pi'.

I dont fully understand why function fmod() is used here. Can someone
please explain its use in second line?

The code is essentially the same as

z = atan2(x,y);
if (z < 0.0)
z += 2*pi;

which strikes me as a lot clearer.

john
Jul 22 '05 #2
> > z=atan2(x,y);
z=(float)(fmod(2*pi+z, 2*pi);
The code is essentially the same as

z = atan2(x,y);
if (z < 0.0)
z += 2*pi;

nope.
while (z<0.0) z+=2*pi;
while (z>2*pi) z-=2*pi;
....which seems a lot slower to me...

;)
-Gernot
Jul 22 '05 #3

"Gernot Frisch" <Me@Privacy.net> schrieb im Newsbeitrag
news:2m************@uni-berlin.de...
z=atan2(x,y);

Sorry, fogot that line... Now if you obviously know z is in range
[-pi; +pi], then
if (z < 0.0)
z += 2*pi;

if of course the best solution.
-Gernot
Jul 22 '05 #4
Hello John,
Thank you for the reply and tip.

If it is possible for your can you please explain a little how the use
of fmod() is changing the ranging from -pi to +pi to '0 to 2pi' now?

Jul 22 '05 #5
seia0106 wrote:

Hello John,
Thank you for the reply and tip.

If it is possible for your can you please explain a little how the use
of fmod() is changing the ranging from -pi to +pi to '0 to 2pi' now?

fmod isn't changing the range.
Look at the first argument given to fmod. There a constant
of 2*pi is added to the number. The result of atan2 can only
be in the range -pi to +pi. So adding 2*pi gives a range
of pi to 3*pi (note that now the boundary of this range
are both positive) and fmod is used to clamp that down
to [0 to 2*pi[, by taking the remainder of the division with
2*pi.

It really is the same as using % on integer

0 % 5 -> 0
1 % 5 -> 1
2 % 5 -> 2
3 % 5 -> 3
4 % 5 -> 4
5 % 5 -> 0
6 % 5 -> 1
7 % 5 -> 2
8 % 5 -> 3
9 % 5 -> 4
10 % 5 -> 0
11 % 5 -> 1
...

Here the division by 5 leaves us with a remainder which by nature is
always in the range 0 to 4. (If you have 23 apples and 5 kids and you
give each kid an equal amount of apples, how many apples are left. The
answer cannot be greater then 4, because of it were 5 or above, each kid
would get an additional apple, which eventually would bring down the number
of apples left to less then 5)

--
Karl Heinz Buchegger
Jul 22 '05 #6

"seia0106" <mi*******@yahoo.com> wrote in message
Hello John,
Thank you for the reply and tip.

If it is possible for your can you please explain a little how the use
of fmod() is changing the ranging from -pi to +pi to '0 to 2pi' now?

Well it's like Gernot explained, fmod(z, 2*pi) does something like this

while (z<0.0)
z+=2*pi;
while (z>=2*pi)
z-=2*pi;

In other words if z is < 0, then 2*pi is added to it until it is >= 0. And
if z is >= 2*pi then 2*pi is subtracted from it until it is < 2*pi.

But since you know z is >= -pi and <= pi this is over the top and just
saying

if (z < 0)
z += 2*pi;

is good enough.

john
Jul 22 '05 #7
John Harrison wrote:

"seia0106" <mi*******@yahoo.com> wrote in message
Hello John,
Thank you for the reply and tip.

If it is possible for your can you please explain a little how the use
of fmod() is changing the ranging from -pi to +pi to '0 to 2pi' now?

Well it's like Gernot explained, fmod(z, 2*pi) does something like this

while (z<0.0)
z+=2*pi;
while (z>=2*pi)
z-=2*pi;

In other words if z is < 0, then 2*pi is added to it until it is >= 0. And
if z is >= 2*pi then 2*pi is subtracted from it until it is < 2*pi.

You might want to try

fmod( -3.5, 2 )

the result is -1.5.

The crucial step in the OP's code is the addition of 2*pi to z which ensures
that the value passed to fmod is always positive. fmod then is used to
clamp that value to [0 .. 2*pi[

fmod(a,b) <==> a - ((int)(a/b)) * b

--
Karl Heinz Buchegger
Jul 22 '05 #8
>
You might want to try

fmod( -3.5, 2 )

the result is -1.5.

Yes my mistake, I had a vague feeling that something was wrong when I
posted.

john
Jul 22 '05 #9

 2 posts views Thread by Zunbeltz Izaola | last post: by 2 posts views Thread by Lonnie Princehouse | last post: by 6 posts views Thread by stau | last post: by 2 posts views Thread by Gintautas | last post: by 17 posts views Thread by joseph.p.doyle | last post: by 5 posts views Thread by Vedran FuraÄ | last post: by 14 posts views Thread by Aaron Gray | last post: by 7 posts views Thread by bummerland | last post: by 3 posts views Thread by narutocanada | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by lanliddd | last post: by reply views Thread by isladogs | last post: by 1 post views Thread by isladogs | last post: by reply views Thread by Trystan | last post: by 2 posts views Thread by cloudytechi147 | last post: by 2 posts views Thread by elpidahope | last post: by 1 post views Thread by IbrarBarlow | last post: by 1 post views Thread by mscomx | last post: by