When developing extensions with AL in Microsoft Dynamics 365 Business Central, precise and efficient mathematical operations can be critical. While AL supports basic arithmetic, for more advanced computations, Microsoft provides a specialized module: the Math Codeunit (Codeunit Math).
In this blog, we’ll explore the capabilities of the Math Codeunit, its benefits, and how to use it effectively in real-world AL development for Microsoft Dynamics 365.
📦 What is the Math Codeunit?
The Math Codeunit (usually internal, but accessible from extensions) exposes a variety of mathematical functions such as constants, exponentials, trigonometry, logarithmic calculations, and hyperbolic functions. It’s a robust solution for developers looking to implement complex math logic with precision.
🧠Why Use the Math Codeunit for AL Development?
Here are key reasons to use this built-in module in your Microsoft Dynamics 365 Business Central customizations:
- Consistency: Uses the same internal logic as the platform.
- Readability: Clean and semantic usage of math functions.
- Precision: Handles decimal math properly and reliably.
- Productivity: Saves you from reinventing the wheel.
🔢 Mathematical Constants in AL
PI()
The Pi() method returns the mathematical constant π (approximately 3.14159). It can be used in geometric calculations such as computing the circumference of a circle. In this example, we calculate the circumference of a circle with radius 10 using the formula 2 × π × radius.
📐 Trigonometric Functions in Business Central
Sin ()
The Sin function computes the sine of an angle, which is a fundamental operation in trigonometry. The formula for sine is:
sin(θ)=Oppositehypotenusesin(𝜃)=Oppositehypotenuse
In this case, the angle x is given in radians. The sine function is useful in various fields, including:
Cos()
The Cos() function calculates the cosine of a given angle, and that angle must be in radians.
Radians vs Degrees: To convert degrees to radians, use:
radians = degrees * (π / 180)
Tan()
The Tan function calculates the tangent of an angle. The tangent is a trigonometric function that relates the angle of a right triangle to the ratio of the opposite side over the adjacent side. The tangent function is typically used in various fields, including geometry, physics, and engineering.
- The tangent of 0 radians is 0.
- The tangent of 45 degrees (or π4\frac{\pi}{4}4π radians) is 1.
- The tangent function approaches infinity as the angle approaches 90 degrees
In mathematical terms, the tangent of an angle θ\thetaθ is given by: tan(θ)
=sin(θ)/cos(θ)
The result from the Tan function is measured in decimal format, which provides an approximation of the tangent value for the given angle.
🔁 Hyperbolic Functions in Dynamics 365 AL
Sinh()
The Sinh function calculates the hyperbolic sine of a number, where the formula is:
sinh(θ)=(e^x−e^−x)/2
This function is useful in various scientific and engineering contexts, such as calculations involving waveforms, heat transfer, or electrical engineering.
Cosh()
The Cosh() function returns the hyperbolic cosine, calculated using the formula:
cosh(x) = (e^x + e^(-x)) / 2
Tanh()
The hyperbolic tangent of a number xxx is a mathematical function defined as the ratio of the hyperbolic sine to the hyperbolic cosine. It’s often used in advanced mathematics, engineering, and even machine learning algorithms.
The hyperbolic tangent has the following properties:
- tanh(0)=0tanh(0)=0
- tanh(∞)=1tanh(∞)=1
- tanh(−∞)=−1tanh(−∞)=−1
The result of the Tanh function is typically between -1 and 1.
Mathematically:
tanh(θ)=ex − e−xex + e−xtanh(𝜃)=ex − e−xex + e−x
In the AL example above, the Tanh() function calculates this hyperbolic tangent for various values:
- For x=1x = 1x=1, the result is approximately 0.761594.
- For x=−1x = -1x=−1, the result is approximately -0.761594.
- For x=0x = 0x=0, the result is 0.
- For very large positive or negative values, the result approaches 1 or -1, respectively.
💥Additional Math Functions in Codeunit Math
Sqrt()
The Sqrt function computes the square root of a number. In mathematics, the square root of a number xxx is a value that, when multiplied by itself, gives xxx. For example:
Ready to Build Powerful Extensions?
Whether you’re a developer building custom logic or an enterprise looking to enhance Dynamics 365 Business Central through mathematical precision, understanding the Math Codeunit gives you a strong technical edge.
Explore more AL development tips and tools with Madhda
At Madhda, we help businesses extend and enhance Microsoft Dynamics 365 Business Central through powerful apps, integrations, and consulting.
🚀 Need help implementing custom AL extensions or Business Central solutions?
Let Madhda guide your development with precision and performance.
👉 Connect with our experts now and bring your ideas to life.