Before You Post, Please Note...

-> Please Note: By Posting, you agree to submit the contents of your post to the Public Domain <- SEE: howto post to this blog

Friday, February 2, 2007

puppybasic intro ch. 04 - numbers are key

green text is being edited and should not be considered  
relevant to the tutorial.
for a while, it will make up
the bulk of this section.

black text has been edited to be more accurate with
regards to puppybasic.

........................................................................
numbers are key
1 of 14




an intro to puppybasic

chapter 4: numbers are key







........................................................................

........................................................................
numbers are key
2 of 14

noooo! not math!

seriously, everything a computer does is enumerating and counting;
math can't be avoided, but most of the time it will not be painful.
in programming, colors are numbers, sounds are numbers, letters are
numbers, and numbers are the difference between a program that does
something and a program that doesn't; so here are some ways to work
with them...





........................................................................

........................................................................
numbers are key
3 of 14

first of all, operations: use of x+y adds the two, use of x-y
subtracts y from x, x*y gives the product, and a forward slash, "/"
will divide. exponents use the notation x^y, where x is raised to
the power of y.

math works well with variables:

x = 7
y = x * 5 ___________________________________________________
PRINT "7 times 5 ="; _7 times 5 = 35____________________________________
PRINT y ___________________________________________________


........................................................................

........................................................................
numbers are key
4 of 14

you can use parentheses in puppybasic the same way you use them in
equations on paper, (except of course that you will want to enter
them with a keyboard, not a pencil.)

moving on, ABS(x) automatically converts any value below zero to the
same value over zero, if you like. yes that's right, it just removes
the little line from a negative number.

q = ABS((5 / 7) * -1) __________________________________________________
PRINT q __.7142857________________________________________
__________________________________________________


........................................................................

........................................................................
numbers are key
5 of 14

INT

is similar in its simplicity to ABS but more likely to prove useful,
INT(x) does nothing to integers at all. if x is not an integer however,
INT will force it to become one, eliminating the fraction (or decimal).
chances are, if you stay with puppybasic you will be using this
function, so go ahead and make yourselves aquainted.

PRINT INT(7 / 3); INT(-4.9); INT(4.9) __________________________________
__2_-5__4_________________________
__________________________________


........................................................................

........................................................................
numbers are key
6 of 14

MOD

