How variables work?

Started by Fluorescent, August 08, 2005, 02:54:35 AM

Previous topic - Next topic

Fluorescent

I've lookes through the help files, but haven't found anything yet. I was wondering how variables work in Play Basic. I've seen variables named x# and something$ etc. What does these suffixes mean??

Is there a little reference on these??

empty

#1
Variables names without a suffix are integer variables that can store values from −2,147,483,648 to +2,147,483,647. A # suffix makes it a floating point variable (range from ±1.5 × 10−^45 to ±3.4 × 10^38, 7 digits precision). A $ suffix indicates that the variable is a string type, ie. you can assign text (in double quotes) to it.

PlayBASIC Code: [Select]
i = 42       ; integer type
f# = 1.234 ; floating point type
s$ = "Hello" ; string type




Like most Basic dialects, numerical variables are weakly typed. That means that they are implicitly converted (or cast) when they are used. Example
PlayBASIC Code: [Select]
f# = 42.2342
i = f#
Print i
Sync : WaitKey

// Would output
42



One important thing to keep in mind is that the type of the result of (division) operations depends on the type of operands:
Integer = Integer / Integer
Float = Integer / Float
Float = Float / Integer
Float = Float / Float

Example:
PlayBASIC Code: [Select]
v1# = 5/2    ; 2 integers are divided, the result is an integer type (ie. 2)
v2# = 5/2.0 ; an integer divided by a floating point value: the result is a float (2.5)
v3# = 5.0/2 ; a float divided by an integer, result = float (2.5)
v4# = 5.0/2.0; a float divided by a float, result = float (2.5)



You can use the functions Float() and Integer() to manually convert types:
PlayBASIC Code: [Select]
i1 = 5
i2 = 2
v1# = i1 / i2 ; two integer operands, result is an integer type
v2# = i1 / Float(i2) ; i2 converted to a float, therefore the result of the expression is a float