Difference between revisions of "LSL 101/Integers"

From Second Life Wiki
Jump to navigation Jump to search
(Changed lsl tags to source lang="lsl2" which seems to be correct now?)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:LSL 101]]
[[Category:LSL 101]]
{{NavNextPrev|prev=Strings and Simple Output|next=The touch_start Event}}
{{NavNextPrev|prev=String Concatenation|next=Using Variables}}
{{LSL Wikibook Index}}


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.


+ add
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 [[LSL_101/Variables|variable]] at run-time. Integers can use the following mathematical operations.
- subtract
* multiply
/ divide
% modulus


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


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 [[typecast]] them into a string.


<lsl>
 
<source lang="lsl2">
default
default
{
{
Line 23: Line 25:
    
    
}
}
</lsl>
</source>


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 [[typecast]]ing it first. We can do the following.




<lsl>
<source lang="lsl2">
default
default
{
{
Line 37: Line 39:
    
    
}
}
</lsl>
</source>


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.
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>
<source lang="lsl2">
default
default
{
{
Line 50: Line 52:
    
    
}
}
</lsl>
</source>


With some [https://wiki.secondlife.com/wiki/LSL_101/String_Concatenation string concatenation] you can combine two strings (including typecast numbers) to help label the output.
With some [[LSL_101/String_Concatenation|string concatenation]] you can combine two strings (including typecast numbers) to help label the output.


<lsl>
<source lang="lsl2">
default
default
{
{
Line 62: Line 64:
         llSay(0, "100 minus 1 equals " + (string)(100 - 1) );
         llSay(0, "100 minus 1 equals " + (string)(100 - 1) );
         llSay(0, "5 times 5 equals " + (string)(5 * 5) );
         llSay(0, "5 times 5 equals " + (string)(5 * 5) );
         llSay(0, "six divides in twelve " + (string)(12 / 6) + " times." );
         llSay(0, "six divides into twelve " + (string)(12 / 6) + " times." );
         llSay(0, "20 divided by 7 equals " + (string)(20 / 7) + " with a remainder of " + (string)(20 % 7));
         llSay(0, "20 divided by 7 equals " + (string)(20 / 7) + " with a remainder of " + (string)(20 % 7));
     }
     }
    
    
}
}
</lsl>
</source>
 
==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):
 
<source lang="lsl2">
integer myNumber = 0x2a;
</source>
 
''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 [[LSL 101/Using Variables| Using Variables]] or go back to [[LSL 101/String Concatenation | String Concatenation]].'''

Latest revision as of 21:37, 22 January 2015

← 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.


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

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.


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

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.

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

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

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 into twelve " + (string)(12 / 6) + " times." );
        llSay(0, "20 divided by 7 equals " + (string)(20 / 7) + " with a remainder of " + (string)(20 % 7));
    }
   
}

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):

integer myNumber = 0x2a;

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.