MathFP - atan2

M

Marek

When I try use function atan2 form MathFP 2.0.5 or 2.0.5 I receive strange
result.
Below you can see test result form C(BC++5.5.1, and Delphi) and java(jdk
1.4)

MathFP:
atan2(toFP(1), toFP(1)) = 0.78539
atan2(toFP(1), toFP(-2)) = 2.67795 <- difference
atan2(toFP(-1), toFP(1)) =-0.78539
atan2(toFP(-1), toFP(-1)) =-2.35619 <- difference

C++/Delphi:
atan2(1, 1) = 0.7854
atan2(1, -2) = -3.6054 <- difference
atan2(-1, 1) = -0.7854
atan2(-1, -1) = -3.9270 <- difference

Can you tell me why? What I do wrong?

Irek
 
P

Paul Lutus

Marek said:
When I try use function atan2 form MathFP 2.0.5 or 2.0.5 I receive strange
result.

The results are not strange, they are correct. The results you posted for
C++/Delphi are wrong.
Below you can see test result form C(BC++5.5.1, and Delphi) and java(jdk
1.4)

MathFP:
atan2(toFP(1), toFP(1)) = 0.78539
atan2(toFP(1), toFP(-2)) = 2.67795 <- difference
atan2(toFP(-1), toFP(1)) =-0.78539
atan2(toFP(-1), toFP(-1)) =-2.35619 <- difference

C++/Delphi:
atan2(1, 1) = 0.7854
atan2(1, -2) = -3.6054 <- difference
atan2(-1, 1) = -0.7854
atan2(-1, -1) = -3.9270 <- difference

Can you tell me why? What I do wrong?

What you did wrong was not post the C++/Delphi code you used to acquire the
results -- those results are wrong.

My Java test program:

public class Test {

public static void main(String[]args) throws Exception
{
double[][] data = {
{1,1},
{1,-2},
{-1,1},
{-1,-1}
};
for(int i = 0;i < data.length;i++) {
double x = data[0];
double y = data[1];
System.out.println(Math.atan2(x,y));
}
}
};

The results:

0.7853981633974483
2.677945044588987
-0.7853981633974483
-2.356194490192345


My C++ program:

#include <iostream>

using namespace std;

int main(int argc,char *argv[])
{
cout << atan2((double)1,(double)1) << endl;
cout << atan2((double)1,(double)-2) << endl;
cout << atan2((double)-1,(double)1) << endl;
cout << atan2((double)-1,(double)-1) << endl;
return 0;
}

The results:

0.785398
2.67795
-0.785398
-2.35619
 
M

Marek

What you did wrong was not post the C++/Delphi code you used to acquire
the
results -- those results are wrong.
Yes, you right. I got mixed up c/Delphi with Java. Sorry

MathFP:
atan2(toFP(1), toFP(1)) = 0.78539
atan2(toFP(1), toFP(-2)) = -3.6054 <- difference
atan2(toFP(-1), toFP(1)) = -0.78539
atan2(toFP(-1), toFP(-1)) = -3.9270 <- difference

C++/Delphi:
atan2(1, 1) = 0.7854
atan2(1, -2) = 2.67795 <- difference
atan2(-1, 1) = -0.7854
atan2(-1, -1) = -2.35619 <- difference

-------------
java:
import net.jscience.math.kvm.MathFP;

public class C1{
C1(){
int a = MathFP.atan2(MathFP.toFP(-1), MathFP.toFP(-1));
System.out.println(MathFP.toString(a));
}
public static void main(String[] arg){
new C1();
}
}

-----------------
C:
#include <iostream.h>
#include <math.h>

int main(){
cout << atan2(-1, -1) << endl;
return 0;
}
 
P

Paul Lutus

Marek said:
Yes, you right. I got mixed up c/Delphi with Java. Sorry

MathFP:
atan2(toFP(1), toFP(1)) = 0.78539
atan2(toFP(1), toFP(-2)) = -3.6054 <- difference
atan2(toFP(-1), toFP(1)) = -0.78539
atan2(toFP(-1), toFP(-1)) = -3.9270 <- difference

Show me the source for the method you are using here. What is toFP()? What
is atan2(), because it is not Math.atan2(). You are asking for advice about
a package you do not provide any information about.

IOW post a complete, short, compilable, working example of your code,
including any external packages.
import net.jscience.math.kvm.MathFP;

