Difference between revisions of "LSL 101/LSL in Focus: Integers"

From Second Life Wiki
Jump to navigation Jump to search
Line 36: Line 36:


== Truth Values (Booleans) ==
== Truth Values (Booleans) ==
== Enumerations ==
== Bit Vectors ==
== Bit Vectors ==
== Hexadecimal Notation ==
== Hexadecimal Notation ==

Revision as of 15:23, 21 May 2009

Integers in LSL roughly correspond to integers in mathematics. They can be written in either the familiar decimal (e.g. 20, 0, -1) or hexadecimal (e.g. 0x14, 0x0, 0xFFFFFFFF).

Integers in LSL are used in three distinct ways:

  • Signed integers
  • Truth values (Booleans)
  • Bit vectors

There are a three operators, namely = (assignment), == (test for equality) and != (test for inequality) that are common to all uses for integers.

Other than assignment and (in)equality, the three different interpretations for integers involve separate sets of operators.

Signed Integers

This is the use of integers that corresponds to the normal use in mathematics. The only substantive difference is that in mathematics, there are infinitely many integers, while in LSL integers are limited to the range −2,147,483,648 to 2,147,483,647. Also, in LSL, no commas or other separators are allowed when writing integers, so these numbers would be written −2147483648 and 2147483647.

Arithmetic Operators

LSL has the normal arithmetic operators +, -, * and / (for division). The first three have the same meaning as what you used in grade school as long as the result is in the range −2147483648 to 2147483647. If the result is outside that range, different rules apply. The typical scripter doesn't need to use numbers that are so big (or small), so there's nor reason to feel you need to learn the details of what happens when the limits are exceeded. But there are instances where it is actually useful to allow numbers to exceed the limits. For the details of what happens in that case, see the Integer Overflow section, below.

Division of integers in LSL is a variation on what you learned in elementary school. If the division results in a whole number, say 10/2, the answer is 5. But 15/2 is neither 7.5 nor 7 with a remainder of 1. Instead, it is exactly 7. Any remainder is simply thrown away. Alternatively, you can think of it as being rounded toward zero. It's more accurate to say "rounded toward zero" than "rounded down", because -15/2 is -7, not -8. If you're scripting something where you really want 15 divided by 2 to be 7.5, don't despair. You'll just need to use the "float" type instead of integers.

Closely related to integer division is the remainder operator, written as %. It gives you what would remain if you were to ask for integer division on the same numbers. Here are some examples: <lsl>

    integer i;
    i = 15 % 2;     // i is assigned the value 1
    i = 15 % 4;     // i is assigned the value 3
    i = -15 % 4;    // i is assigned the value -3

</lsl>

If an attempt is made to divide by 0 (using either / or %), a run-time error occurs and script execution halts.

Comparison Operators

In addition to the arithmetic operators, there are also the normal integer comparison operators < (less than) and > (greater than). For "less than or equal", or "greater than or equal", there are two-character operators <= and >=. In the latter two, there can't be any space between the two characters. But the comparison operators are a little different that the arithmetic operators, in that they evaluate to an integer intended to be interpreted as a truth value, i.e. 0 or 1.

Integer Overflow

Truth Values (Booleans)

Enumerations

Bit Vectors

Hexadecimal Notation