By combining mathematical operators, you can create practically any type of mathematical formula. But creating some mathematical formulas may prove too cumbersome, so as a shortcut, many programming languages provide built-in mathematical functions that you can use. Table lists the names of common mathematical functions.
Technical Stuff If you don’t understand terms like arcsine or logarithm, you probably don’t need to use them anyway. The important point to remember is that all programming languages include built-in mathematical functions that you can use if you need them.
To see how the Liberty BASIC mathematical functions work, run the following program and type different numbers (negative, positive, decimal, and so on) between 0 and 1.0 to see how the program works:
PROMPT “Type in a number”; AnyNumber
PRINT “The ABS value = “; ABS(AnyNumber)
PRINT “The ACS value = “; ACS(AnyNumber)
PRINT “The ASN value = “; ASN(AnyNumber)
PRINT “The ATN value = “; ATN(AnyNumber)
PRINT “The COS value = “; COS(AnyNumber)
PRINT “The EXP value = “; EXP(AnyNumber)
PRINT “The LOG value = “; LOG(ABS(AnyNumber))
PRINT “The SIN value = “; SIN(AnyNumber)
PRINT “The SQR value = “; SQR(AnyNumber)
PRINT “The TAN value = “; TAN(AnyNumber)
PRINT
END
You can use only positive numbers with the LOG function or in calculating a square root.
Technical Stuff Unlike many programming languages, C++ doesn’t have built-in math functions. Instead, C++ compilers come with a library of math functions written in C++. You don’t have to worry about how these math functions work; you can just use them by adding the #include <math.h> command at the top of your program, as shown here:
#include <iostream.h>
#include <stdio.h>
#include <math.h>
int main()
{
double anyNumber;
cout << “Enter a number: “;
cin >> anyNumber;
cout << “The ABS value = “; cout << abs(anyNumber);
cout << “nThe ACOS value = “; cout << acos(anyNumber);
cout << “nThe ASIN value = “; cout << asin(anyNumber);
cout << “nThe ATAN value = “; cout << atan(anyNumber);
cout << “nThe COS value = “; cout << cos(anyNumber);
cout << “nThe EXP value = “; cout << exp(anyNumber);
cout << “nThe LOG value = “; cout << log(anyNumber);
cout << “nThe SIN value = “; cout << sin(anyNumber);
cout << “nThe SQRT value = “; cout << sqrt(anyNumber);
cout << “nThe TAN value = “; cout << tan(anyNumber);
cout << “nThe ACOS value = “; cout << acos(anyNumber);
cout << “nPress ENTER to continue…” << endl;
getchar();
return 0;
}
A mathematical function accepts a value and uses that value to calculate a result. So you can assign a mathematical function to a variable, as the following REALbasic program demonstrates:
Dim x, y as Single ?1
x = 0.52 ?2
y = cos(x) ?3
MsgBox str(y) ?4
This REALbasic program tells the computer to do the following:
-
1 Declares two variables x and y that can hold only a single-precision (decimal) number.
-
2 Stores the value 0.52 into the x variable.
-
3 Gives the value 0.52 to the cosine (cos) mathematical function. The cosine mathematical function calculates the cosine of 0.52, which is 0.8678191. This value gets stored in the y variable.
-
4 The str command converts the number 0.8678191 into the string “0.8678191”, which it then displays in a message box created by the MsgBox command.
Mathematical functions work the same way in Revolution, as the following program shows:
put 0.52 into x ?1
put cos(x) into y ?2
put y into message ?3
This Revolution program tells the computer to do the following:
-
1 Stores the value 0.52 into a variable called x.
-
2 Gives the value of x (0.52) to the cosine mathematical function, which returns the value 0.876819 and stores this value in a variable named y.
-
3 Displays the value of the y variable on-screen in a message box defined by the message command.
