A utility module that provides mathematical functions.

To use this module, you must import it to your DataWeave code, for example, by adding the line import * from dw::util::Math to the header of your DataWeave script.

Functions

acos

acos(angle: Number): Number | NaN

Returns an arc cosine value that can range from 0.0 through pi.

If the absolute value of the input is greater than 1, the result is null.

Parameters
Name Description

angle

Number to convert into it arc cosine value.

Example

This example shows how acos behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::util::Math
output application/json
---
{
  "acos0": acos(0),
  "acos13": acos(0.13),
  "acos-1": acos(-1),
  "acos1": acos(1),
  "acos1.1": acos(1.1)
}
Output
1
2
3
4
5
6
7
{
   "acos0": 1.5707963267948966,
   "acos13": 1.440427347091751,
   "acos-1": 3.141592653589793,
   "acos1": 0.0,
   "acos1.1": null
 }

asin

asin(angle: Number): Number | NaN

Returns an arc sine value that can range from -pi/2 through pi/2.

If the absolute value of the input is greater than 1, the result is null.

Parameters
Name Description

angle

Number to convert into its arc sine value.

Example

This example shows how asin behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::util::Math
output application/json
---
{
  "asin0": asin(0),
  "asin13": asin(0.13),
  "asin-1": asin(-1),
  "asin1.1": asin(1.1)
}
Output
1
2
3
4
5
6
{
   "asin0": 0.0,
   "asin13": 0.1303689797031455,
   "asin-1": -1.5707963267948966,
   "asin1.1": null
 }

atan

atan(angle: Number): Number

Returns an arc tangent value that can range from -pi/2 through pi/2.

Parameters
Name Description

angle

Number to convert into its arc tangent value.

Example

This example shows how atan behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::util::Math
output application/json
---
{
  "atan0":  atan(0),
  "atan13": atan(0.13),
  "atan-1": atan(-1)
}
Output
1
2
3
4
5
{
   "atan0": 0.0,
   "atan13": 0.12927500404814307,
   "atan-1": -0.7853981633974483
}

cos

cos(angle: Number): Number

Returns the trigonometric cosine of an angle from a given number of radians.

Parameters
Name Description

angle

Number of radians in an angle.

Example

This example shows how cos behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::util::Math
output application/json
---
{
  "cos0": cos(0),
  "cos13": cos(0.13),
  "cos-1": cos(-1)
}
Output
1
2
3
4
5
{
  "cos0": 1.0,
  "cos13": 0.9915618937147881,
  "cos-1": 0.5403023058681398
}

decimalAdd

decimalAdd(lhs: Number, rhs: Number, ctx: OperationContext = DECIMAL_128_CONTEXT): Number

Performs number addition with rounding specified by the operation context

decimalDivide

decimalDivide(dividend: Number, divisor: Number, ctx: OperationContext = DECIMAL_128_CONTEXT): Number

Performs number division with rounding specified by the operation context. If precision is set to 0 (unlimited precision) and the result has an infinite decimal expansion it will error

decimalMultiply

decimalMultiply(leftFactor: Number, rightFactor: Number, ctx: OperationContext = DECIMAL_128_CONTEXT): Number

Performs number multiplication with rounding specified by the operation context

decimalPow

decimalPow(base: Number, exponent: Number, ctx: OperationContext = DECIMAL_128_CONTEXT): Number

Returns a number with value base^exponent with rounding specified by the operation context. Maximum value for exponent is 99999999.

decimalRound

decimalRound(n: Number, ctx: OperationContext = DECIMAL_128_CONTEXT)

Returns the argument number with rounding specified by the operation context

decimalSqrt

decimalSqrt(n: Number, ctx: OperationContext = DECIMAL_128_CONTEXT): Number

Returns a number that approximates the square root of the argument with rounding specified by the operation context

decimalSubtract

decimalSubtract(lhs: Number, rhs: Number, ctx: OperationContext = DECIMAL_128_CONTEXT): Number

Performs number subtraction with rounding specified by the operation context

log10

log10(a: Number): Number | NaN

Returns the logarithm base 10 of a number.

Parameters
Name Description

a

A Number value that serves as input to the function.

Example

This example shows how log10 behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::util::Math
output application/json
---
{
  "log1010": log10(10),
  "log1013": log10(0.13),
  "log10-20": log10(-20)
}
Output
1
2
3
4
5
{
   "log1010": 1.0,
   "log1013": -0.8860566476931632,
   "log10-20": null
}

logn

logn(a: Number): Number | NaN

Returns the natural logarithm (base e) of a number.

If the input value is less than or equal to zero, the result is NaN (or null).

Parameters
Name Description

a

Number to convert into its natural logarithm.

Example

