The basic method in Java.lang.Math

Source: Internet
Author: User
Tags cos

The methods provided by the Java.lang.Math class are static, and " static ingestion " makes it unnecessary to write the class name before the method every time the class method is called:
import static java.lang.math.*;
This allows you to simply write the method name when you call the math method, such as:
cos (radians);
----------------------------------------------------------

1, the Basic method:
ABS, Max, Min, ceil, floor, rint, round

Heavy-duty ABS method that returns the absolute value of a number (int, long, float, double)

Overloads Max and Min methods, returns the maximum and minimum values for two numbers (int, long, float, double)

public static double Ceil (double x); Rounding up, returns double

public static double floor (double x); Rounding down, returns double

public static double rint (double x); Returns the nearest integer to x as a double value, assuming an equal distance from x to two integers, returning an even number

public static long round (double x); Returns (long) Math.floor (x+0.5);

public static int round (float x); Returns (int) Math.floor (x+0.5);

2. Exponential and Logarithmic methods:
(MATH.E = 2.7183)
Exp, log, pow, sqrt

3. Trigonometric functions:
Sin, cos, tan, ASIN, ACOS, Atan
Double atan2(double y, double x); The coordinates of the Cartesian coordinate system (x, Y) are converted to coordinates in the polar coordinates (R, Theta), and the angle is returned Thera
public static double Todegrees (double radians);

public static double Toradians (double degree);

4. Random number
0.0 <= Math.random () < 1.0
Suppose you want to get a random integer between [0, 10]:
int number = (int) (Math.random () * 10);


Suppose you want to get a random integer between [50, 100]:

int number = + (int) (Math.random () * 50);

Suppose you want to get a random integer between (A, a+b]:

int number = a + (int) (Math.random () * b);


You can use Math.random () to get a single random number, but to get a series of random numbers, you can use the Java.util.Random class to invoke the corresponding method implementation by creating an object.

The basic method in Java.lang.Math

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.