Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common methods of the JAVA Math class

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the common methods of the JAVA Math class". In the daily operation, I believe that many people have doubts about the common methods of the JAVA Math class. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what are the common methods of the JAVA Math class?" Next, please follow the editor to study!

Because the basic arithmetic operators in java can not perform more complex mathematical operations, such as trigonometric function, logarithmic operation, exponential operation and so on. So Java provides Math utility classes to complete these complex operations.

In Java, Math class encapsulates commonly used mathematical operations and provides basic mathematical operations, such as exponent, logarithm, square root and trigonometric functions. The Math class is located in the java.lang package, and its constructor is private, so you cannot create objects of the Math class, and all methods in the Math class are class methods that can be called directly through the class name.

Static constant

The Math class contains two static constants, E and PI, whose values are equal to e (natural logarithm) and π (pi), respectively.

Code example:

System.out.println ("value of E constant:" + Math.E)

System.out.println ("value of PI constant:" + Math.PI); value of E constant: value of 2.718281828459045PI constant: 3.141592653589793II, maximum, minimum and absolute values

As shown below:

The method explains that static int abs (int a) returns the absolute value of a static long abs (long a) returns the absolute value of a static float abs (float a) returns the absolute value of a static double abs (double a) returns the absolute value of a (int x static int max) returns the maximum value static double max in x and y (double x double double y) returns the maximum value static long max in x and y (long x long y) returns the maximum value static float max (float x long y) in x and y (float y) returns the maximum values in x and y static int min (int x focus int y) returns the minimum values in x and y static long min (long x Magi long y) returns the minimum values in x and y static double min (double x focus static double min y) returns the minimum values in x and y static float min (float x float y) returns the minimum values in x and y

Code example:

Public class Test02 {

Public static void main (String [] args) {

System.out.println ("larger values of 10 and 20:" + Math.max (10,20))

System.out.println ("smaller values of" 15.6,15: "+ Math.min (15.6,15))

System.out.println (absolute value of "- 12:" + Math.abs (- 12))

}

}

The results are as follows:

Larger values of 10 and 20: smaller values of 2015.6 and 15: absolute values of 15-12: 12.

The table is as follows:

The method states that static double ceil (double a) returns the smallest integer greater than or equal to a static double floor (double a) returns the largest integer static double rint (double a) that is less than or equal to a, and returns the integer value closest to an if there are two integers that are equally close Then the result takes an even number static int round (float a) the parameter is added with 1 double 2 and then returns the integer nearest to the parameter static long round (double a) the parameter is added with 1 stroke 2 and then the integer closest to the parameter is returned, and then forced to be converted to a long integer

Code example:

Import java.util.Scanner

Public class Test03 {

Public static void main (String [] args) {

Scanner input = new Scanner (System.in)

System.outprintln ("Please enter a number:")

Double num = input.nextDouble ()

System.out.println (minimum integer "greater than or equal to" + num + ":" + Math.ceil (num))

System.out.println (maximum integer "less than or equal to" + num + ":" + Math.floor (num))

System.out.println (the nearest integer after adding "+ num +" to 0.5: "+ Math.round (num))

System.out.println (the integer closest to "+ num+": "+ Math.rint (num))

}

}

The results are as follows:

Please enter a number: the smallest integer greater than or equal to 100.0: the largest integer less than or equal to 99.01.The nearest integer after adding 0.5. the nearest integer: 99.04. trigonometric function operation.

As shown below:

The method explains the triangular sine of the return angle of static double sin (double a), the triangular cosine of the return angle of static double cos (double a), the arc sine of a value in the unit of static double asin (double a), the inverse cosine of a value in the range of [- 1 double 1], and the inverse cosine of a value in the range of [- PI/2,PI/2] static double acos (double a). The trigonometric tangent of the return angle in the range [0.0pi] staticdouble tan (double a). The parameter staticdouble atan (double a) returns the arc tangent of a value in radians. The range in [- PI/2,PI/2] staticdouble toDegrees (double angrad) converts the angle expressed by solitudes into approximately equivalent angles expressed by angles staticdouble toRadians (double angdeg) converts the angles expressed by angles into approximately equivalent angles expressed by radians.

Note: the parameters and return values of each method in the above table are of type double, and the parameters are realized in radians instead of angles, where 1 degree is equal to π / 180Radians, so the flat angle is π radians.

Code example:

Public class Test04 {

Public static void main (String [] args) {

System.out.println {"sine of 90 degrees:" + Math.sin (Math.PI/2))

System.out.println ("cosine of 0 degrees:" + Math.cos (0))

System.out.println (arc tangent of "1:" + Math.atan (l))

System.out.println ("Radian value of 120o:" + Math.toRadians (120.0))

}

}

In the previous sample code, because the parameter in Math.sin () is in radians and 90 degrees represents an angle, you need to convert 90 degrees to radians, that is, Math.PI/180*90, so the converted radians are Math.PI/2, and then call the sin () method in the Math class to calculate its sine.

The results are as follows:

Sine of 90 degrees: cosine of 1.00: arc tangent of 1.01: 0.7853981633974483120 arc: 2.0943951023931953, exponential operation

The table is as follows:

The method explains that static double exp (double a) returns the a power of e static double pow (double a double b) returns the base a, the power value static double sqrt (double a) returns the square root of a (double a), the cube root static double log (double a) returns the natural logarithm of a, that is, the value static double log10 (double a) of lna returns the logarithm of base a.

Code example:

Public class Test05 {

Public static void main (String [] args) {

System.out.println ("cube of 4:" + Math.pow (4,3))

System.out.println ("square root of 16:" + Math.sqrt (16))

System.out.println ("10 is the logarithm of base 2:" + Math.log1O (2))

}

}

The results are as follows:

The cubic value of 4: the square root of 64.016: 4.010 is the logarithm of base 2: 0.3010299956639812 so far, the study of "what are the common methods of JAVA Math class" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report