Math Class

Contains methods for mathematical operations.

System

The following are fields for Math.

  • E

    Returns the mathematical constant e, which is the base of natural logarithms.

  • PI

    Returns the mathematical constant pi, which is the ratio of the circumference of a circle to its diameter.

Returns the mathematical constant e, which is the base of natural logarithms.

public static final Double E

Type: Double

Returns the mathematical constant pi, which is the ratio of the circumference of a circle to its diameter.

public static final Double PI

Type: Double

The following are methods for Math. All methods are static.

  • abs(decimalValue)

    Returns the absolute value of the specified Decimal.

  • abs(doubleValue)

    Returns the absolute value of the specified Double.

  • abs(integerValue)

    Returns the absolute value of the specified Integer.

  • abs(longValue)

    Returns the absolute value of the specified Long.

  • acos(decimalAngle)

    Returns the arc cosine of an angle, in the range of 0.0 through pi.

  • acos(doubleAngle)

    Returns the arc cosine of an angle, in the range of 0.0 through pi.

  • asin(decimalAngle)

    Returns the arc sine of an angle, in the range of -pi/2 through pi/2.

  • asin(doubleAngle)

    Returns the arc sine of an angle, in the range of -pi/2 through pi/2.

  • atan(decimalAngle)

    Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.

  • atan(doubleAngle)

    Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.

  • atan2(xCoordinate, yCoordinate)

    Converts rectangular coordinates (xCoordinate and yCoordinate) to polar (r and theta). This method computes the phase theta by computing an arc tangent of xCoordinate/yCoordinate in the range of -pi to pi.

  • atan2(xCoordinate, yCoordinate)

    Converts rectangular coordinates (xCoordinate and yCoordinate) to polar (r and theta). This method computes the phase theta by computing an arc tangent of xCoordinate/yCoordinate in the range of -pi to pi.

  • cbrt(decimalValue)

    Returns the cube root of the specified Decimal. The cube root of a negative value is the negative of the cube root of that value’s magnitude.

  • cbrt(doubleValue)

    Returns the cube root of the specified Double. The cube root of a negative value is the negative of the cube root of that value’s magnitude.

  • ceil(decimalValue)

    Returns the smallest (closest to negative infinity) Decimal that is not less than the argument and is equal to a mathematical integer.

  • ceil(doubleValue)

    Returns the smallest (closest to negative infinity) Double that is not less than the argument and is equal to a mathematical integer.

  • cos(decimalAngle)

    Returns the trigonometric cosine of the angle specified by decimalAngle.

  • cos(doubleAngle)

    Returns the trigonometric cosine of the angle specified by doubleAngle.

  • cosh(decimalAngle)

    Returns the hyperbolic cosine of decimalAngle. The hyperbolic cosine of d is defined to be (ex + e-x)/2 where e is Euler’s number.

  • cosh(doubleAngle)

    Returns the hyperbolic cosine of doubleAngle. The hyperbolic cosine of d is defined to be (ex + e-x)/2 where e is Euler’s number.

  • exp(exponentDecimal)

    Returns Euler’s number e raised to the power of the specified Decimal.

  • exp(exponentDouble)

    Returns Euler’s number e raised to the power of the specified Double.

  • floor(decimalValue)

    Returns the largest (closest to positive infinity) Decimal that is not greater than the argument and is equal to a mathematical integer.

  • floor(doubleValue)

    Returns the largest (closest to positive infinity) Double that is not greater than the argument and is equal to a mathematical integer.

  • log(decimalValue)

    Returns the natural logarithm (base e) of the specified Decimal.

  • log(doubleValue)

    Returns the natural logarithm (base e) of the specified Double.

  • log10(decimalValue)

    Returns the logarithm (base 10) of the specified Decimal.

  • log10(doubleValue)

    Returns the logarithm (base 10) of the specified Double.

  • max(decimalValue1, decimalValue2)

    Returns the larger of the two specified Decimals.

  • max(doubleValue1, doubleValue2)

    Returns the larger of the two specified Doubles.

  • max(integerValue1, integerValue2)

    Returns the larger of the two specified Integers.

  • max(longValue1, longValue2)

    Returns the larger of the two specified Longs.

  • min(decimalValue1, decimalValue2)

    Returns the smaller of the two specified Decimals.

  • min(doubleValue1, doubleValue2)

    Returns the smaller of the two specified Doubles.

  • min(integerValue1, integerValue2)

    Returns the smaller of the two specified Integers.

  • min(longValue1, longValue2)

    Returns the smaller of the two specified Longs.

  • mod(integerValue1, integerValue2)

    Returns the remainder of integerValue1 divided by integerValue2.

  • mod(longValue1, longValue2)

    Returns the remainder of longValue1 divided by longValue2.

  • pow(doubleValue, exponent)

    Returns the value of the first Double raised to the power of exponent.

  • random()

    Returns a positive Double that is greater than or equal to 0.0 and less than 1.0.

  • rint(decimalValue)

    Returns the value that is closest in value to decimalValue and is equal to a mathematical integer.

  • rint(doubleValue)

    Returns the value that is closest in value to doubleValue and is equal to a mathematical integer.

  • round(doubleValue)

    Do not use. This method is deprecated as of the Winter ‘08 release. Instead, use Math.roundToLong. Returns the closest Integer to the specified Double. If the result is less than -2,147,483,648 or greater than 2,147,483,647, Apex generates an error.

  • round(decimalValue)

    Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using half-even rounding mode, that is, it rounds towards the “nearest neighbor” unless both neighbors are equidistant, in which case, this mode rounds towards the even neighbor. If the result is less than -2,147,483,648 or greater than 2,147,483,647, Apex generates an error.

  • roundToLong(decimalValue)

    Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using half-even rounding mode, that is, it rounds towards the “nearest neighbor” unless both neighbors are equidistant, in which case, this mode rounds towards the even neighbor.

  • roundToLong(doubleValue)

    Returns the closest Long to the specified Double.

  • signum(decimalValue)

    Returns the signum function of the specified Decimal, which is 0 if decimalValue is 0, 1.0 if decimalValue is greater than 0, -1.0 if decimalValue is less than 0.

  • signum(doubleValue)

    Returns the signum function of the specified Double, which is 0 if doubleValue is 0, 1.0 if doubleValue is greater than 0, -1.0 if doubleValue is less than 0.

  • sin(decimalAngle)

    Returns the trigonometric sine of the angle specified by decimalAngle.

  • sin(doubleAngle)

    Returns the trigonometric sine of the angle specified by doubleAngle.

  • sinh(decimalAngle)

    Returns the hyperbolic sine of decimalAngle. The hyperbolic sine of decimalAngle is defined to be (ex - e-x)/2 where e is Euler’s number.

  • sinh(doubleAngle)

    Returns the hyperbolic sine of doubleAngle. The hyperbolic sine of doubleAngle is defined to be (ex - e-x)/2 where e is Euler’s number.

  • sqrt(decimalValue)

    Returns the correctly rounded positive square root of decimalValue.

  • sqrt(doubleValue)

    Returns the correctly rounded positive square root of doubleValue.

  • tan(decimalAngle)

    Returns the trigonometric tangent of the angle specified by decimalAngle.

  • tan(doubleAngle)

    Returns the trigonometric tangent of the angle specified by doubleAngle.

  • tanh(decimalAngle)

    Returns the hyperbolic tangent of decimalAngle. The hyperbolic tangent of decimalAngle is defined to be (ex - e-x)/(ex + e-x) where e is Euler’s number. In other words, it is equivalent to sinh(x)/cosinh(x). The absolute value of the exact tanh is always less than 1.

  • tanh(doubleAngle)

    Returns the hyperbolic tangent of doubleAngle. The hyperbolic tangent of doubleAngle is defined to be (ex - e-x)/(ex + e-x) where e is Euler’s number. In other words, it is equivalent to sinh(x)/cosinh(x). The absolute value of the exact tanh is always less than 1.

