CodeGym /ื‘ืœื•ื’ Java /Random-HE /ืฉื™ื˜ืช Math.cos() ื‘-Java
John Squirrels
ืจึธืžึธื”
San Francisco

ืฉื™ื˜ืช Math.cos() ื‘-Java

ืคื•ืจืกื ื‘ืงื‘ื•ืฆื”
ืฉื™ืขื•ืจ ืžืชืžื˜ื™ืงื” ื‘-Java ืžื›ื™ืœ ื”ืจื‘ื” ืคื•ื ืงืฆื™ื•ืช ืžืชืžื˜ื™ื•ืช. ืคื•ื ืงืฆื™ื•ืช ื˜ืจื™ื’ื•ื ื•ืžื˜ืจื™ื•ืช ื”ืŸ ื‘ื™ืŸ ื”ืคื•ื ืงืฆื™ื•ืช ื”ื—ืฉื•ื‘ื•ืช ื‘ื™ื•ืชืจ ืœืชื›ื ื•ืช. ืื—ืช ืžื”ืคื•ื ืงืฆื™ื•ืช ื”ืœืœื• ื”ื™ื Math.cos() .

ื˜ืจื™ื’ื•ื ื•ืžื˜ืจื™ื” ืœืชื›ื ื•ืช?

ื›ืžื•ื‘ืŸ, ื™ืฉื ื ืžืชื›ื ืชื™ื ืฉื›ืžืขื˜ ื•ืœื ื ืชืงืœื™ื ื‘ืคื•ื ืงืฆื™ื•ืช ื˜ืจื™ื’ื•ื ื•ืžื˜ืจื™ื•ืช ื‘ืขื‘ื•ื“ืชื, ืืš ืขื ื–ืืช, ืขื‘ื•ืจ ืžืฉื™ืžื•ืช ืจื‘ื•ืช, ื”ืคื•ื ืงืฆื™ื•ืช ื”ืœืœื• ื—ืฉื•ื‘ื•ืช ื‘ื™ื•ืชืจ. ืœื“ื•ื’ืžื”, ืขื‘ื•ืจ ื’ืจืคื™ืงื” ืžืžื•ื—ืฉื‘ืช ืื• ืœื•ื’ื™ืงื” ืฉืœ ืžืฉื—ืง. ื‘ืžื™ื•ื—ื“, ืกื™ื ื•ืกื™ื ื•ืงื•ืกื™ื ื•ืกื™ื ืžืขื•ืจื‘ื™ื ื‘ืžื˜ืจื™ืฆืช ื”ืกื™ื‘ื•ื‘ ืฉื‘ื” ืืชื” ื™ื›ื•ืœ ืœื”ืฉืชืžืฉ ื›ื“ื™ ืœืกื•ื‘ื‘ ืื•ื‘ื™ื™ืงื˜ื™ื ื•ืขื•ืœืžื•ืช. ื•ืื ืืชื” ืฆืจื™ืš ืœื—ืฉื‘ ืืช ืื•ืจืš ื”ื ืชื™ื‘ ืœืื•ืจืš ื”ืžืคื”, ืคื•ื ืงืฆื™ื•ืช ื˜ืจื™ื’ื•ื ื•ืžื˜ืจื™ื•ืช ื™ื›ื•ืœื•ืช ืœื”ื™ื•ืช ืฉื™ืžื•ืฉื™ื•ืช.

ืฉื™ื˜ืช Math.cos() ื‘-Java

ื”ืฉื™ื˜ื” ื”ื›ืคื•ืœื” cos (ื›ืคื•ืœ x) ืฉืœ ื”ืžื—ืœืงื” Math ืžื—ื–ื™ืจื” ืืช ืขืจืš ื”ืงื•ืกื™ื ื•ืก ืฉืœ ื”- x , ื›ืืฉืจ x ื”ื•ื ืืจื’ื•ืžื ื˜, ื–ื•ื•ื™ืช ื‘ืจื“ื™ืื ื™ื . ื”ื ื” ื”ืฆื”ืจื” ืขืœ ืฉื™ื˜ืช Java.lang.Math.cos() :
double cos(double x)
ืื ืœื ื ื•ื— ืœืš ื‘ื—ื™ืฉื•ื‘ ื–ื•ื•ื™ื•ืช ื‘ืจื“ื™ืื ื™ื, ืชื•ื›ืœ ืœื”ืฉืชืžืฉ ื‘ืคื•ื ืงืฆื™ื” ื”ืžื™ื•ื—ื“ืช ืœื”ืžืจืช ืจื“ื™ืื ื™ื ืœืžืขืœื•ืช:
double toDegrees(double angRad)
ื™ืฉื ื” ื’ื ืคื•ื ืงืฆื™ื” ื”ืคื•ื›ื” ื”ืžืžื™ืจื” ืžืขืœื•ืช ืœืจื“ื™ืื ื™ื, ืฉื’ื ื”ื™ื ื™ื›ื•ืœื” ืœื”ื™ื•ืช ืฉื™ืžื•ืฉื™ืช.
double toRadians(double angDeg)
ื”ื ื” ื“ื•ื’ืžื” ืœืงื•ื“ ืฉืœ 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)));

   }
}
ื”ืคืœื˜ ื”ื•ื:
ืงื•ืกื™ื ื•ืก ืฉืœ 1 ืจืื“ = 0.5403023058681398 ืงื•ืกื™ื ื•ืก ืฉืœ 0.5 ืจืื“ = 1.0 ืงื•ืกื™ื ื•ืก 3.141592653589793 ืจืื“ = 23.140692632779267 ืงื•ืกื™ื ื•ืก ืฉืœ 600.00000 =00.00000

