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

From Second Life Wiki
Jump to navigation Jump to search
Line 12: Line 12:
LSL has the normal algebraic 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.
LSL has the normal algebraic 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.
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.


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>
 
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 ===
=== Integer Overflow ===

Revision as of 12:05, 11 May 2009

Integers in LSL roughly correspond to integers in mathematics. But integers in LSL are used in three distinct ways:

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

We'll treat each of these separately.

Signed Integers

This is the use of integers that corresponds to the normal use in mathematics. The only difference is that mathematics, there are an infinite number of 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.

LSL has the normal algebraic 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>

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)

Bit Vectors