News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Variables & Math Questions

Started by kevin, November 10, 2006, 08:07:53 AM

Previous topic - Next topic

kevin





 NOTE: See up-to-date FAQ's on PlayBASIC.com

This FAQ might be out of date, always check the home page versions for the most up to date editions




Variable Casting and Math operations






Q.  What can I store in Integer, Floating Point# or String$ Variables ?



  A.  These are three different ways to storing information in your program.   Integer and Floating point (also known as REAL)  are numerical,  while Strings store rows of character (text) information.  

Quote
 Integer Variables only hold numeric values. These values can represent a numeric range of -2147483647 to 2147483647. Integer Variables can only contain whole numbers. (so no fractional values). Integer variables can be identified easily as they no post-fix character eg. "Score"


Floating Point Variables hold decimal numbers (numbers with a decimal point). They are ideal for calculations that require fractional accuracy. Floating point variables can be identified by their "#" symbol post-fix. This classifies the variable to PlayBasic as being of Floating Point type. eg. "Gravity#"


String Variables hold a row text. String variables allow us to store and manipulate words/text and individual characters. These characters can be any character on your keyboard. String variables can be identified by their "$" symbol post-fix. This classifies the variable to PlayBasic as being of string type. eg. "Name$"

Through out your program you are going to be using variables constantly. You'll be using them in calculations and to hold information about the different aspects of your game. So it's important you get familiar with the different types and the conventions that programming languages like PlayBasic use.

 See the Help for more information (About\Variables)






Q. Can I use the same name for many different types of variables ?


   A. Yes, PlayBasic is a fairly traditional version of basic, and therefore does support multi instance variables.   That just means that you can have  Integer/Float and String variables, that all share the same name.   The only notable difference will be their post fix symbols.  I.e Float variables use the "#" character as a postfix and strings use the "$" character. Integers don't require a postfix symbol.

PlayBASIC Code: [Select]
; Define three different variables that use the same name
Variable = 100
Variable#=123.456
Variable$="Hello World"

; display them
Print Variable
Print Variable#
Print Variable$
; show the screen and wait for the user to press a key before ending
Sync
Waitkey







Q. Are Variables/Functions /Array Names Case sensitive ?


A. No.  I.e.  PlayBasic would treat the following string variables named "ProgramName$" and "pROGRAMname$" as the same variable. So the capitalization you use is irrelevant.





Q. In calculations I noticed that PB does not recognize numbers like 'Answer = 2.45 + 7.28', is this normal or am I doing something wrong?


   A. Yes.  A common mistake new programmers make, is they misunderstand the difference between Integer and Floating Point variables. Integer variables in PlayBasic and the vast majority of programming languages are whole numbers.  That is, they have no fractional part. So the following numbers are integers i.e. 5, 100, 42, and 8743 while these are not i.e. 1.23, 543.876, 33.33 they're floating point.  Floats are also known as REALS in some programming languages.

   PlayBasic distinguishes between Integer and Floating Point variables using the "#" post fix symbol. Floating point variables always require the "#" Postfix symbol at the end. This tells PlayBasic that this variable has a fractional part, while integer variables don't.

   Therefore, In the expression Answer = 2.45 + 7.28 we're telling PlayBasic to perform an addition between the two floating point numbers 2.45 and 7.28 , then store the result in the Integer Variable called "Answer".   In order for this to occur, the floating point result (9.73)  has to be converted to a whole number. This conversion include automatic rounding up or down.

 
PlayBASIC Code: [Select]
; Add floats and Store them in an Integer Variable
Answer = 2.45 + 7.28

; Add floats and Store them in a Floating Point Variable
FloatAnswer# = 2.45 + 7.28

; show the results
print 2.45 + 7.28
print Answer
print FloatAnswer#

; refresh the display and wait for a key press before ending
Sync
Waitkey








Q. How come when I do a Division between two Variables or Values the result is integer ?



  A.  During math operations like Addition, Subtraction, Multiplication and Division,  PlayBasic  automatically casts the type of the result. given the type of the two input values/variables using the following table.

Quote
   Division Casting Table

   IntegerResult  =  Integer / Integer    
   FloatResult# =  Integer / Float    
   FloatResult# =  Float / Integer
   FloatResult# =  Float / Float

   While this table is showing division, the same logic applies for Addition, Subtraction and Multiplications also.  

   Now if you have two integers and want PlayBasic to perform a floating point Division, you have to cast at least one side of divider operation as float.   You can do this with the Float() function.  

  Example.

