Difference between revisions of "LSL 101/Integers"
m (header/footer fixes) |
Samm Florian (talk | contribs) (Changed lsl tags to source lang="lsl2" which seems to be correct now?) |
||
(3 intermediate revisions by 3 users not shown) | |||
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 [ | 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. | ||
* + add | * + add | ||
Line 13: | Line 13: | ||
To be able to print these numbers however, you'll need to [ | To be able to print these numbers however, you'll need to [[typecast]] them into a string. | ||
< | <source lang="lsl2"> | ||
default | default | ||
{ | { | ||
Line 25: | Line 25: | ||
} | } | ||
</ | </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 [ | 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. | ||
< | <source lang="lsl2"> | ||
default | default | ||
{ | { | ||
Line 39: | Line 39: | ||
} | } | ||
</ | </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. | ||
< | <source lang="lsl2"> | ||
default | default | ||
{ | { | ||
Line 52: | Line 52: | ||
} | } | ||
</ | </source> | ||
With some [ | With some [[LSL_101/String_Concatenation|string concatenation]] you can combine two strings (including typecast numbers) to help label the output. | ||
< | <source lang="lsl2"> | ||
default | default | ||
{ | { | ||
Line 64: | 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 | 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)); | ||
} | } | ||
} | } | ||
</ | </source> | ||
==Beyond Base 10== | ==Beyond Base 10== | ||
Line 76: | Line 76: | ||
Hexadecimal numbers are written as in this example (which does exactly the same as the example above): | Hexadecimal numbers are written as in this example (which does exactly the same as the example above): | ||
< | <source lang="lsl2"> | ||
integer myNumber = 0x2a; | integer myNumber = 0x2a; | ||
</ | </source> | ||
''2a in hexidecimal = 42 [(2 * 16) + 10 is the same as (4 * 10) + 2]'' | ''2a in hexidecimal = 42 [(2 * 16) + 10 is the same as (4 * 10) + 2]'' |
Latest revision as of 20: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.