[Go to site: main page, start]

0% found this document useful (0 votes)
23 views3 pages

Java Math Class Methods Overview

The Java Math class provides a variety of methods for performing mathematical operations on numbers, including functions for trigonometry, logarithms, and basic arithmetic. Each method is described with its functionality and return type, such as abs for absolute value and pow for exponentiation. A comprehensive list of these methods is available in the document.

Uploaded by

G. Netpryse
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Java Math Class Methods Overview

The Java Math class provides a variety of methods for performing mathematical operations on numbers, including functions for trigonometry, logarithms, and basic arithmetic. Each method is described with its functionality and return type, such as abs for absolute value and pow for exponentiation. A comprehensive list of these methods is available in the document.

Uploaded by

G. Netpryse
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Math Methods

The Java Math class has many methods that allows you to perform
mathematical tasks on numbers.

All Math Methods


A list of all Math methods can be found in the table below:

Method Description Return Type

abs(x) Returns the absolute value of x double|float|


int|long

acos(x) Returns the arccosine of x, in radians double


addExact(x, Returns the sum of x and y int|long
y)

asin(x) Returns the arcsine of x, in radians double

atan(x) Returns the arctangent of x as a numeric double


value between -PI/2 and PI/2 radians

atan2(y,x) Returns the angle theta from the double


conversion of rectangular coordinates (x,
y) to polar coordinates (r, theta).

cbrt(x) Returns the cube root of x double

ceil(x) Returns the value of x rounded up to its double


nearest integer

copySign(x, y) Returns the first floating point x with the double|float


sign of the second floating point y

cos(x) Returns the cosine of x (x is in radians) double

cosh(x) Returns the hyperbolic cosine of a double


double value

decrementEx Returns x-1 int|long


act(x)

exp(x) Returns the value of Ex double

expm1(x) Returns ex -1 double

floor(x) Returns the value of x rounded down to double


its nearest integer

floorDiv(x, y) Returns the division between x and y int|long


rounded down

floorMod(x, y) Returns the remainder of a division int|long


between x and y where the result of the
division was rounded down

getExponent( Returns the unbiased exponent used in x int


x)

hypot(x, y) Returns sqrt(x2 +y2) without double


intermediate overflow or underflow

IEEEremainde Computes the remainder operation on x double


r(x, y) and y as prescribed by the IEEE 754
standard

incrementExa Returns x+1 int|double


ct(x)

log(x) Returns the natural logarithm (base E) of double


x

log10(x) Returns the base 10 logarithm of x double

log1p(x) Returns the natural logarithm (base E) of double


the sum of x and 1

max(x, y) Returns the number with the highest double|float|


value int|long

min(x, y) Returns the number with the lowest double|float|


value int|long

multiplyExact( Returns the result of x multiplied with y int|long


x, y)

negateExact( Returns the negation of x int|long


x)

nextAfter(x, Returns the floating point number double|float


y) adjacent to x in the direction of y

nextDown(x) Returns the floating point value adjacent double|float


to x in the negative direction

nextUp(x) Returns the floating point value adjacent double|float


to x in the direction of positive infinity

pow(x, y) Returns the value of x to the power of y double

random() Returns a random number between 0 double


and 1

rint(x) Returns the double value that is closest double


to x and equal to a mathematical integer

round(x) Returns the value of x rounded to its long|int


nearest integer
scalb(x, y) Returns x multiplied by 2 to the power of double|float
y

signum(x) Returns the sign of x double|float

sin(x) Returns the sine of x (x is in radians) double

sinh(x) Returns the hyperbolic sine of a double double


value

sqrt(x) Returns the square root of x double

subtractExact Returns the result of x minus y int|long


(x, y)

tan(x) Returns the tangent of an angle double

tanh(x) Returns the hyperbolic tangent of a double


double value

toDegrees(x) Converts an angle measured in radians double


to an approx. equivalent angle measured
in degrees

toIntExact(x) Converts a long value to an int int

toRadians(x) Converts an angle measured in degrees double


to an approx. angle measured in radians

ulp(x) Returns the size of the unit of least double|float


precision (ulp) of x

Common questions

Powered by AI

The 'subtractExact(x, y)' method is advantageous in scenarios where overflow checking is critical. It throws an ArithmeticException if the subtraction results in an overflow, unlike the simple '-' operator which wraps around the value. This makes it preferable in applications requiring high reliability and correctness in arithmetic calculations involving large numbers .

The 'nextAfter(x, y)' method is significant for precision handling as it returns the floating-point number adjacent to x in the direction of y. This allows for precise adjustments in numerical computations, preventing rounding errors and ensuring correct results in iterative algorithms that depend on precise floating-point operations .

The 'IEEEremainder(x, y)' differs from the standard modulus operation as it returns the remainder of x divided by y in a manner consistent with the IEEE 754 standard. Specifically, the result will be the difference between x and the nearest integer multipler of y. This can be preferred in scenarios requiring computations that adhere strictly to floating-point arithmetic standards, ensuring greater compatibility and precision in scientific calculations .

The 'copySign(x, y)' method changes the sign of the first floating-point number, x, to match the sign of the second floating-point number, y, without altering the magnitude of x. This is particularly useful when needing to control or predict the sign of computation results in complex numerical scenarios where specific sign conformity is required .

The 'pow(x, y)' method offers greater computational efficiency and precision compared to iterative multiplication by using optimized algorithms to compute powers, reducing the potential accumulation of rounding errors. For high values of y, iterative multiplication may introduce significant numerical inaccuracies due to repeated floating-point operations, while 'pow(x, y)' maintains precision through optimized calculations .

The 'addExact(x, y)' method in Java Math is designed to throw an ArithmeticException if integer overflow occurs during the addition of two int or long values. This is in contrast to the regular '+' operator which silently wraps around on overflow .

'multiplyExact(x, y)' handles the risk of integer overflow by throwing an ArithmeticException if the product exceeds the limits of int or long types. While the standard '*' operator may silently produce incorrect wrap-around values, 'multiplyExact' provides a safeguard, making it advantageous for applications demanding numerical accuracy and error handling in large integer computations .

The 'ceil(x)' method rounds the input value x up to the nearest integer, whereas the 'floor(x)' method rounds x down to the nearest integer. For a positive decimal input like 2.7, 'ceil(2.7)' would result in 3.0, while 'floor(2.7)' would result in 2.0. For a negative decimal input like -2.7, 'ceil(-2.7)' would result in -2.0, while 'floor(-2.7)' would result in -3.0 .

'toRadians(x)' is used to convert an angle in degrees to radians, while 'toDegrees(x)' performs the inverse operation. These methods facilitate trigonometric computations where libraries or functions only accept radians, offering seamless conversions for accurate mathematical modeling and geometric calculations in Java applications .

The 'hypot(x, y)' method returns the square root of the sum of squares of x and y, effectively calculating the Euclidean distance from the origin to the point (x, y). This avoids overflow or underflow by not computing the squares directly. It is useful in geometric computations where precise distance measurements between two points are needed without the risk of precision loss .

You might also like