ื›ืžื” ืžืงืจื™ื ืžื™ื•ื—ื“ื™ื

ื‘ืžืชืžื˜ื™ืงื” ื™ืฉ ืžื•ืฉื’ื™ื ืฉืœ ืฆื•ืจื” ื‘ืœืชื™ ืžื•ื’ื“ืจืช, ืื™ื ืกื•ืฃ ื—ื™ื•ื‘ื™ ื•ืฉืœื™ืœื™. ืžืกืคืจ ื—ืœืงื™ 0.0 ื ื•ืชืŸ ืื™ื ืกื•ืฃ, ื—ื™ื•ื‘ื™ ืื• ืฉืœื™ืœื™ ื‘ื”ืชืื ืœื—ื™ื•ื‘ื™ื•ืช ืื• ื”ืฉืœื™ืœื™ื•ืช ืฉืœ ืื•ืชื• ืžืกืคืจ. ืืชื” ื™ื›ื•ืœ ืœืงื‘ืœ ืฆื•ืจื” ื‘ืœืชื™ ืžื•ื’ื“ืจืช ื‘ื“ืจื›ื™ื ืฉื•ื ื•ืช. ืœื“ื•ื’ืžื”, ืื ืชื ืกื” ืœื—ืœืง ืืคืก ื‘ืืคืก ืื• ืื™ื ืกื•ืฃ ืœืื™ื ืกื•ืฃ. ื‘-Java ื™ืฉ ืงื‘ื•ืขื™ื ืžื™ื•ื—ื“ื™ื ืž-class Double ื›ืžื• Double.NaN (ืœื ืžืกืคืจ, ืืคืฉืจ ืœื•ืžืจ ืฉื–ื” ืกื•ื’ ืฉืœ ืฆื•ืจื” ื‘ืœืชื™ ืžื•ื’ื“ืจืช), Double.POSITIVE_INFINITY ื•-Double.NEGATIVE_INFINITY. ืฉื™ื˜ืช Math.cos() ืžืชื ื”ื’ืช ื‘ืฆื•ืจื” ืกืคืฆื™ืคื™ืช ื›ืืฉืจ ื”ื™ื ืžืชืžื•ื“ื“ืช ืขื ืฉืœื•ืฉืช ื”ืžื•ืฉื’ื™ื ื”ืœืœื•. ืื ื”ืืจื’ื•ืžื ื˜ ื”ื•ื NaN ืื• ืื™ื ืกื•ืฃ, ืื– Math.cos() ื”ื•ื NaN. ื‘ื•ื ื ื‘ื™ื ื“ื•ื’ืžื” ืœืงื•ื“:
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));
       }
   }
ื”ืคืœื˜ ื”ื•ื:
NaN NaN NaN

ืžืฉื™ืžืช ืกื™ื ื•ืก ื•ืงื•ืกื™ื ื•ืก ืœืžืชื—ื™ืœื™ื

ื ืกื• ืœืชื›ื ืช ืืช ืชื ื•ืขืช ืžื—ื•ื’ื™ ื”ืฉืขื•ืŸ ื‘ืืžืฆืขื•ืช Math.cos() ื•- Math.sin() . ืืชื” ื™ื›ื•ืœ ื’ื ืœืฆืจืฃ ื’ืจืคื™ืงื” (ื‘ืืžืฆืขื•ืช Processing, JavaFX ืื• ืžืฉื”ื• ืื—ืจ) ืœืžืฉื™ืžื” ื”ื–ื• ื•ืชืงื‘ืœ ืฉืขื•ืŸ ืžื•ื ืคืฉ.

ืงืจื™ืื” ื ื•ืกืคืช:

ื”ืขืจื•ืช
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION