Difference between revisions of "LSL 101/Integers"

From Second Life Wiki
Jump to navigation Jump to search
m (added something from previous page to this one as it was too complicated for the overview page.)
m (typo fixes & headers & footers)
Line 4: Line 4:




Integers are another data type found in LSL along with Strings. LSL can handle whole numbers from −2,147,483,648 to +2,147,483,647.  Integers can either be represented literally in a script or placed within a [https://wiki.secondlife.com/wiki/LSL_101/Variables variable] at runtime. Integers can use the following mathematial operations.
Integers are another data type found in LSL along with Strings. LSL can handle whole numbers from −2,147,483,648 to +2,147,483,647.  Integers can either be represented literally in a script or placed within a [https://wiki.secondlife.com/wiki/LSL_101/Variables variable] at run-time. Integers can use the following mathematical operations.


* + add
* + add
Line 13: Line 13:




To be able to print these numbers however, you'll need to [https://wiki.secondlife.com/wiki/Typecast typcast] then into a string.  
To be able to print these numbers however, you'll need to [https://wiki.secondlife.com/wiki/Typecast typecast] them into a string.  




Line 27: Line 27:
</lsl>
</lsl>


Why do we preface the number 5 with (string)?  llSay is only expecting a string and will not be able to print a number without you [https://wiki.secondlife.com/wiki/Typecast typcasting] it first. We can do the following.
Why do we preface the number 5 with (string)?  llSay is only expecting a string and will not be able to print a number without you [https://wiki.secondlife.com/wiki/Typecast typecasting] it first. We can do the following.




Line 83: Line 83:


A note to those who have used other programming languages before: LSL does not have a binary variable type.  TRUE and FALSE are stored using integers with TRUE having a value of 1 and FALSE having a value of 0.
A note to those who have used other programming languages before: LSL does not have a binary variable type.  TRUE and FALSE are stored using integers with TRUE having a value of 1 and FALSE having a value of 0.
'''Go back to [[LSL 101/Variables | Variables]] or proceed onward to [[LSL 101/Functions | Functions]].'''

Revision as of 20:44, 7 July 2012

← Variables ↑̲  LSL 101  ̲↑ Functions →


Integers are another data type found in LSL along with Strings. LSL can handle whole numbers from −2,147,483,648 to +2,147,483,647. Integers can either be represented literally in a script or placed within a variable at run-time. Integers can use the following mathematical operations.

  • + add
  • - subtract
  • * multiply
  • / divide
  • % modulus


To be able to print these numbers however, you'll need to typecast them into a string.


<lsl> default {

   state_entry()
   {
       llSay(0, (string)5 );
   }
  

} </lsl>

Why do we preface the number 5 with (string)? llSay is only expecting a string and will not be able to print a number without you typecasting it first. We can do the following.


<lsl> default {

   state_entry()
   {
       llSay(0, "5" );
   }
  

} </lsl>

In the example above, 5 is represented as a string so no typecasting is needed. However in this form it's not really the integer 5. If it's not an integer, you won't be able to perform math on the number. Here's an example where math is performed before printing.

<lsl> default {

   state_entry()
   {
       llSay(0, (string)(2+3) );
   }
  

} </lsl>

With some string concatenation you can combine two strings (including typecast numbers) to help label the output.

<lsl> default {

   state_entry()
   {
       llSay(0, "two plus three equals " + (string)(2 + 3) );
       llSay(0, "100 minus 1 equals " + (string)(100 - 1) );
       llSay(0, "5 times 5 equals " + (string)(5 * 5) );
       llSay(0, "six divides in twelve " + (string)(12 / 6) + " times." );
       llSay(0, "20 divided by 7 equals " + (string)(20 / 7) + " with a remainder of " + (string)(20 % 7));
   }
  

} </lsl>

Beyond Base 10

You are used to using the decimal number system (called base 10), where numbers are counted using the digits 0 to 9, but you should also know there are other number systems that can be used with LSL, such as hexadecimal (base 16), which uses digits 0 to 9 and letters A to F. You don't need to know about the hexadecimal system to write scripts but you may well come across hexadecimal numbers if you are modifying scripts someone else has written and you may later find that there are some places where it makes sense to use hexadecimal numbers instead of decimals.

Hexadecimal numbers are written as in this example (which does exactly the same as the example above):

<lsl> integer myNumber = 0x2a; </lsl>

2a in hexidecimal = 42 [(2 * 16) + 10 is the same as (4 * 10) + 2]

A note to those who have used other programming languages before: LSL does not have a binary variable type. TRUE and FALSE are stored using integers with TRUE having a value of 1 and FALSE having a value of 0.

Go back to Variables or proceed onward to Functions.