Bookmark Topic Watch Topic
  • New Topic

Trouble with java's sin() method  RSS feed

 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi everyone. I am trying to learn the sin() method for a program I am trying to write. It doesn't seem to be working for me. I have the code pasted below. I have checked the Java API and it seems rather vague. If somebody could help me see what I am doing in error here, I would appreciate it. Thanks



When this code tries to run, it give the following error: "cannot find symbol - method sin(double)"
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
Where are you getting the sin method from? How are you importing it?
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
I read the Java API and saw nothing mentioned about importing anything. Or did I miss it? I thought it was along the same lines as Math.pow(). That works fine without anything being imported, I believe.
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
. . . and that sort of error is not thrown when you try to run the code, but when you try to compile it.
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
So perhaps it should be: Math.sin() ?
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
You can either write Math.sin(1.234) or do a static import; luckily the example quoted there is for the Math class.
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
I got it. Thanks, Campbell. Have a great day.
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
You're welcome
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
I believe I know have the sin() method figured out thanks to the wonderful insight of Campbell. However, I am now trying to implement the calculations, and I getting totally wrong answers. This program is to calculate distance of a projectile based on launch angles as well as (of course) launch velocities(speeds). The formula is as follows:

Here is the two classes:





For example, I know that the first problem should equal 410 feet and by the program calculation it is showing: -2065.08

So.... ...something is wrong here I just don't know what!!
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
I have looked over the calculation(s), and I believe they are correct...

...so finding where the trouble is; well, it's difficult
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Report post to moderator
The lauch angles look they're given in degrees, while the Math.xyz methods use radians. So you need to convert those before passing them to the sine function.
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
Ok. I really don't understand what you are saying here You are saying that Math.pow() and Math.sin() uses radians? How do you know that? Thank you for the tip; I am just looking for more clarification here.
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
And never use a square method; write x * x rather than using pow(x, 2). It will give a performance enhancement, and maybe better precision.

There are methods in the Math class which converts degrees to radians and vice versa.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Report post to moderator
David Barry wrote:You are saying that Math.pow() and Math.sin() uses radians? How do you know that?

That isn't really secret information. It says that in the documentation:

public static double sin(double a)

Returns the trigonometric sine of an angle. Special cases:

* If the argument is NaN or an infinity, then the result is NaN.
* If the argument is zero, then the result is a zero with the same sign as the argument.

The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Parameters:
a - an angle, in radians.
Returns:
the sine of the argument.


Here's a link to the Math class's documentation: java.lang.Math

you should really get into the habit of looking in the API documentation. I really don't see how people can write Java programs without consulting it regularly.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
David Barry wrote:Ok. I really don't understand what you are saying here You are saying that Math.pow() and Math.sin() uses radians? How do you know that? Thank you for the tip; I am just looking for more clarification here.

Go to the java API (here). in the bottom left panel, scroll down to find the Math class, and click on it. The main panel will then have details about the class and all the methods - one of which is the 'sin()' method. You should see this:

static double sin(double a)
Returns the trigonometric sine of an angle.

if you click on the blue 'sin', it will take you to details of the method. There, you will see this:

sin

public static double sin(double a)

Returns the trigonometric sine of an angle. Special cases:

* If the argument is NaN or an infinity, then the result is NaN.
* If the argument is zero, then the result is a zero with the same sign as the argument.

The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Parameters:
a - an angle, in radians.
Returns:
the sine of the argument.

(emphasis mine)

You can read about the pow() method there as well.
 
David Barry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks everybody for the tips and suggestions. However, when I go to use printf() in this output; I run into some trouble. Because when I do this: it prints out all of the values for the distance. I only want ones to go underneath this header:

"MPH 25 deg 30 deg 35 deg 40 deg 45 deg 50 deg 55 deg");

However, it prints the values underneath these and goes on in one big line until it runs out of values. I tried using "%n" but all that does is makes a vertical line of values. Can somebody help here?
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Report post to moderator
The bit about %17.0f will print out a floating-point number taking 17 spaces, with 0 places after the decimal point. As you have noticed, %n inserts a new line. I think you will have to set up some sort of loop which prints out the values for 25 30 35 etc. Not sure I can help much more on this particular point, I am afraid.

And you did say earlier you had read the API, so people presumed you knew Math.sin() takes radians.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!