[Java] How to use Math class

Programming study diary

October 31, 2020 A brief summary of the main methods of the Math class that handles numbers and how to use each.

What is a Math class?

A class that summarizes methods for performing numerical processing that can use square roots, exponential functions, logarithmic functions, and so on. The Math class is in the java.lang package, so you don't need to import it. Below is a summary of how to use the main functions of the Math class.

abs method

The abs method is a method that calculates the absolute value.

Basic writing


Absolute value= Math.abs(Target number);

Here, one argument of int type, double type, float type, and long type can be entered. (Return value is the same argument type)

max method and min method

A method for finding the magnitude of a value. Use the max method to find a larger value by comparing numbers, and the min method to find a smaller value.

Basic writing


Large value= Math.max(Value to compare,Another value to compare);
Small value= Math.min(Value to compare,Another value to compare);

Like the abs method, the argument can be one int type, double type, float type, or long type. (Return value is the same argument type)

ceil method, floor method, round method

Use the ceil method to round up, the floor method to round down, and the round method to round.

Basic writing


Rounded up value= Math.ceil(Target number);
Rounded down value= Math.floor(Target number);
Rounded value= Math.round(Target number);

The argument type can be a double type number, and the return value is also a double type.

pow method

The pow method is a method for raising values to the power.

Basic writing


Exponentiation value= Math.pow(bottom,index);

Like the ceil method, floor method, and round method, the argument type can be a double type number, and the return value is also a double type.

sqrt and cbrt methods

The aqrt method is for finding the square root, and the cbrt method is for finding the cube root.

Basic writing


square root= Math.sqrt(Target numerical value);
Cube root= Math.cbrt(Target numerical value);

Like the pow method, the argument type can be a double type number, and the return value is also a double type.

References

Active engineers explain how to use Java's Math class [for beginners] Math class

Recommended Posts

[Java] How to use Math class
How to use java class
[Java] How to use LinkedHashMap class
[Java] How to use the File class
[Java] How to use the HashMap class
How to use Java Scanner class (Note)
[Java] How to use the Calendar class
[Java] How to use Map
[Java] How to use Map
How to use java Optional
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
[Java] How to use FileReader class and BufferedReader class
[Java] How to use Calendar class and Date class
[Java] How to use compareTo method of Date class
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
How to use the wrapper class
[Processing × Java] How to use variables
How to decompile java class files
[JavaFX] [Java8] How to use GridPane
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
How to use Twitter4J
How to use active_hash! !!
How to use hidden_field_tag
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM