Objective 7.03 Apply Built-in Math Class Functions Computer Programming I Image taken from: http://www.stsd.org/webpages/cyurconic/news.cfm?subpage=67815
Objects/Classes and Methods in Visual Basic An object is a structure containing data and methods that manipulate the data. Almost everything you do in Visual Basic is associated with objects. If you are new to object-oriented programming, the following terms and concepts will help you get started. Classes and Objects The words "class" and "object" are used so much in object-oriented programming that it is easy to get the terms mixed up. Generally speaking, a class is an abstract representation of something, whereas an object is a usable example of the thing the class represents.
Objects/Classes and Methods in Visual Basic Methods – Functions Methods are operations that can be performed on data. A method may take some input values through its parameters and may return a value of a particular data type. There are two types of methods in VB: Sub Procedures and Functions. For this Unit, we will discuss two built in classes with built in Functions: The Math and String classes.
Math Class Functions The Math class provides programmers with numerous built-in functions. We will only look at a few of these functions. Abs(num) Returns the absolute value of a positive or negative number Sqrt(num) Returns the square root of a number Sign(num) Returns the sign of a number Round (num, places) Returns number nearest to the specified value. Image taken from: http://www.amazingclassroom.com/templates/blue_business/math_1.asp?cag=sample1
Using Abs(num) The Abs() function returns the absolute value of the number and works with the following data types: Decimal, Double, Integer, Single Abs() Function intNum Value after execution intNum = Math.Abs(4) 4 sngNum = Math.Abs(-4.123) 4.123 intNum = Math.Abs(0) Image taken from: http://withlovero.com/2012/05/15/absolute-value-17-2/
Using Sqrt(num) Sqrt(num) Returns the square root of a number as a double Sqrt() Function dblNum Value after execution dblNum = Math.Sqrt(4) 2.0 dblNum = Math.Sqrt(0) 0.0 dblNum = Math.Sqrt(-4) NaN (not a number) (NaN represents a value that is not a number) Image taken from: http://keep3.sjfc.edu/students/anb02630/e-port/vsg/virtual%20strudy%20guide%2021.html
Using Sign(num) The Sign() function works with the following data types: Decimal, Double, Int16, Int32, Int64, SByte, Single Sign() Function intNum Value after execution intNum = Math.Sign(4) 1 intNum = Math.Sign(-4) -1 intNum = Math.Sign(0) Image taken from: https://www.honors.umass.edu/event/last-day-adddrop
Round Function The Round() function works with either a decimal or a double. The variable/value that represents the number of places should be an Integer. Round() Function dblNum Value after execution dblNum = Math.Round(5.23651, 2) 5.24 decNum = Math.Round(5.23651) 5.0 dblNum = Math.Round(5.5) 6.0 decNum = Math.Round(5.225, 2) 5.23 Note that 5 does round up here.
More info? This PowerPoint provided an overview of several methods in the Math class in Visual Studio. For more information on this topic http://msdn.microsoft.com/en-us/library/32s6akha(v=VS.90).aspx http://msdn.microsoft.com/en-us/library/system.math.aspx