MATHEMATICAL / NUMERICAL FUNCTIONS Function Definition Syntax Example ABS (Absolute value) ASC It returns the absolute value of a number, turning a negative to a positive (e.g. - 4 to 4) It returns the ASCII code for the first character in a string N = ABS (numerical expression) N = The numeric variable. numerical expression = The numeric value whose absolute value we want. ASC(stringexpression$) stringexpression$ = Any string of ABS function. N=ABS(-44) PRINT N 44 of ASC function. PRINT ASC("ry") 144 CDBL CHR$ It converts a numeric expression to a Doubleprecision floating-point value (that is, more digits to the right of the decimal point than a singleprecision number). It is a computer number format that occupies 8 bytes (64 bits) in computer memory and represents a wide, dynamic range of values by using a floating point. It converts an ASCII code into its corresponding character. CDBL(numeric-expression) numeric-expression = Any CHR$(Numeric Variable) Numeric Variable= the ASCII code whose corresponding character has to be found of CDBL function. PRINT 1 / 7 142857142857143 Q. Write a program to see the result of CHR$ function. a = 68 PRINT CHR$(a) D CINT CSNG It rounds off the fractional part of the numeric expression and changes it into an integer. It converts a numeric expression to a singleprecision value.( Singleprecision floating-point format is a computer number format that occupies 4 bytes (32 bits) in computer memory and N = CINT(Numeric Expression) N=Numeric variable. Numerical Expression = The floating point value/the real number that will be converted to an integer. CSNG(numeric-expression) numeric-expression = Any of CINT function. N = CINT(64.6) PRINT N 65 of CSNG function. PRINT CSNG(975.3422715#) 975.3423
represents a wide dynamic range of values by using a floating point.) EXP It returns the exponential of a number to the base e(2.71828..). EXP(X) X is a value. Exponential function raises e to the value X. i.e. e^x of EXP function. PRINT EXP(3) 20.08554 FIX It removes the fractional part of a real number and return the integer portion / factorial part. If number is negative, Fix returns the first negative integer greater than or equal to number. X = INT (Numerical Expression) X = The numeric variable. Numerical Expression = The floating point value/the real number whose integer part we want. of FIX function. X = FIX(64.5) X=FIX(-64.5) 64-64 HEX$ It returns a hexadecimal string representation of a number. HEX$(numeric-expression&) numeric-expression& is any of HEX$ function. INPUT a b$ = HEX$ (a) PRINT a; "decimal is "; b$ ;" hexadecimal"?135 135 decimal is 87 hexadecimal INT (Integer) It returns only the integer portion/factorial part of a real number. If number is negative, Int returns the first negative integer less than or equal to number. Numeric Variable = FIX(Numeric Expression) X = The numeric variable. Numerical Expression = The floating point value/the real number whose integer part we want. of INT function. X = INT(64.5) X=INT(-64.5) 64-65 LOG It returns the natural logarithm of a numeric LOG(numeric-expression) numeric-expression Any positive numeric
MOD (Modulo or Modulus) It returns the integer remainder. A= Numeric-expression1 MOD Numeric-expression2 A=Numeric variable/remainder. of MOD function. x = 9 MOD 3 X=9 MOD 2.6 X=9.6 MOD 3 PRINT x Numeric-expression1= is the numerator 0 0 1 Numeric-expression2 = is the divisor OCT$ It returns an octal string representation of a number. Real number numeric expressions are rounded to integers. OCT$(numeric-expression&) of OCT$ function. numeric-expression& is any INPUT a b$ = OCT$ (a) PRINT a; "decimal is "; b$ ; " octal"?64 64 decimal is 100 octal SGN It returns a value indicating the sign of a numeric If expression is positive, it turns + 1. If expression is negative, it turns 1. A=SGN(numeric expression) of SGN function. x = SGN (-12) X= SGN (12) X=SGN (0) PRINT x -1 1 0 SPC If expression is zero, it turns 0. It skips a specified number of spaces when used with the PRINT and the LPRINT statement. SPC(n) n is the number of spaces to skip. n must be within the range of 0 through 32,767. of SPC function. PRINT "QBASIC"; SPC(15); "Programming language" QBASIC Programming language SQR (Square Root) It returns the square root of the value represented by the Variable. N = SQR (numerical expression) N = The numeric variable. numerical expression = The numeric value whose square root value we want. of SQR function. N = SQR(64) PRINT N 8
TAB It moves the text cursor to a specified print position. TAB(column%) of TAB function. column% is the column number of the new print position. INPUT "My name is"; a$ PRINT TAB(20); a$ My name is? Ria Ria Trigonometric functions COS It is a Trigonometric function which computes the cosine of an angle measured in radians. SIN It is a Trigonometric function which computes the sine of an angle measured in radians A=COS(numeric expression) A= Numeric variable(cosine of the angle) Numeric expression=the angle expressed in radians. To convert degrees to radians use the formula: Radian = degree*(pi/180) where, Pi=3.14 A=SIN(numeric expression) A= Numeric variable(cosine of the angle) Numeric expression=the angle expressed in radians. To convert degrees to radians use the formula: Radian = degree*(pi/180) where, Pi=3.14 of COS function. P = COS(R) PRINT "Cosine of"; a; "degree is "; P Cosine of 30 degree is.8661581 of SIN function. P = SIN(R) PRINT "Sine of"; a; "degree is "; P Sine of 30 degree is.4997701 TAN It is a Trigonometric function which computes the tangent of an angle measured in radians. A=TAN(numeric expression) A= Numeric variable(cosine of the angle) Numeric expression=the angle expressed in radians. To convert degrees to radians use the formula: Radian = degree*(pi/180) where, Pi=3.14. Pi is the ratio between circumference and diameter shared by all circles. of TAN function. P = TAN(R) PRINT "Tangent of"; a; "degree is "; P Tangent of 30 degree is.5769964 ATN It is the inverse Trigonometric function of Tan. The ATN or arctangent function returns the angle in radians of a numerical tangent value. radian_angle = ATN(tangent) of ATN function. P = TAN(R)
PRINT "Tangent of"; a; "degree is "; P radian = ATN(P) PRINT " Arctangent="; radian Tangent of 30 degree is.5769964 Arctangent=.5233333 RND Function RND It returns a single precision random number between 0 & 1. RND[(n#)] Q. Examples are discussed below. n# is a value that sets how These numbers are decimal RND generates the next numbers. random number. To get integers use INT function. n# RND returns Example n#<0 The same number for any n#. Result=RND(-2) n#>0 The sequence of numbers generated will not change unless RANDOMIZE Result=RND(2) is initiated. n#=0 The last number generated. Result=RND(0) n# is omitted The next random number. Result=RND To get values in a range smaller than 1: FOR x = 1 TO 5 PRINT RND To get values in a range larger than 1: Multiply RND with a number to get return up to but not including the numerical value. To get values starting at a certain number: Add that number to the RND. To get an integer range of numbers: randnum%=int(rnd*max%)+min% To get an closest integer range of numbers: randnum%=cint(rnd*(max%-min%))+min% FOR x = 1 TO 5 PRINT RND * 10 FOR x = 1 TO 5 PRINT RND + 2 PRINT INT(RND * 30 )+ 12 [returns an integer from 12 to 31 (30+12-1)] PRINT CINT(RND*(30-12)) + 12 To get different random number results each time a program runs use: RANDOMIZE TIMER Here's a program that rolls two dice and prints the value of each. RANDOMIZE TIMER PRINT (RND * 12) + 2 RANDOMIZE TIMER PRINT INT(RND * 12) + 2
RANDOMIZE TIMER INPUT "Press ENTER to roll dice...", A$ PRINT Die1 = INT(RND * 6 + 1) Die2 = INT(RND * 6 + 1) PRINT "Die 1: "; Die1 PRINT "Die 2: "; Die2