VB2019 VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

Lesson 14 : The Math functions


We have learned how to VB2010 can perform arithmetic functions using standard mathematical operators. However, for more complex mathematical calculations, we need to use the built-in math functions in VB2010. There are numerous built-in mathematical functions in Visual Basic which we will introduce them one by one.

14.1 The Abs function

The Abs return the absolute value of a given number.

The syntax is

Math.Abs(number)

*The Math keyword here indicates that the Abs function belong to the Math class. However, not all mathematical functions belong to the Math class.

14.2 The Exp function

The Exp of a number x is the exponential value of x, i.e.  ex .

For example, Exp(1)=e=2.71828182

The syntax is

Math.Exp(number)

Example:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 Dim num1, num2 As Single
 num1 = TextBox1.Text
 num2 = Math.Exp(num1)
 Label1.Text = num2
 End Sub
 

14.3 The Fix Function

The Fix function truncate 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. For example, Fix(9.2)=9  but Fix(-9.4)=-9

Example:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim num1, num2 As Single num1 =TextBox1.Text num2 = Fix(num1) Label1.Text =num2 End Sub

14.4 The Int Function

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 the number. For example

Int(2.4)=2, Int(6.9)=6 , Int(-5.7)=-6, Int(-99.8)=-100

14.5 The Log Function

The Log function is the function that returns the natural logarithm of a number. For example, Log(10)=2.302585

Example 14.3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1, num2 As Single
 num1 = TextBox1.Text
 num2 = Math.Log(num1)
 Label1.Text = num2
End Sub

* The logarithm of num1 will be displayed on label1

14.6 The Rnd( ) Function

The Rnd function returns a random value between 0 and 1. Random numbers often need to be converted into integers in programming. For example, if we wish to obtain a random output of 6 integers ranging from 1 to 6, which makes the program behaves like a virtual dice, we need to convert the random numbers to integers using the formula Int(Rnd*6)+1.

Example 14.4

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num as integer
 Num=Int(Rnd()*6)+1
 Label1.Text=Num
End Sub

In this example, Int(Rnd*6) will generate a random integer between 0 and 5 because the function Int truncates the decimal part of the random number and returns an integer. After adding 1, you will get a random number between 1 and 6 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 integer function Int(5.88) will convert the number to 5, and after adding 1 you will get 6.

14.7 The Round Function

The Round function 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

Example 14.5

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1, num2 As Single
 num1 = TextBox1.Text
 num2 = Math.Round(num1, 2)
 Label1.Text = num2
End Sub

* The Math keyword indicates that the Round function belongs to the Math class.


❮ Previous Lesson Next Lesson ❯


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy