Java Programming/Basic Arithmetic/Math functions
Navigate Basic Arithmetic topic:
)
|
Beginners Topics: |
Java contains a set of mathematical functions for common operations in the class java.lang.Math. These functions include both the stanard set of functions (e.g. sin, cos, tan, etc.) as well as convenience functions that mirror operations normally performed on integers.
When dealing with mathematical functions, you will want to import the class to get easier access to the functions:
import java.lang.Math;
Contents |
[edit] Basic functions
- abs() returns the absolute value of a number.
- ceil() returns the lowest integer that is equal to or greater than the number. floor() returns the highest ineger equal to or less than the nu,ber.
- rint() and round() round the number to the closest integer.
[edit] Exponents
- pow() returns a number to a given power.
- sqrt() returns a square root.
- cbrt() returns a cuberoot.
- log() and log10() return the natural logarithm and the logarithm to base 10, respectivly. log1p() is equivalent to log(1+x).
- exp() returns e^x. expm1() returns e^x-1.
[edit] Trigonometry
There are ten functions that relate to trigonometry. Three are the classic sin, cos and tan functions, four are the inverses (acos, asin, atan, and atan2), and three are hyperbolic (sinh, cosh, and tanh, which use a hyperbola rather than a circle for calculations.)
As with most other languages, these trigonometric functions use radians as either the parameter or return value. If you need to convert an value to ro from degrees, you can use the toDegrees() and toRadians() functions.
[edit] Comparison functions
You can use max() and min() to compare two values, and return either the maximum or minimum.
[edit] Constants
E is the base of the natural logarithms.
PI is the ratio of a circle's circumference to its diameter.
[edit] Other methods
The math class also provides additional functions that can be used.
- IEEEremainder(a, b), which computes the remainder of a/b.
- getExponent()
- hypot(x,y), which returns sqrt(x^2 + y^2)
- nextAfter()
- nextUp()
- signum(), which returns either 1, 0, or -1 depending on the sign.
- scalb(x,y), which returns x * 2^y
- ulp()
- random()