472,344 Members | 2,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,344 software developers and data experts.

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?
thanks in advance.
Jul 22 '05 #1
8 5514

"seia0106" <mi*******@yahoo.com> wrote in message
news:4f**************************@posting.google.c om...
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
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?

thanks in advance.
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
kb******@gascad.at
Jul 22 '05 #6

"seia0106" <mi*******@yahoo.com> wrote in message
news:4f**************************@posting.google.c om...
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
Jul 22 '05 #7
John Harrison wrote:

"seia0106" <mi*******@yahoo.com> wrote in message
news:4f**************************@posting.google.c om...
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.


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
kb******@gascad.at
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

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

Similar topics

2
by: Zunbeltz Izaola | last post by:
Hi, I have a problem with % operation. It returns a incorrect value in this case: >>> -2.77555756156e-17%1 1.0 where the returned value...
2
by: Lonnie Princehouse | last post by:
I've been trying to debug this for two days now, and it's a longshot but I'm hoping that someone here might recognize a solution. I've got a C...
6
by: stau | last post by:
Hi! I'm reading a C book, and it says that fmod() returns the remainder of the exact division of it's arguments. Well, in a exact division, the...
2
by: Gintautas | last post by:
I'm trying to play a part of wav file. FSOUND_Sample_Load (0,"T01.wav",FSOUND_NORMAL, 0,0); plays all file FSOUND_Sample_Load...
17
by: joseph.p.doyle | last post by:
This code, compiled with visual studio .NET 2003, double a = 95.022, b = 0.01; printf ("%lf - floor(%lf / %lf) * %lf = %.17lf\n", a, a, b, b, a -...
5
by: Vedran Furač | last post by:
I think that this results must be the same: In : math.atan2(-0.0,-1) Out: -3.1415926535897931 In : math.atan2(-0,-1) Out: 3.1415926535897931...
14
by: Aaron Gray | last post by:
Does anyone have a good fmod() function written in Javascript ? Many thanks in advance, Aaron
7
by: bummerland | last post by:
Hi, I have a problem with the function fmod. Why is fmod(5.7, 0.1) = 0.1 ?? Why is it not 0? tia bummerland
3
by: narutocanada | last post by:
hi atan2 is supposed to return the angle to x-axis when given y and x, I suppose if I take to one full circle, I should get 0-360 degree...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.