atan2() and fmod()

S

seia0106

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?
thanks in advance.
 
J

John Harrison

seia0106 said:
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?
thanks in advance.

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
 
G

Gernot Frisch

z=atan2(x,y);
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
 
G

Gernot Frisch

Sorry, fogot that line... Now if you obviously know z is in range
[-pi; +pi], then

if of course the best solution.
-Gernot
 
S

seia0106

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?

thanks in advance.
 
K

Karl Heinz Buchegger

seia0106 said:
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)
 
J

John Harrison

seia0106 said:
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?

thanks in advance.

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
 
K

Karl Heinz Buchegger

John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top