# QBasic/Basic Math

There are six numerical variables within QBasic:

Type | Minimum | Maximum |
---|---|---|

Integer | -32,768 | 32,767 |

Long Integer | -2,147,483,648 | 2,147,483,647 |

Float | -3.37x10^38 | 3.37x10^38 |

Double | -1.67x10^308 | 1.67x10^308 |

64-bit Integer | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |

64-bit Float | ±1.18E−4932 | ±1.18E+4932 |

Please note that Integer and Float type variables for 64-bit are available only in QB64.

A lot of programming is math. Don't let this scare you: a lot of the math is simple, but it's still math. In this section, we will look at doing some basic math (the same stuff you learned in the 3rd grade) and manipulating numbers.

## Equation SetupEdit

In QBasic an equation has a basic setup a right side and a left side. For instance X=5, as you can probably figure out, this sets the variable X to 5. But we can use variables on the right side too. Y=X*10 would set Y equal to 10 times X, in this situation, 50. In this next program I will show several equations to give you a feel for math.

## 7MATH.BASEdit

```
CLS
'Set a-d to initial values
a = 10
b = 6
c = 3.1415
d = 3.333333
e = a + b
PRINT a; "+"; b; "="; e
f = c * d
PRINT c; "x"; d; "="; f
g = b - c
PRINT b; "-"; c; "="; g
h = b / d
PRINT b; "/"; d; "="; h
i = INT(d)
PRINT "Remove the decimal from "; d; "="; i
```

## Understanding 7MATH.BASEdit

The most important thing you can take away from this is the setup for math equations. I think you can figure out what all the symbols are and what they do, but QBasic is picky about equations. For 'e=a+b', if you try 'a+b=e' it will not work. The final thing I would like to address in 7MATH.BAS is the INT() function. As far as vocabulary, a function is something that takes in a piece of information and gives you another piece of information back. So PRINT, was a statement, and INT() is a function. The INT() function takes a number and truncates its decimal, it does not round. So INT(5.1) is 5 and INT(5.999) is still 5. If you want to round a number use CINT().

## 8MATH.BASEdit

```
CLS
INPUT "Enter a number: ", x
PRINT
x = x + 5
PRINT "X is now: "; x
x = x * x
PRINT "X is now: "; x
x = x / 5
PRINT "X is now: "; x
x = x - 4
PRINT "X is now: "; x
x = x / x
PRINT "X should be 1: "; x
```

## Understanding 8MATH.BASEdit

8MATH.BAS shows one simple concept that is very important in programming, but impossible in math. The way that the computer calculates the equation is it does all the math on the right side of the equation and then sticks it in the variable on the left side. So the equation x=x+5 makes perfect sense, unlike math where it is a contradiction. Reassigning a value to a variable based on its current value is common and a good way to keep the number of variables down.

## 9TIP.BASEdit

```
CLS
INPUT "How much is your bill: ", bill
INPUT "What percent tip do you want to give: ", tip
tip = tip / 100 'change percent to decimal
tip = tip * bill 'change decimal to money
PRINT
PRINT "The tip is"; tip; "$."
PRINT "Pay"; tip + bill; "$ total."
```

## Tip CalculatorEdit

9TIP.BAS calculates your tip and total bill from the bill and percent tip you wish to give. The first three lines clear the screen and get the information from the user. The fifth line changes the tip from a percent to the correct decimal by dividing by 100 (ex. 20%=.2 because 20/100=.2) the next line takes that percent and turns it into a dollar value by multiplying the decimal value by the bill. So if your bill is $20.00 and you leave a 20% tip, it multiplies 20*.2 which is 4 or $4.00. The last three lines format the output.

This is a good example of a complete program. It collects information from the user, it processes the information and it gives the user feedback. Also, the middle section of the program is a good example of variable conservation. This is subject that will take some practice to get used to. In writing a program, if you use too many variables, it will become difficult to keep track of all of them. If you try and conserve too much, you code may become difficult to understand.

You may notice that the program may print more than two decimal places if you enter a bill that is not an exact dollar value. As an exercise, try modifying the program so that it only displays two decimal places - you can use the `CINT()`

function or any other rounding method you intend to use.

## 10OROP.BASEdit

```
'ORder of OPerations
CLS
a = 15
b = 10
c = 12.2
d = 1.618
PRINT a * b + c 'these two are different
PRINT a * (b + c)
PRINT
PRINT b - c / d 'these two are different
PRINT (b - c) / d
PRINT
PRINT a * b - c * d / a + d 'these two are the same
PRINT (a * b) - ((c * d) / a) + d
```

## Parentheses and Order of OperationsEdit

10OROP.BAS is an example of order of operations and how parentheses can be used to manipulate it. I do not want to go into an indepth explanation of the order of operations here. The best advice I can give is unless you are sure of the order of operations, use parentheses to make sure the equation works how you want. All you need to know about parentheses is that the deepest nested parentheses calculate first. If you wish to know more, there are plenty of algebra resources available. On that note, you may wish to brush up on algebra. While it is not necessary for programming, it can help make programming easier and it can allow you to create more advanced programs.

## Random NumbersEdit

Though we will not go into their use until the next section, I would like to discuss the generation of random numbers. QBasic has a random number statement, RND, that generates a random decimal between 0 and 1. You can think of it as a random percent. At first, this may seem like an odd way to generate random numbers. However, with a little math it is very easy to manipulate this to provide numbers in whatever range you want.

The first step is to multiply RND by a number (the range you want). For instance 'RND*10'. This will return random numbers (decimal numbers) between 0 and 10(both included). So, to pick a random number between zero and ten we would say '(RND*10)'

## 11RND.BASEdit

```
CLS
RANDOMIZE TIMER
PRINT "Random number from 0-9:"; RND * 10
PRINT
PRINT "Random number from 1-10:"; (RND * 10) + 1
PRINT
PRINT "Random integer from 1-10:"; INT(RND * 10) + 1
PRINT
PRINT "Random even integer from 50-100:"; INT(RND * 25) * 2 + 50
```

## More on RNDEdit

A few notes on 11RND.BAS, the second line, RANDOMIZE TIMER, sets it so that the computer uses the current time to pick random number. If you don't do this, it picks the same random number every time (try it, write a one line program, PRINT RND, and run it over and over, your screen will fill up with the same number) this can prove useful for some applications, but not most. Stick RANDOMIZE TIMER in at the top of all your programs that use the RND statement and they will be far less predictable. This program just show some ways to choose what you want from your random number generator. The last line shows that you can be very specific in what you get. Make sure to run this program several times to see the different results.