is a weird function with a weird name. (it stands for "modulus.") it
forces a long set of numbers into a smaller set: say you have a number
like... 42 (and why wouldn't you?) but you want a number in the range
between 0 and 15, because the number is *supposed to* represent a
color. you could of course reject any number over 15 and set it as 15,
or you could instead say 42 MOD 16 (much, much more useful with
variables, and even loops or counters) which will invariably (heh)
give you a number in the range 0 to 15. let's illustrate with smaller
numbers and see if it makes sense:


........................................................................

........................................................................
numbers are key
7 of 14

____________
PRINT 0 MOD 7 __0_________
PRINT 1 MOD 7 __1_________
PRINT 2 MOD 7 __2_________
PRINT 3 MOD 7 __3_________
PRINT 4 MOD 7 __4_________
PRINT 5 MOD 7 __5_________
PRINT 6 MOD 7 __6_________
PRINT 7 MOD 7 __0_________ again!
PRINT 8 MOD 7 __1_________
____________


........................................................................

........................................................................
numbers are key
8 of 14

the most fun you can have with modulus is in graphics routines, while
its best purpose may be handling (really) long division. if you never
use it, well, that's one less thing.

i'll follow this odd numeric function with one that could be the hardest
to avoid using...







........................................................................

........................................................................
numbers are key
9 of 14

VAL

n = VAL(q$) turns the string q$ into the value n, so the string
" 0.557" becomes a simple .557, to the program: something which can
be added to, subtracted from, and otherwise used as a number, which
strings (otherwise) cannot. this is priceless when working with data
from files and the keyboard.

q$ = "27a" ____________________________
PRINT VAL("hi there"); VAL(q$); VAL("4.91") __0__27__4.91_______________
____________________________


........................................................................

........................................................................
numbers are key
10 of 14

if you need a sufficiently "random" number, for dice, a "magic"
8-ball, or just a cool display on the screen, using the RND function
with INT and the number of "sides" you want:

n = INT(RND * 6)

will return a number 0 to 5, and you can have each number represent
the side of a die or anything you like.





........................................................................

........................................................................
numbers are key
11 of 14

prefer 1 to 6? why not?

n = INT(RND * 6 + 1)

will return numbers more specifically useful to dice. (you may be
thinking, "why not 'RND * 7'?", and the simple answer is "'cause it's
wrong!"

the useful answer is that RND * 6 + 1 produces a number of 6 possible
numbers, with 1 added to that number, but RND * 7 actually creates a
7-sided die, something i've never seen in my life.)


........................................................................

........................................................................
numbers are key
12 of 14

you will find that even if rolled 100 times, the 100 results you get
will be the same 100 results every time you use the program. to get
around this, the code RANDOMIZE TIMER will "seed" the RND function.
then all you have to do to roll the same dice twice is start at
precisely the same time of day as last time, to the millisecond,
accounting also for the fact that the clock circuit in your computer
is probably not as accurate a timepiece as your digital wristwatch,
even if you can set it from the atom clock: and good luck to you.

RANDOMIZE TIMER
? "this number will be random enough:"; INT(RND * 10)


........................................................................

........................................................................
numbers are key
13 of 14

q = SGN(n)

will set q to -1 if n is less than 0, set q to 0 if n is 0, and set
q to 1 if n is more than 0. i've used this, but not that often.

SQR(n)

is the square root of n, and is almost always easier to use than a
pencil and paper.




........................................................................

........................................................................
numbers are key
14 of 14

i use these to do neat things with graphics, maybe you can find some
other use for trig. here are some of the trig functions:

cosine: COS(n)
sine: SIN(n)
tangent: i'm just thrilled with the program i made using this, it...

sorry, i meant to say TAN(n)
n is the angle in radians. radians are equal to degrees * pi / 180.

-= end of chapter 4 =-


........................................................................
an intro to puppybasic (chapter 4)
this content is public domain.
........................................................................


click here to go back to the contents page

27 comments:

Anonymous said...

Well written article.

Anonymous said...

generic valium 10mg valium online paypal - order valium online

Anonymous said...

xanax order no prescription prescription drug xanax - xanax withdrawal after a week

Anonymous said...

buy tramadol online tramadol 100mg forum - buy tramadol online saturday delivery

Anonymous said...

buy tramadol online tramadol purchase online usa - tramadol dosage recreational use

Anonymous said...

buy tramadol online can you buy tramadol on the internet - tramadol hcl dogs dosage

Anonymous said...

xanax 0.5mg xanax bars yellow school buses - kenny powers quotes xanax

Anonymous said...

buy tramadol online overdose amount of tramadol - tramadol 50mg generic

Anonymous said...

buy tramadol online tramadol for dogs how much to give - buy tramadol medication

Anonymous said...

generic xanax xanax cheap no script - generic xanax from india

Anonymous said...

buy tramadol online tramadol 50 - tramadol for dogs with arthritis

Anonymous said...

cialis for sale cialis online pharmacy us - cialis sublingual review

Anonymous said...

buy tramadol online generic medicine tramadol - tramadol 50mg for toothache

Anonymous said...

buy cheap cialis 40 mg cialis reviews - generic cialis 40 mg

Anonymous said...

cialis online buy cialis online in nz - cialis 5 mg daily

Anonymous said...

buy cialis online cialis blood pressure - cheapest cialis online us

Anonymous said...

xanax online xanax effects birth control - how to buy alprazolam

Anonymous said...

http://landvoicelearning.com/#51602 tramadol 400 - tramadol hcl er dosage

Anonymous said...

tramadol without prescription buy tramadol online 100mg - where can i buy tramadol online

Anonymous said...

learn how to buy tramdadol tramadol no prescription 100mg - 50 mg tramadol lot

Anonymous said...

buy tramadol online purchase tramadol dogs - illegal to buy tramadol online

Anonymous said...

http://landvoicelearning.com/#21906 buy tramadol c.o.d saturday delivery - where can i buy tramadol for dogs

Anonymous said...

cheap tramadol online buy tramadol online next day delivery - tramadol hcl 50 mg get high

Anonymous said...

http://buytramadolonlinecool.com/#73892 tramadol 40 mg - tramadol addiction nhs

Anonymous said...

ways to buy ativan online ativan side effects men - generic name of ativan

Anonymous said...

http://staam.org/#36750 tramadol online cod - tramadol for dogs with arthritis

Anonymous said...

buy tramadol online buy tramadol for dogs usa - tramadol purchase cheap