This example shows how logn behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::util::Math
output application/json
---
{
   "logn10":  logn(10),
   "logn13": logn(0.13),
   "logn-20": logn(-20)
}
Output
1
2
3
4
5
{
   "logn10": 2.302585092994046,
   "logn13": -2.0402208285265546,
   "logn-20": null
}

sin

sin(angle: Number): Number

Returns the trigonometric sine of an angle from a given number of radians.

Parameters
Name Description

angle

Number of radians in an angle.

Example

This example shows how sin behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::util::Math
output application/json
---
{
  "sin0": sin(0),
  "sin13": sin(0.13),
  "sin-1": sin(-1)
}
Output
1
2
3
4
5
{
  "sin0": 0.0,
  "sin13": 0.12963414261969486,
  "sin-1": -0.8414709848078965
}

tan

tan(angle: Number): Number

Returns the trigonometric tangent of an angle from a given number of radians.

Parameters
Name Description

angle

Number of radians in an angle.

Example

This example shows how tan behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::util::Math
output application/json
---
{
   "tan0": tan(0),
   "tan13": tan(0.13),
   "tan-1": tan(-1)
}
Output
1
2
3
4
5
{
   "tan0": 0.0,
   "tan13": 0.13073731800446006,
   "tan-1": -1.5574077246549023
 }

toDegrees

toDegrees(angrad: Number): Number

Converts an angle measured in radians to an approximately equivalent number of degrees.

Parameters
Name Description

angrad

Number of radians to convert to degrees.

Example

This example shows how toDegrees behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::util::Math
output application/json
---
{
  "toDegrees0.17":  toDegrees(0.174),
  "toDegrees0": toDegrees(0),
  "toDegrees-20": toDegrees(-0.20)
}
Output
1
2
3
4
5
{
   "toDegrees0.17": 9.969465635276323832571267395889251,
   "toDegrees0": 0E+19,
   "toDegrees-20": -11.45915590261646417536927286883822
 }

toRadians

toRadians(angdeg: Number): Number

Converts a given number of degrees in an angle to an approximately equivalent number of radians.

Parameters
Name Description

angdeg

Number of degrees to convert into radians.

Example

This example shows how toRadians behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::util::Math
output application/json
---
{
  "toRadians10":  toRadians(10),
  "toRadians013": toRadians(0.13),
  "toRadians-20": toRadians(-20)
}
Output
1
2
3
4
5
{
   "toRadians10": 0.1745329251994329576922222222222222,
   "toRadians013": 0.002268928027592628449998888888888889,
   "toRadians-20": -0.3490658503988659153844444444444444
 }

Variables

DECIMAL_128_CONTEXT: OperationContext

A MathContext object with a precision setting matching the precision of the IEEE 754-2019 decimal128 format, 34 digits, and a rounding mode of HALF_EVEN.

E

Variable E sets the value of mathematical constant e, the base of natural logarithms.

PI

Variable PI sets the value of constant value pi, the ratio of the circumference of a circle to its diameter.

UNLIMITED_CONTEXT: OperationContext

A MathContext object whose settings have the values required for unlimited precision arithmetic. The values of the settings are: precision=0 roundingMode=HALF_UP

Types

OperationContext

Defines the configuration under which a math operation will be executed. * precision: specifies number of digits used for the operation. * roundingMode: specifies the strategy used for rounding to the defined precision.

Definition
1
{| precision?: Number, roundingMode?: RoundingMode |}

RoundCeiling

Rounding mode to round towards positive infinity. If the number is positive, behaves as for ROUND_UP; if negative, behaves as for ROUND_DOWN. Note that this rounding mode never decreases the calculated value.

Definition
1
"CEILING"

RoundDown

Rounding mode to round towards zero. Never increments the digit prior to a discarded fraction (i.e., truncates).

Definition
1
"DOWN"

RoundFloor

Rounding mode to round towards negative infinity. If the number is positive, behave as for ROUND_DOWN; if negative, behave as for ROUND_UP.

Definition
1
"FLOOR"

RoundHalfDown

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. Behaves as for ROUND_UP if the discarded fraction is > 0.5; otherwise, behaves as for ROUND_DOWN.

Definition
1
"HALF_DOWN"

RoundHalfEven

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for ROUND_HALF_DOWN if it’s even.

Definition
1
"HALF_EVEN"

RoundHalfUp

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for ROUND_UP if the discarded fraction is ≥ 0.5; otherwise, behaves as for ROUND_DOWN.

Definition
1
"HALF_UP"

RoundUnnecessary

Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

Definition
1
"UNNECESSARY"

RoundUp

Rounding mode to round away from zero. Always increments the digit prior to a nonzero discarded fraction.

Definition
1
"UP"

RoundingMode

Enumeration of rounding strategies available for math operations.

Definition
1
RoundUp | RoundDown | RoundCeiling | RoundFloor | RoundHalfUp | RoundHalfDown | RoundHalfEven | RoundUnnecessary