This site requires JavaScript, please enable it in your browser!
Greenfoot back
yolomilk
yolomilk wrote ...

2015/11/12

Problems with Math.sin()

yolomilk yolomilk

2015/11/12

#
Hello I am trying to do some Math and therefore I need sin and cos. I had some issuse getting random numbers with no sence so than I focused on Math.sin() my result was that there are some Numbers that give wrong answers.
1
2
3
4
5
6
public double sinOf(double Grad)
    {
 
        return Math.sin(Grad);
 
    }
sinOf(347); returns as an answer 0.9893626321... also sin347° = -0.224951... sinOf(26); returns 0.762558... also sin26°= 0.438371... I think it is because I want to use Degrese (°) but I don't know how to chance it in the java code so that it is treading it as ° and not as somethink else.
danpost danpost

2015/11/13

#
Yes. The 'sin' method of the Math class requires a value in radians -- not in degrees (as does 'cos' and 'tan').
1
2
int degs = 347;
double rads = (double)degs*Math.PI/180.0;
davmac davmac

2015/11/14

#
(You can also use the 'Math.toRadians()' method, which does the same thing:)
1
return Math.sin(Math.toRadians(Grad));
You need to login to post a reply.