Returns the absolute value of the specified Decimal.

public static Decimal abs(Decimal decimalValue)

Type: Decimal

Returns the absolute value of the specified Double.

public static Double abs(Double doubleValue)

Type: Double

Returns the absolute value of the specified Integer.

public static Integer abs(Integer integerValue)

Type: Integer

Returns the absolute value of the specified Long.

public static Long abs(Long longValue)

  • longValue:

    Type: Long

Type: Long

Returns the arc cosine of an angle, in the range of 0.0 through pi.

public static Decimal acos(Decimal decimalAngle)

Type: Decimal

Returns the arc cosine of an angle, in the range of 0.0 through pi.

public static Double acos(Double doubleAngle)

Type: Double

Returns the arc sine of an angle, in the range of -pi/2 through pi/2.

public static Decimal asin(Decimal decimalAngle)

Type: Decimal

Returns the arc sine of an angle, in the range of -pi/2 through pi/2.

public static Double asin(Double doubleAngle)

Type: Double

Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.

public static Decimal atan(Decimal decimalAngle)

Type: Decimal

Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.

public static Double atan(Double doubleAngle)

Type: Double

Converts rectangular coordinates (xCoordinate and yCoordinate) to polar (r and theta). This method computes the phase theta by computing an arc tangent of xCoordinate/yCoordinate in the range of -pi to pi.

