Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Java – Find Trigonometric Cosine – cos() Method with Examples

java.lang.Math.cos() method is used to find the trigonometric cosine of an angle of a double value in Java, (an angle must be in radians), for the given input (x – parameter).
Note: The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

 

cos() method exists in Math class of java.lang package.
Since: JDK1.0

 

Declaration of cos() Method

The declaration of cos() method is:

 

Syntax of cos() Method

The syntax of cos() method in Java is:

Example for Syntax of cos() Method

Output:
0.7071067811865476

Another example for Syntax of cos() Method

By importing java.lang package the syntax of cos() method can be used as:

Output:
0.7071067811865476

 

Special Cases of cos() Method in Java

If the argument is NaN or an infinity, then the result is NaN.
Example

Output:

NaN
NaN
NaN

 

Parameters of cos() Method in Java

x – Where x is any valid double input value. This parameter is required.

 

Error Handling

If the x parameter is not a number cos() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.Example

Output:
Error

 

Return Value of cos() Method in Java

cos() method returns the trigonometric cosine of an angle of a double value for the given input (x – parameter).

 

Java cos() Method Example 1


Output:

The value of trigonometric cosine is: 0.9984971498638638
The value of trigonometric cosine is: 0.25881904510252074
The value of trigonometric cosine is: 1.0

 

Java cos() Method Example 2


Output:

-1.0
-0.9117339147869651
1.0
NaN
-0.5550380793155483
1.0
-0.41392320157395335
1.0
-0.8464080412157756
-0.4202728158729685

 

LearnAndLearn.com © 2018 - All rights reserved