Using Built-In Math Functions

February 1st, 2008 yudi Posted in Number and Strings No Comments »

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.

AddThis Social Bookmark Button

Manipulating Strings

February 1st, 2008 yudi Posted in Number and Strings No Comments »

Besides manipulating numbers, computers can also manipulate strings. A string is anything that you can type from the keyboard, including letters, symbols (such as #, &, and +), and even numbers used to identify something such as a mailing address (3095 Main Street) or a person (student number 80895).

Most programming languages identify a string as anything that appears inside quotation marks, as in the following Liberty BASIC example:


PRINT “Everything enclosed in quotation marks”
PRINT “is a string, including the numbers below:”
PRINT “72 = 9 * 8″
PRINT “You can even mix letters and numbers like this:”
PRINT “I made $4,500 last month and still feel broke.”
END

Remember In the preceding program, the formula “72 = 9 * 8” is actually a string, even though it consists of numbers. That’s because anything inside quotation marks is a string, including any numbers.

AddThis Social Bookmark Button

Declaring variables as strings

February 1st, 2008 yudi Posted in Number and Strings No Comments »

As with numbers, you can use strings directly in your program, as follows:


PRINT “Print me.”
PRINT 54
END

Just as with numbers, you may want to store strings in variables so you can reuse that particular string over and over again by typing just the variable rather than the entire string.

Unfortunately, every programming language tends to use different ways to declare a variable to hold a string data type. In Liberty BASIC, you never declare variables until you actually assign data to them. When assigning a string to a variable, the variable name must include the dollar sign ($) at the end of the variable name, as follows:


MyName$ = “Bo the Cat”

In Revolution, you can assign a string to a variable (just like you can assign a number to a variable) by using the into command like so:


put “Bo the Cat” into MyName

In REALbasic, you can declare a variable to hold string data types, as follows:


Dim MyName As String

The C++ language won’t recognize string data types, so you must use a library file called std, which you can include in your program by adding the following:


using namespace std;

This command tells your C++ program to use a library file called std, which defines a string data type. After you include the std library file, you can declare a string variable such as:


string myName;

So an entire C++ program that declares a string variable might look like this:


#include <iostream.h>
#include <stdio.h>
using namespace std;
int main()
{
   string myName;
   myName = “Bo the Cat”;
   cout << myName;
   cout << “\nPress ENTER to continue…” << endl;
   getchar();
   return 0;
}
AddThis Social Bookmark Button

Smashing strings together

February 1st, 2008 yudi Posted in Number and Strings No Comments »

Unlike with numbers, you can’t subtract, divide, or multiply strings. But you can add strings (which is technically known as concatenating strings). To concatenate two strings, you use the plus sign (+) to essentially smash two strings into a single string, as the following Liberty BASIC example shows:


MyName$ = “Joe Smith”                             ?1
PRINT “Hello, ” + MyName$                         ?2
END                                               ?3

This Liberty BASIC program tells the computer to do the following:

  • Creates a string variable called MyName$ and stuffs it with the string “Joe Smith”.

  • Prints the string “Hello, “ followed by the string stored in the MyName$ variable, so the entire line prints out Hello, Joe Smith.

  • Tells the computer that the program is at an end.

Remember If you concatenate strings, make sure that you leave a space between the two strings so that they don’t appear smashed together (likethis). In the previous example, notice the space in the second line following the word Hello and the comma.

Both REALbasic and C++ also let you use the plus sign (+) to concatenate strings. However, Revolution uses the dual ampersand symbols (&&) to concatenate strings like so:


put “Joe Smith” into MyName
put “Hello, ” && MyName into message
AddThis Social Bookmark Button

Converting Strings into Numbers (And Vice Versa)

February 1st, 2008 yudi Posted in Number and Strings No Comments »

A string can consist of letters, symbols, and numbers. The most common use for storing numbers as a string is when the numbers represent something special, such as a telephone number or address. If you want the computer to print a telephone number, you must store that number as a string, as shown here:


PRINT “555-1212″

This command prints the string 555-1212 on-screen. The quotation marks tell the computer, “Anything inside the quotation marks is a string.” What happens if you try the following command?


PRINT 555-1212

Liberty BASIC interprets this command to mean, “Subtract the number 1,212 from the number 555 and print the result (which is –657) on-screen.”

Although the number 53 and the string “53” may look identical to a human, computers treat numbers as entirely different from strings. To a computer, the number 53 is as completely different from the string “53” as a painting of a cat is different from a real cat.

AddThis Social Bookmark Button

Converting strings into numbers

February 1st, 2008 yudi Posted in Number and Strings No Comments »

The problem with strings is that when users enter numbers, the computer may treat those numbers as strings, but your program may need to use those numbers as data. If a program stores a number as a string but needs to use that number as data, the program needs to convert that number (as a string) into data.

To convert a string into data, most programming languages come with special string-to-data and data-to-string conversion commands. In Liberty BASIC, you can convert a string into a number by using the VAL function, which works like this:

 


VAL(”String”)

The VAL function tells Liberty BASIC, “Take a string (such as “45”) and turn it into a number.” If you have a string “45” and use the VAL function, Liberty BASIC converts that string “45” into the number 45.

Remember If you use the VAL function on a string that doesn’t represent a number, such as the string “Hello!”, the VAL function returns a zero value.

To see how the VAL function works, run the following Liberty BASIC program:

 


YearBorn$ = “1964″                                ?1
PRINT “You were born in ” + YearBorn$             ?2
Age = 2007 - VAL(YearBorn$)                       ?3
PRINT “In 2007, you were this old = “; Age        ?4
END ?5

This Liberty BASIC program tells the computer to do the following:

  • ?1 Creates a string variable called YearBorn$ and stuffs it with the string “1964”.

  • ?2 Prints the string “You were born in ” followed by the string stored in the YearBorn$ variable, so the entire line prints You were born in 1964. In this line, the computer treats “1964” as a string.

  • ?3 The VAL function converts the string “1964” to the number 1964. Then it subtracts 1964 from 2007 and stores the calculated result in the Age variable.

  • Prints the string In 2007, you were this old = and then prints the number stored in the Age variable, which is 20071964 (43).

  • This line marks the end of the program.

REALbasic uses the same Val function to convert a string into data, as shown here:

 


Dim YearBorn as string                            ?1
Dim Age as integer                                ?2
YearBorn = “1964″                                 ?3
MsgBox “You were born in ” + YearBorn             ?4
Age = 2007 - Val(YearBorn)                        ?5
MsgBox “In 2007, you were this old = ” + Str(Age) ?6

This REALbasic program tells the computer to do the following:

  • Creates a string variable called YearBorn.

  • Creates an integer variable called Age.

  • Stores the string “1964” into the YearBorn string variable.

  • Uses the MsgBox command to display a box on-screen that shows the string “You were born in “ followed by the string stored in the YearBorn variable, so the entire line prints You were born in 1964. In this line, the computer treats “1964” as a string.

  • The Val function converts the string “1964” to the number 1964. Then it subtracts 1964 from 2007 and stores the calculated result (43) in the Age variable.

  • Uses the MsgBox command to display a box on-screen that shows the string In 2007, you were this old = and then uses the Str function to convert the Age variable (43) to the string “43”. So the entire line prints out In 2007, you were this old = 43.

One problem with type-safe languages like REALbasic is that they restrict what type of data can go into variables (to prevent errors in your program), but they can also make programming more cumbersome when you want to treat a number as both a string and data.

The following Revolution program automatically detects when a number should be treated as a string and when it should be treated as data. Although this has the potential for causing errors, it also gives you the freedom to concentrate on the logic of your program and not worry about trivial details like converting strings into data (and vice versa).

 


put “1964″ into YearBorn                                 ?1
put “You were born in ” && YearBorn into message         ?2
wait for 5 seconds                                       ?3
put 2007 - YearBorn into Age                             ?4
put “In 2007, you were this old = ” && Age into message  ?5

This Revolution program tells the computer to do the following:

  • Stores the string “1964” in a variable called YearBorn.

  • The two ampersand symbols (&&) tack the string “1964” onto the other string “You were born in ”. Then this line display You were born in 1964 in a message box on-screen.

  • Tells the computer to wait for five seconds. Without this command, Revolution would rush into the rest of the program and the message box displayed in line 2 would appear and disappear in the blink of an eye.

  • Automatically converts the string “1964” into the number 1964. Then subtracts the number 1964 from 2007 and stores the result (43) in a variable called Age.

  • ?5 Converts the Age variable number (43) into the string “43” and then the two ampersand symbols (&&) tack the “43” string onto the end of the “In 2007, you were this old = ” string. Then this line displays In 2007, you were this old = 43 in a message box that appears on-screen.

The main difference between Revolution and the previous two BASIC examples is that Revolution is smart enough to figure out when to treat a number as data and when to treat a number as a string without having to define any data conversion yourself.

Technical Stuff The C++ language doesn’t recognize strings as valid data types, although you can define a string data type by using the using namespace std; command at the top of your programs. However, C++ doesn’t provide any built-in commands for converting strings into numbers (and vice versa), so many programmers simply write their own conversion programs and store them in library files that they can reuse later.

AddThis Social Bookmark Button

Converting a number into a string

February 1st, 2008 yudi Posted in Number and Strings No Comments »

Rather than convert a string into a number, you might want to go the other way and convert a number into a string.

In Liberty BASIC, you can convert a number into a string by using the STR$ function, which works like this:


STR$ (Number)

The following Liberty BASIC program shows how to use the STR$ function to convert the BossIQ variable into a string:


BossIQ = 34                                      ?1
NewString$ = “This is the IQ of your boss = ” +
           STR$(BossIQ)                          ?2
PRINT NewString$                                 ?3
END                                              ?4

This Liberty BASIC program tells the computer to do the following:

  • 1 Stores the number 34 in a variable called BossIQ.

  • 2 Uses the STR$ function to convert the number stored in the BossIQ variable (34) into a string (“34”). Then it pastes the two strings together to store This is the IQ of your boss = 34 in a string variable called NewString$.

  • 3 Prints the string This is the IQ of your boss = 34 on-screen.

  • 4 Defines the end of the program.

In this example, Liberty BASIC lets you declare a number variable and convert that number into a string. Here’s the equivalent REALbasic program that converts a number into a string by using the Str function:


Dim BossIQ as Integer
Dim NewString as String
BossIQ = 34
NewString = “This is the IQ of your boss = ” + Str(BossIQ)
MsgBox NewString

In Revolution, you don’t need to give an explicit command to convert a number into a string; the computer does this conversion for you automatically, as the following program demonstrates:


put 34 into BossIQ
put “This is the IQ of your boss = ” && BossIQ into
           message
AddThis Social Bookmark Button




eXTReMe Tracker