public static Decimal atan2(Decimal xCoordinate, Decimal yCoordinate)

Type: Decimal

Converts rectangular coordinates (xCoordinate and yCoordinate) to polar (r and theta). This method computes the phase theta by computing an arc tangent of xCoordinate/yCoordinate in the range of -pi to pi.

public static Double atan2(Double xCoordinate, Double yCoordinate)

Type: Double

Returns the cube root of the specified Decimal. The cube root of a negative value is the negative of the cube root of that value’s magnitude.

public static Decimal cbrt(Decimal decimalValue)

Type: Decimal

Returns the cube root of the specified Double. The cube root of a negative value is the negative of the cube root of that value’s magnitude.

public static Double cbrt(Double doubleValue)

Type: Double

Returns the smallest (closest to negative infinity) Decimal that is not less than the argument and is equal to a mathematical integer.

public static Decimal ceil(Decimal decimalValue)

Type: Decimal

Returns the smallest (closest to negative infinity) Double that is not less than the argument and is equal to a mathematical integer.

public static Double ceil(Double doubleValue)

Type: Double

Returns the trigonometric cosine of the angle specified by decimalAngle.

public static Decimal cos(Decimal decimalAngle)

Type: Decimal

Returns the trigonometric cosine of the angle specified by doubleAngle.

public static Double cos(Double doubleAngle)

Type: Double

Returns the hyperbolic cosine of decimalAngle. The hyperbolic cosine of d is defined to be (ex + e-x)/2 where e is Euler’s number.

public static Decimal cosh(Decimal decimalAngle)

Type: Decimal

Returns the hyperbolic cosine of doubleAngle. The hyperbolic cosine of d is defined to be (ex + e-x)/2 where e is Euler’s number.

public static Double cosh(Double doubleAngle)

Type: Double

Returns Euler’s number e raised to the power of the specified Decimal.

public static Decimal exp(Decimal exponentDecimal)

Type: Decimal

Returns Euler’s number e raised to the power of the specified Double.

public static Double exp(Double exponentDouble)

  • exponentDouble:

    Type: Double

Type: Double

Returns the largest (closest to positive infinity) Decimal that is not greater than the argument and is equal to a mathematical integer.

public static Decimal floor(Decimal decimalValue)

Type: Decimal

Returns the largest (closest to positive infinity) Double that is not greater than the argument and is equal to a mathematical integer.

public static Double floor(Double doubleValue)

Type: Double

Returns the natural logarithm (base e) of the specified Decimal.

public static Decimal log(Decimal decimalValue)

Type: Decimal

Returns the natural logarithm (base e) of the specified Double.

public static Double log(Double doubleValue)

Type: Double

Returns the logarithm (base 10) of the specified Decimal.

public static Decimal log10(Decimal decimalValue)

Type: Decimal

Returns the logarithm (base 10) of the specified Double.

public static Double log10(Double doubleValue)

Type: Double

Returns the larger of the two specified Decimals.

public static Decimal max(Decimal decimalValue1, Decimal decimalValue2)

Type: Decimal

Returns the larger of the two specified Doubles.

public static Double max(Double doubleValue1, Double doubleValue2)

Type: Double

Returns the larger of the two specified Integers.

public static Integer max(Integer integerValue1, Integer integerValue2)

Type: Integer

Returns the larger of the two specified Longs.

public static Long max(Long longValue1, Long longValue2)

  • longValue1:

    Type: Long

  • longValue2:

    Type: Long

Type: Long

Returns the smaller of the two specified Decimals.

public static Decimal min(Decimal decimalValue1, Decimal decimalValue2)

Type: Decimal

Returns the smaller of the two specified Doubles.

public static Double min(Double doubleValue1, Double doubleValue2)

Type: Double

Returns the smaller of the two specified Integers.

public static Integer min(Integer integerValue1, Integer integerValue2)

Type: Integer

Returns the smaller of the two specified Longs.

public static Long min(Long longValue1, Long longValue2)

  • longValue1:

    Type: Long

  • longValue2:

    Type: Long

Type: Long

