Search⌘ K
AI Features

Built-In Function: Mathematical Operations

Understand and compare built-in mathematical functions in Python and PowerShell to perform common operations like accessing constants, calculating sums, powers, logarithms, and using trigonometric functions. Learn how each language implements these operations to enhance your scripting efficiency.

Built-in functions in a programming language are very useful and can be a huge advantage in day-to-day tasks since we don’t have to write them ourselves . Python has a lot of functions built into it that are always available. Similarly, PowerShell has access to .Net classes that brings some useful functions as well. In this lesson, we are going to cover various built-in functions provided by PowerShell and Python.

Basic Mathematical Operations

We’ll come across various day-to-day tasks where it is required to perform basic mathematical operations. Let’s look into them one at a time.

Constants: PI and E

Mathematical constant Pi = 3.141592 and Euler’s number(e = 2.718281) can be accessed in PowerShell using the System.Math class. This has static properties holding values of these constants as shown in the following example.

Python 3.5
[math]::PI
[math]::E

Likewise, Python has a math module that can be utilized to access the values of these constants.

Python:

Python 3.5
import math
print(math.pi)
print(math.e)

Count and Sum: len() and sum()

PowerShell by default adds a property length to containers like an array. They also have an alias named count that can be utilized to get the count of items in an array.

C++
$array = 1,2,3,4,5
$array.Length
## alternatively
$array.Count

Python, on the other hand, has a built-in function len() that can return the number of items in a container.

Python 3.5
print(len([1,2,3]))

Adding values together in PowerShell is as easy as piping the values to Measure-Object cmdlet with a -Sum switch.

C++
$array = 1,2,3,4,5
($array | Measure-Object -Sum).Sum

Python utilizes sum() function to add items of a list and return the sum.

Python 3.5
array = [1,2,3,4,5]
print(sum(array))

Absolute and Rounded Value: abs() and round()

Python has an inbuilt function abs() that can return the absolute value of a number which is an integer or a floating-point number.

Python 3.5
print(abs(-1.45))
print(abs(-25))

Likewise, in PowerShell, we can use the [math]::Abs() method of the System.Math class to get the absolute values of the number passed as an argument.

C++
[math]::Abs(-1.45)
[math]::Abs(-25)

The System.Math class also provides a Round() method that will return a rounded floating-point number x up to decimal digits n. If n is omitted, then the default value 0 is taken.

Syntax:

[math]::Round(x,n)

Example:

Python 3.5
[math]::Round(1.345)
[math]::Round(1.345, 2)
[math]::Round(1.348, 2)
[math]::Round(1.3489, 3)

Python, on the other hand, has a built-in function round() to return the rounded floating-point values.

Python 3.5
print(round(1.345))
print(round(1.345, 2))
print(round(1.348, 2))
print(round(1.3489, 3))

Maximum and Minimum: max() and min()

Finding maximum and minimum numbers in PowerShell is achieved by using the Measure-Object cmdlet with -Max and -Min switches on an array of items.

Python 3.5
$array = 1,2,3,4,5
$Array | Measure-Object -Maximum -Minimum
# finding maximum number
($Array | Measure-Object -Maximum).Maximum
# finding minimum number
($Array | Measure-Object -Minimum).Minimum

Python has built-in functions max() and min() to find the largest or smallest items in a list.

Python 3.5
array = 1,2,3,4,5
# finding maximum number
print(max(array))
# finding minimum number
print(min(array))

Powers and Square Roots: pow() and sqrt()

PowerShell can access the Pow(x, y) method of the System.Math class to return x to the power y. Both the arguments passed to this function are numerical values.

Python 3.5
[math]::pow(2,3)
[math]::pow(2,4)
[math]::pow(10,10)

Python has a built-in function pow(x, y) to get x to the power of y.

Python 3.5
print(pow(2,3))
print(pow(2,4))
print(pow(10,10))

In PowerShell, we can use the sqrt() function of System.Math class to calculate square roots.

Python 3.5
[math]::sqrt(0)
[math]::sqrt(8)
[math]::sqrt(4.5)

In Python, we have to first import the math module, then access the sqrt() function using the (.) dot operator to return square roots.

Python 3.5
import math
print(math.sqrt(0))
print(math.sqrt(8))
print(math.sqrt(4.5))

Trigonometric Functions: sin(), cos() and tan()

Both languages have inbuilt trigonometric functions to calculate the sine, cosine, and tangent of an angle as a numerical value.

PowerShell:

Python 3.5
[Math]::Sin(90)
[Math]::Cos(90)
[Math]::Tan(30)

Python:

Python 3.5
import math
print(math.sin(90))
print(math.cos(90))
print(math.tan(30))

Logarithm: log() and log10()

Logarithm is a function used to reverse the operation of exponentiation. For example, when the fourth power of 10 equals 10000, then the logarithm of 10000 with respect to base 10 is 4. In PowerShell, we can use the Log10() function from the System.Math class to return the logarithm of any number for base 10. Similarly, math library in Python has a log10() function. Both the languages provide a log() function that can return the logarithm of x to the given base. If the base is not specified, then the natural logarithm (base e) of x is returned. PowerShell:

Python 3.5
# logarithm of X with base 10
[Math]::Log10(10000)
# logarithm of X with base 2
[Math]::Log(10000, 2)
# default base = e
[Math]::Log(10000)
[Math]::Log(10000, [Math]::E)

Python:

Python 3.5
import math
# logarithm of X with base 10
print(math.log10(10000))
# logarithm of X with base 2
print(math.log(10000, 2))
# default base = e
print(math.log(10000))
print(math.log(10000, math.e))

Floor and Ceiling: floor() and ceil()

In Python and PowerShell, the floor(x) function returns the floor of x as an integral, which is the largest integer less-than or equal to x ( <= x ). On the other hand, Python has a ceil(x) function that returns the ceiling of x as an integral, which is the smallest integer greater-than or equal to x ( >= x ). In PowerShell, this function is called Ceiling(x) but provides the same functionality. Powershell:

C++
[Math]::floor(3.14)
[Math]::floor(3.99)
[Math]::ceiling(3.14)
[Math]::ceiling(3.99)

Python:

Python 3.5
import math
print(math.floor(3.14))
print(math.floor(3.99))
print(math.ceil(3.14))
print(math.ceil(3.99))