Newer Version Available

This content describes an older version of this product. View Latest

Math Class

Contains methods for mathematical operations.

Namespace

System

Math Fields

The following are fields for Math.

E

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

Signature

public static final Double E

Property Value

Type: Double

PI

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

Signature

public static final Double PI

Property Value

Type: Double

Math Methods

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

abs(Decimal)

Returns the absolute value of the specified Decimal.

Signature

public static Decimal abs(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

abs(Double)

Returns the absolute value of the specified Double.

Signature

public static Double abs(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

abs(Integer)

Returns the absolute value of the specified Integer.

Signature

public static Integer abs(Integer i)

Parameters

i
Type: Integer

Return Value

Type: Integer

Example

1Integer i = -42;
2Integer i2 = math.abs(i);
3system.assertEquals(i2, 42);

abs(Long)

Returns the absolute value of the specified Long.

Signature

public static Long abs(Long l)

Parameters

l
Type: Long

Return Value

Type: Long

acos(Decimal)

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

Signature

public static Decimal acos(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

acos(Double)

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

Signature

public static Double acos(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

asin(Decimal)

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

Signature

public static Decimal asin(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

asin(Double)

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

Signature

public static Double asin(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

atan(Decimal)

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

Signature

public static Decimal atan(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

atan(Double)

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

Signature

public static Double atan(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

atan2(Decimal, Decimal)

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

Signature

public static Decimal atan2(Decimal x, Decimal y)

Parameters

x
Type: Decimal
y
Type: Decimal

Return Value

Type: Decimal

atan2(Double, Double)

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

Signature

public static Double atan2(Double x, Double y)

Parameters

x
Type: Double
y
Type: Double

Return Value

Type: Double

cbrt(Decimal)

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.

Signature

public static Decimal cbrt(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

cbrt(Double)

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.

Signature

public static Double cbrt(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

ceil(Decimal)

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

Signature

public static Decimal ceil(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

ceil(Double)

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

Signature

public static Double ceil(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

cos(Decimal)

Returns the trigonometric cosine of the angle specified by d.

Signature

public static Decimal cos(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

cos(Double)

Returns the trigonometric cosine of the angle specified by d.

Signature

public static Double cos(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

cosh(Decimal)

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

Signature

public static Decimal cosh(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

cosh(Double)

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

Signature

public static Double cosh(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

exp(Decimal)

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

Signature

public static Decimal exp(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

exp(Double)

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

Signature

public static Double exp(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

floor(Decimal)

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

Signature

public static Decimal floor(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

floor(Double)

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

Signature

public static Double floor(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

log(Decimal)

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

Signature

public static Decimal log(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

log(Double)

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

Signature

public static Double log(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

log10(Decimal)

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

Signature

public static Decimal log10(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

log10(Double)

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

Signature

public static Double log10(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

max(Decimal, Decimal)

Returns the larger of the two specified Decimals.

Signature

public static Decimal max(Decimal d1, Decimal d2)

Parameters

d1
Type: Decimal
d2
Type: Decimal

Return Value

Type: Decimal

Example

1Decimal larger = math.max(12.3, 156.6);
2system.assertEquals(larger, 156.6);

max(Double, Double)

Returns the larger of the two specified Doubles.

Signature

public static Double max(Double d1, Double d2)

Parameters

d1
Type: Double
d2
Type: Double

Return Value

Type: Double

max(Integer, Integer)

Returns the larger of the two specified Integers.

Signature

public static Integer max(Integer i1, Integer i2)

Parameters

i1
Type: Integer
i2
Type: Integer

Return Value

Type: Integer

max(Long, Long)

Returns the larger of the two specified Longs.

Signature

public static Long max(Long l1, Long l2)

Parameters

l1
Type: Long
l2
Type: Long

Return Value

Type: Long

min(Decimal, Decimal)

Returns the smaller of the two specified Decimals.

Signature

public static Decimal min(Decimal d1, Decimal d2)

Parameters

d1
Type: Decimal
d2
Type: Decimal

Return Value

Type: Decimal

Example

1Decimal smaller = math.min(12.3, 156.6);
2system.assertEquals(smaller, 12.3);

min(Double, Double)

Returns the smaller of the two specified Doubles.

Signature

public static Double min(Double d1, Double d2)

Parameters

d1
Type: Double
d2
Type: Double

Return Value

Type: Double

min(Integer, Integer)

Returns the smaller of the two specified Integers.

Signature

public static Integer min(Integer i1, Integer i2)

Parameters

i1
Type: Integer
i2
Type: Integer

Return Value

Type: Integer

min(Long, Long)

Returns the smaller of the two specified Longs.

Signature

public static Long min(Long l1, Long l2)

Parameters

l1
Type: Long
l2
Type: Long

Return Value

Type: Long

mod(Integer, Integer)

Returns the remainder of i1 divided by i2.

Signature

public static Integer mod(Integer i1, Integer i2)

Parameters

i1
Type: Integer
i2
Type: Integer

Return Value

Type: Integer

Example

1Integer remainder = math.mod(12, 2);
2system.assertEquals(remainder, 0);
3
4Integer remainder2 = math.mod(8, 3);
5system.assertEquals(remainder2, 2);

mod(Long, Long)

Returns the remainder of L1 divided by L2.

Signature

public static Long mod(Long L1, Long L2)

Parameters

L1
Type: Long
L2
Type: Long

Return Value

Type: Long

pow(Double, Double)

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

Signature

public static Double pow(Double d, Double exp)

Parameters

d
Type: Double
exp
Type: Double

Return Value

Type: Double

random()

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

Signature

public static Double random()

Return Value

Type: Double

rint(Decimal)

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

Signature

public static Decimal rint(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

rint(Double)

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

Signature

public static Double rint(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

round(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.

Signature

public static Integer round(Double d)

Parameters

d
Type: Double

Return Value

Type: Integer

round(Decimal)

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.

Signature

public static Integer round(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Integer

Usage

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

Example

1Decimal d1 = 4.5;
2Integer i1 = Math.round(d1);
3System.assertEquals(4, i1);
4
5Decimal d2 = 5.5;
6Integer i2 = Math.round(d2);
7System.assertEquals(6, i2);

roundToLong(Decimal)

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.

Signature

public static Long roundToLong(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Long

Usage

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

Example

1Decimal d1 = 4.5;
2Long i1 = Math.roundToLong(d1);
3System.assertEquals(4, i1);
4
5Decimal d2 = 5.5;
6Long i2 = Math.roundToLong(d2);
7System.assertEquals(6, i2);

roundToLong(Double)

Returns the closest Long to the specified Double.

Signature

public static Long roundToLong(Double d)

Parameters

d
Type: Double

Return Value

Type: Long

signum(Decimal)

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

Signature

public static Decimal signum(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

signum(Double)

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

Signature

public static Double signum(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

sin(Decimal)

Returns the trigonometric sine of the angle specified by d.

Signature

public static Decimal sin(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

sin(Double)

Returns the trigonometric sine of the angle specified by d.

Signature

public static Double sin(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

sinh(Decimal)

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

Signature

public static Decimal sinh(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

sinh(Double)

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

Signature

public static Double sinh(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

sqrt(Decimal)

Returns the correctly rounded positive square root of d.

Signature

public static Decimal sqrt(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

sqrt(Double)

Returns the correctly rounded positive square root of d.

Signature

public static Double sqrt(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

tan(Decimal)

Returns the trigonometric tangent of the angle specified by d.

Signature

public static Decimal tan(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

tan(Double)

Returns the trigonometric tangent of the angle specified by d.

Signature

public static Double tan(Double d)

Parameters

d
Type: Double

Return Value

Type: Double

tanh(Decimal)

Returns the hyperbolic tangent of d. The hyperbolic tangent of d 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.

Signature

public static Decimal tanh(Decimal d)

Parameters

d
Type: Decimal

Return Value

Type: Decimal

tanh(Double)

Returns the hyperbolic tangent of d. The hyperbolic tangent of d 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.

Signature

public static Double tanh(Double d)

Parameters

d
Type: Double

Return Value

Type: Double