CodeGym /Blog Jawa /Acak /Metode Math.cos() ing Jawa
John Squirrels
tingkat
San Francisco

# Metode Math.cos() ing Jawa

Kelas matematika ing Jawa ngemot akeh fungsi matematika. Fungsi trigonometri minangka salah sawijining fungsi sing paling penting kanggo pemrograman. Salah sawijining fungsi kasebut yaiku Math.cos() .

## trigonometri kanggo pemrograman?

Mesthine, ana programer sing meh ora tau nemoni fungsi trigonometri ing karyane, nanging, kanggo akeh tugas, fungsi kasebut penting banget. Contone, kanggo grafis komputer utawa logika game. Utamane, sinus lan kosinus melu ing matriks rotasi sing bisa digunakake kanggo muter obyek lan jagad. Lan yen sampeyan kudu ngetung dawa dalan ing peta, fungsi trigonometri bisa migunani.

## Metode Math.cos() ing Jawa

Metode dobel cos (dobel x) saka kelas Math ngasilake nilai kosinus x , ing ngendi x minangka argumen, sudut ing radian . Punika deklarasi Metode Java.lang.Math.cos() :
``````
double cos(double x)
``````
Yen sampeyan ora seneng ngitung sudut ing radian, sampeyan bisa nggunakake fungsi khusus kanggo ngowahi radian dadi derajat:
``````
``````
Ana uga fungsi kuwalik sing ngowahi derajat dadi radian, sing uga bisa migunani.
``````
``````
Punika conto kode java.lang.Math.cos() :
``````
public class CosExample {
public static void main(String[] args) {

int x1 = 1;
double x2 = 0.5;
double x3 = Math.PI;

//using java.lang.Math.cos() for 1, 0.5 and PI rad

System.out.println("cosine of " + x1 + " rads = " + Math.cos(x1));
System.out.println("cosine of  " + x2 + " rads = " + Math.cos(0));
System.out.println("cosine  " + x3 + " rads = " + Math.exp(x3));

//here we declare an 60 degrees angle

double degree = 60;
//here we use Math.toRadians to convert 60 degrees to radians, use the cos() method
//to calculate the cosine of 60 degrees angle and print the result out
System.out.println("cosine of " + degree + " degrees = " + Math.cos(Math.toRadians(degree)));

}
}
``````
Output yaiku:
kosinus 1 rad = 0,5403023058681398 kosinus 0,5 rad = 1,0 kosinus 3,141592653589793 rad = 23,140692632779267 kosinus 60,00000 = 60,00000

## Sawetara kasus khusus

Ing math ana konsep wangun indeterminate, positif lan negatif tanpa wates. Nomer sing dibagi 0,0 menehi infinity, positif utawa negatif gumantung saka positif utawa negatif saka nomer kasebut. Sampeyan bisa njaluk wangun indeterminate ing macem-macem cara. Contone, yen sampeyan nyoba kanggo dibagi nul dening nul utawa tanpa wates kanggo tanpa wates. Ing Jawa ana konstanta khusus saka kelas Double kayata Double.NaN (ora nomer, sampeyan bisa ngomong iku jenis indeterminate wangun), Double.POSITIVE_INFINITY lan Double.NEGATIVE_INFINITY. Metode Math.cos () tumindak kanthi cara tartamtu nalika ngadhepi telung konsep kasebut. Yen argumen kasebut NaN utawa tanpa wates, banjur Math.cos() yaiku NaN. Ayo duwe conto kode:
``````
public class CosSpecialCases {

public static void main(String[] args) {

double positiveInfinity = Double.POSITIVE_INFINITY;
double negativeInfinity = Double.NEGATIVE_INFINITY;
double nan = Double.NaN;

//The argument is positive infinity, the output is NaN
System.out.println(Math.cos(positiveInfinity));

//The argument is negative infinity, the output NaN
System.out.println(Math.cos(negativeInfinity));

//The argument is NaN, the output is NaN
System.out.println(Math.cos(nan));
}
}
``````
Output yaiku:
NaN NaN NaN

## Tugas sinus lan kosinus kanggo pamula

Coba program gerakan jam tangan nggunakake Math.cos () lan Math.sin () . Sampeyan uga bisa masang grafis (nggunakake Processing, JavaFX utawa liyane) kanggo tugas iki lan sampeyan bakal entuk jam animasi.

Wacan liyane:

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION