Difference between revisions of "LSL 101/Integers"

From Second Life Wiki
Jump to navigation Jump to search
m (typo fixes & headers & footers)
m (header/footer fixes)
Line 1: Line 1:
[[Category:LSL 101]]
[[Category:LSL 101]]
{{NavNextPrev|prev=Variables|next=Functions}}
{{NavNextPrev|prev=String Concatenation|next=Using Variables}}
{{LSL Wikibook Index}}
{{LSL Wikibook Index}}


Line 84: Line 84:
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]].'''
'''Continue the tutorial by going forward to [[LSL 101/Using Variables| Using Variables]] or go back to [[LSL 101/String Concatenation | String Concatenation]].'''

Revision as of 23:40, 7 July 2012

← String Concatenation ↑̲  LSL 101  ̲↑ Using Variables →


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.

Continue the tutorial by going forward to Using Variables or go back to String Concatenation.