X



Home>Tutorial>

Visual Basic Math Functions:Abs, Log, Rnd, Int, Exp, Fix Made Easy

Nuelson Penuel Wednesday, September 9, 2020 Visual Basic.Net

Summary

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.

Visual Basic Math Functions Overview

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.

Visual Basic Abs Function

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.

Math.Abs Function Example


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

Visual Basic Exp Function

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)

Math.Exp Example


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

Visual Basic Fix Function

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

Fix Function Example


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

Visual Basic 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 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

Visual Basic Log Function

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

Log Function Example


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

Visual Basic Rnd( ) Function

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.

Rnd() Function Example


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

Math.Round Example


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

| | Views(896)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services