PlayBASIC Code: [Select]
; Assign the integer variable X the integer value of 100
X = 100

; Assign the integer variable Y the integer value of 30
Y = 3


; Show Integer X divided by Integer Y
Print X / Y

; Show Integer X divided by Y recast as float()
Print X / float(Y)

; Show Float(X) divided by integer Y
Print Float(X) / Y

; Show Float(X) divided by Float(y)
Print Float(X) / Float(Y)


; refresh the display and wait for a key press before ending
Sync
Waitkey









Q.  How precise are floating Point values in PlayBasic ?


  A.  PlayBasic uses 32bit (Single) precision with floating point values.  These are also commonly known as Singles & Reals.

     However, There is common misconception that's present between all programming languages, about the accuracy of Floating Point numbers in computer programs.    You might be horrified to discover that float pointing numbers aren't actually very precise, in fact they're more an approximation!

    See-> (Complete) Tutorial to Understand IEEE Floating-Point Errors

 



Q. Does PlayBasic round Floating Point to Integer conversions ?


   A. Yes,  this is the default behavior of the PlayBasic runtime.    





Q. Can automatic Rounding (of floats to integers) to be turned off ?


    A. No.  However, this behavior can be overcome via using the INT() function.  The  Int()  function truncates the float to it's whole number, without rounding.  Making it's functionality the same as the Floor() function found in other languages.

PlayBASIC Code: [Select]
   // create variable a# and assign it the value 1.62
A#=1.62

// Assign A# to a integer . This will be auto rounded

MyInteger = A#
print MyInteger


// Convert a# to an integer, then assign MyInteger The result

MyInteger = Int(A#)
print MyInteger

Sync
Waitkey








Q. Can automatic rounding skew results ?


    A. Yes it can.    If you use,  user defined functions that return floats and you assign them to an integer, then your result will be automatically rounded.  Which can (depending upon what you're doing) skew the result of a calculation.     This can also occur with internally bound functions.     A good example of this is the RND() function.  

     RND() on the surface appears to be produce inclusive results,  meaning that if you request a random number between 0 and 10  (Eg Rnd(10))  the result will range from 0 to 10.  Or does it ?   Well despite what the documentation tells us.   Rnd isn't actually inclusive of the upper range.   It's just easier for new users to comprehend.

     Internally RND() produces floating point results, this value will be inclusive of 0 and exclusive of the upper range.    But unfortunately, this float result is then passed internally and type cast (converted to integer) through the runtime into an integer.   It's this casting operation will round the result and bias the result.   So if we called RND(10) and  internally the function produced 9.76, this result will be automatically rounded.   Skewing it towards 10.   In the case of RND() that can cause some bais in the  the distribution of random numbers produced.

    Example,

PlayBASIC Code: [Select]
   Dim Results(10)

// call the rnd function 10000 times and tally the results
For lp=0 to 10000
// call rnd() which and assign it's internal float to an integer
Index=rnd(10)
Results(Index)=Results(Index)+1
next

print "Show Skewed Results Due to Rounding"
For lp=0 to 10
print digits$(lp,2)+") = ("+digits$(results(lp),4)
next

print "notice how 0 and 10, appear to be less likely ?"

Sync
Waitkey






   Notice how 0 and 10 occur approximately 50% less frequently than numbers 1 through 9 ? -  This shows the rounding is pushing the results off by 0.5.





Q. Can I avoid skewed results when generating  random numbers ?


    A. Yes you can.   To do this,  rather than calling the integer versions of the Rnd() and RndRange() functions, use the Floating point editions Rnd#(), RndRange#() and then INT() the result.  This will show you the true distribution of results.

PlayBASIC Code: [Select]
   Dim Results(10)

// call the rnd function 10000 times and tally the results
For lp=0 to 10000
// call rnd#() and then use the INT() function to convent the
// result to integer manually. This avoids rounding!
Index=int(rnd#(10))

Results(Index)=Results(Index)+1
next

print "Show Results"
For lp=0 to 10
print digits$(lp,2)+") = ("+digits$(results(lp),4)
next

print "notice how 10, never occurs ?"

Sync
Waitkey




 You'll noticed that in these results the upper range (10 in this example) of the random never occurs.  Showing that the random number generator is actually range exclusive.