Why are you not using the standard Java classes that are provided for this
purpose? Why are you not telling the maintainers of this package that their
code is broken (http://jscience.org)?

As a last resort, you could show us the source for the methods you are
using, so we can tell you how its programmers went wrong. If you cannot get
the source, maybe it would be better if you didn't use this package.
 
M

Marek

Show me the source for the method you are using here. What is toFP()? What
is atan2(), because it is not Math.atan2(). You are asking for advice about
a package you do not provide any information about.
I haven't external package source, only class files.
Why are you not using the standard Java classes that are provided for this
purpose?
Because I develop programs for J2ME. This platfom haven't float point
arithmetic.

Why are you not telling the maintainers of this package that their
They don't answer me
code is broken (http://jscience.org)? Maybe

As a last resort, you could show us the source for the methods you are
using, so we can tell you how its programmers went wrong. If you cannot get
the source, maybe it would be better if you didn't use this package.
I need ArcTan2 function work without float point arithmetic.

irek
 
T

Thomas Richter

Hi,
MathFP:
atan2(toFP(1), toFP(1)) = 0.78539
atan2(toFP(1), toFP(-2)) = -3.6054 <- difference
atan2(toFP(-1), toFP(1)) = -0.78539
atan2(toFP(-1), toFP(-1)) = -3.9270 <- difference

From a mathematical p.o.v, these results are fine. Angles are
only defined mod 2Pi, and -3.6054 = 2.67795 mod 2Pi, so the
result is ok. Typically, one wants atan2 to end up in the interval
(-Pi,Pi], though.

So long,
Thomas
 
B

Babu Kalakrishnan

Marek said:
Yes, you right. I got mixed up c/Delphi with Java. Sorry

MathFP:
atan2(toFP(1), toFP(1)) = 0.78539
atan2(toFP(1), toFP(-2)) = -3.6054 <- difference
atan2(toFP(-1), toFP(1)) = -0.78539
atan2(toFP(-1), toFP(-1)) = -3.9270 <- difference

Are you sure that the value in test case (4) is -3.9270 and not +3.9270
? If it is -3.9270, it is looks like a bug in the library. If positive,
it is mathematically correct (Case 2 is correct mathematically also)

Check the API contract of the atan2 method in the documentation and see
if it guarantees the range of the output. If it doesn't, you might need
to add some code to bring it to a known range (such as say -Pi to +Pi or
0 to 2*Pi)

BK
 
M

Marek

MathFP:
Are you sure that the value in test case (4) is -3.9270 and not +3.9270
? If it is -3.9270, it is looks like a bug in the library. If positive,
it is mathematically correct (Case 2 is correct mathematically also)

[ 1, 1] 0.7854
[ 1, -1] -3.9270
[-1, 1] -0.7854
[-1, -1] -3.9270
[ 1, 2] 0.4638
[ 1, -2] -3.6054
[-1, 2] -0.4638
 
J

Jacob

Babu said:
Are you sure that the value in test case (4) is -3.9270 and not +3.9270
? If it is -3.9270, it is looks like a bug in the library. If positive,
it is mathematically correct (Case 2 is correct mathematically also)

Check the API contract of the atan2 method in the documentation and see
if it guarantees the range of the output. If it doesn't, you might need
to add some code to bring it to a known range (such as say -Pi to +Pi or
0 to 2*Pi)

The numbers are fine as Thomas R. points out.

According to the MathFP docs, it doesn't guarantee the output
interval as far as I can see:

"Compute the principal value of the arc tangent of y/x, using
the signs of both parameters to determine the quadrant of the
return value or inother words computes elementwise the angle
in radian between the positive part of the x-axis and the
line with origin in (0,0) which contains the point (x, y)."

It is most probably a mistake from their side, but not a critical
one.

Depending what the result will be used for, you might need to
rescale them, but typically this is not necessary.
 
B

Babu Kalakrishnan

Marek said:
Are you sure that the value in test case (4) is -3.9270 and not +3.9270
? If it is -3.9270, it is looks like a bug in the library. If positive,
it is mathematically correct (Case 2 is correct mathematically also)


[ 1, 1] 0.7854
[ 1, -1] -3.9270
[-1, 1] -0.7854
[-1, -1] -3.9270
[ 1, 2] 0.4638
[ 1, -2] -3.6054
[-1, 2] -0.4638

I'd file a bug report if I find atan2(1,-1) returning the same value as
atan2(-1,-1).

BK
 
P

Paul Lutus

Marek said:
I haven't external package source, only class files.

Because I develop programs for J2ME. This platfom haven't float point
arithmetic.

Why are you not telling the maintainers of this package that their
They don't answer me

Umm, first, please quote your posts according to convention, and second, if
you cannot get the source, and if the maintainers won't reply to your
inquiries, I strongly advise against using this package. It is a bug, some
may argue not terribly significant, but their unwillingness to reply/react
speaks volumes.
I need ArcTan2 function work without float point arithmetic.

Cal you tell me again wny you cannot use Java's Math package?
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top