Returns the remainder of integerValue1 divided by integerValue2.

public static Integer mod(Integer integerValue1, Integer integerValue2)

Type: Integer

Returns the remainder of longValue1 divided by longValue2.

public static Long mod(Long longValue1, Long longValue2)

  • longValue1:

    Type: Long

  • longValue2:

    Type: Long

Type: Long

Returns the value of the first Double raised to the power of exponent.

public static Double pow(Double doubleValue, Double exponent)

Type: Double

Returns a positive Double that is greater than or equal to 0.0 and less than 1.0.

public static Double random()

Type: Double

Returns the value that is closest in value to decimalValue and is equal to a mathematical integer.

public static Decimal rint(Decimal decimalValue)

Type: Decimal

Returns the value that is closest in value to doubleValue and is equal to a mathematical integer.

public static Double rint(Double doubleValue)

Type: Double

Do not use. This method is deprecated as of the Winter ‘08 release. Instead, use Math.roundToLong. Returns the closest Integer to the specified Double. If the result is less than -2,147,483,648 or greater than 2,147,483,647, Apex generates an error.

public static Integer round(Double doubleValue)

Type: Integer

Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using half-even rounding mode, that is, it rounds towards the “nearest neighbor” unless both neighbors are equidistant, in which case, this mode rounds towards the even neighbor. If the result is less than -2,147,483,648 or greater than 2,147,483,647, Apex generates an error.

public static Integer round(Decimal decimalValue)

Type: Integer

Note that this rounding mode statistically minimizes cumulative error when applied repeatedly over a sequence of calculations.

Returns the rounded approximation of this Decimal. The number is rounded to zero decimal places using half-even rounding mode, that is, it rounds towards the “nearest neighbor” unless both neighbors are equidistant, in which case, this mode rounds towards the even neighbor.

public static Long roundToLong(Decimal decimalValue)

Type: Long

Note that this rounding mode statistically minimizes cumulative error when applied repeatedly over a sequence of calculations.

Returns the closest Long to the specified Double.

public static Long roundToLong(Double doubleValue)

Type: Long

Returns the signum function of the specified Decimal, which is 0 if decimalValue is 0, 1.0 if decimalValue is greater than 0, -1.0 if decimalValue is less than 0.

public static Decimal signum(Decimal decimalValue)

Type: Decimal

Returns the signum function of the specified Double, which is 0 if doubleValue is 0, 1.0 if doubleValue is greater than 0, -1.0 if doubleValue is less than 0.

public static Double signum(Double doubleValue)

Type: Double

Returns the trigonometric sine of the angle specified by decimalAngle.

public static Decimal sin(Decimal decimalAngle)

Type: Decimal

Returns the trigonometric sine of the angle specified by doubleAngle.

public static Double sin(Double doubleAngle)

Type: Double

Returns the hyperbolic sine of decimalAngle. The hyperbolic sine of decimalAngle is defined to be (ex - e-x)/2 where e is Euler’s number.

public static Decimal sinh(Decimal decimalAngle)

Type: Decimal

Returns the hyperbolic sine of doubleAngle. The hyperbolic sine of doubleAngle is defined to be (ex - e-x)/2 where e is Euler’s number.

public static Double sinh(Double doubleAngle)

Type: Double

Returns the correctly rounded positive square root of decimalValue.

public static Decimal sqrt(Decimal decimalValue)

Type: Decimal

Returns the correctly rounded positive square root of doubleValue.

public static Double sqrt(Double doubleValue)

Type: Double

Returns the trigonometric tangent of the angle specified by decimalAngle.

public static Decimal tan(Decimal decimalAngle)

Type: Decimal

Returns the trigonometric tangent of the angle specified by doubleAngle.

public static Double tan(Double doubleAngle)

Type: Double

Returns the hyperbolic tangent of decimalAngle. The hyperbolic tangent of decimalAngle is defined to be (ex - e-x)/(ex + e-x) where e is Euler’s number. In other words, it is equivalent to sinh(x)/cosinh(x). The absolute value of the exact tanh is always less than 1.

public static Decimal tanh(Decimal decimalAngle)

Type: Decimal

Returns the hyperbolic tangent of doubleAngle. The hyperbolic tangent of doubleAngle is defined to be (ex - e-x)/(ex + e-x) where e is Euler’s number. In other words, it is equivalent to sinh(x)/cosinh(x). The absolute value of the exact tanh is always less than 1.

public static Double tanh(Double doubleAngle)

Type: Double