In this article you will learn Visual Basic Math and Non-math functions: Abs function, Exp function, Fix function,. Int function, Log function, Rnd function and Round function. The syntax that contains Math are Math functions why the ones without Math are Non-math functions.
In order to perform more complex mathematical calculations, we need to use the built-in math functions in Visual Basic. There are numerous built-in mathematical functions in Visual Basic which will be introduced one by one in this article.
The Abs function returns the absolute value of a given number. The syntax is
Math.Abs (number)
The Math keyword in the syntax indicates that the Abs function belongs to the Math class. But note, not all mathematical functions belong to the Math class.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num As Integer
Dim ans As Double
num = TextBox1.Text
ans = Math.Abs(num)
Label1.Text = ans
End Sub
The Exp of a number x is the exponential value of x, i.e. ex . For example, Exp(1)=e=2.71828182
Its syntax is written as
Math.Exp (number)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num As Integer
Dim ans As Double
num = TextBox1.Text
ans = Math.Exp(num)
Label1.Text = ans
End Sub
The Fix function truncates the decimal part of a positive number and returns the largest integer smaller than the number. However, when the number is negative, it will return smallest integer larger than the number.
Its syntax is as follows;
Fix(value)
Fix(9.2)=9 and Fix(-9.4)=-9
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num As Integer
Dim ans As Double
num = TextBox1.Text
ans = Fix(num)
Label1.Text = ans
End Sub
The Int is a function that converts a number into an integer by truncating its decimal part and the resulting integer is the largest integer that is smaller than he number.
Its syntax is
Int(value)
For instance
Int(2.4)=2, Int(6.9)=6 , Int(-5.7)=-6, Int(-99.8)=-100
The Log function is the function that returns the natural logarithm of a number.
Its syntax is
Log(value)
For instance
Log(10)=2.302585
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num As Integer
Dim ans As Double
num = TextBox1.Text
ans = Math.Log(num)
Label1.Text = ans
End Sub
Rnd is a very useful function in Visual Basic. The Rnd function is used to write code that involves chance and probability. The Rnd function returns a random value between 0 and 1. Random numbers in their original form are not very useful in programming until we convert them to integers. For example, if we need to obtain a random output of 6 integers ranging from 1 to 6, which makes the program behave like a virtual dice, we need to convert the random numbers to integers using the formula Int(Rnd*6)+1.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rVal as Integer
rVal=Int(Rnd()*9)
Label1.Text=rVal
End Sub
In this example, Int(Rnd*9) will generate integer at random between 0 and 8. Why the function Int will truncate the decimal part of the random number and return only integer value. For every time you click the command button. For example, let say the random number generated is 0.98, after multiplying it by 6, it becomes 5.88, and using the int function; Int(5.88) will convert the number to 5.
14.7 The Round Function
The Round function is the function that rounds up a number to a certain number of decimal places. The syntax is
Round (n, m)
which means to round a number n to m decimal places. For example, Math.Round (7.2567, 2) =7.26
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num As Integer
Dim ans As Double
num = TextBox1.Text
ans = Math.Round(num, 2)
Label1.Text = num
End Sub