Scala Tutorial Scala References

Scala Math Methods



The Scala has package object scala.math which contains a number of methods for performing basic numeric operations such as elementary exponential, logarithmic, root and trigonometric methods. All methods forward to java.lang.Math unless otherwise noted. These methods are listed below. For more details about any one of these, please visit its page.

Scala Math Methods

Power & Logarithmic Methods

MethodsDescription
exp() Returns the exponent of e.
expm1() Returns the exponent of e minus 1, i.e., ex-1.
log() Returns the natural logarithm of a given number.
log10() Returns the base-10 logarithm of a given number.
log1p() Returns the natural logarithm of (1+number), i.e., log(1+number).
sqrt() Returns the square root of the given number.
cbrt() Returns the cube root of the given number.
pow() Returns base raised to the power of exponent.

Trigonometric Methods

MethodsDescription
sin() Returns the trigonometric sine of an angle.
cos() Returns the trigonometric cosine of an angle.
tan() Returns the trigonometric tangent of an angle.
asin() Returns the arc sine of a value.
acos() Returns the arc cosine of a value.
atan() Returns the arc tangent of a value.
atan2() Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).
hypot() Returns square root of sum of squares of two arguments, i.e., sqrt(x2 +y2).

Hyperbolic Methods

MethodsDescription
sinh() Returns the hyperbolic sine of a value.
cosh() Returns the hyperbolic cosine of a value.
tanh() Returns the hyperbolic tangent of a value.

Angular Conversion

MethodsDescription
toDegrees() Returns an angle measured in radians to an approx. equivalent angle measured in degrees.
toRadians() Returns an angle measured in degrees to an approx. equivalent angle measured in radians.

Exact Methods

MethodsDescription
addExact() Returns the sum of its arguments, throws an exception if the result overflows an Int or a Long.
subtractExact() Returns the difference of its arguments, throws an exception if the result overflows an Int or a Long.
multiplyExact() Returns the product of its arguments, throws an exception if the result overflows an Int or a Long.
incrementExact() Returns the argument increased by one, throws an exception if the result overflows an Int or a Long.
decrementExact() Returns the argument decreased by one, throws an exception if the result overflows an Int or a Long.
negateExact() Returns the negation of the argument, throws an exception if the result overflows an Int or a Long.
toIntExact() Returns the value of the Long argument. Throws an exception if the value overflows an Int.

Other Methods

MethodDescription
abs() Returns the absolute (positive) value of the given number.
ceil() Rounds the given number up to the nearest integer.
copySign() Returns a number with magnitude of first argument and sign of second argument.
floor() Rounds the given number down to the nearest integer.
floordiv() Returns the largest integer value that is less than or equal to the algebraic quotient.
floormod() Returns the floor modulus of the arguments.
getExponent(x) Returns the unbiased exponent used in the representation of argument.
IEEEremainder() Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
max() Returns the number with maximum value.
min() Returns the number with minimum value.
nextAfter() Returns the floating-point number adjacent to the first argument in the direction of the second argument.
nextDown() Returns the floating-point value adjacent to argument in the direction of negative infinity.
nextUp() Returns the floating-point value adjacent to argument in the direction of positive infinity.
random() Returns a random number in range [0.0, 1.0).
round() Returns the value of argument rounded to its nearest integer.
rint() Returns the Double value that is closest to the argument and equal to a mathematical integer.
scalb() scalb(a, b) returns a x 2b.
signum() Returns the sign of a given value.
ulp() Returns the size of the unit of least precision (ulp) of the argument.

Scala Math Fields

FieldDescription
E Returns the value of e to the available precision.
PI Returns the value of 𝜋 to the available precision.