LSL 101/LSL in Focus: Integers

From Second Life Wiki
< LSL 101
Revision as of 20:31, 9 May 2009 by Omei Turnbull (talk | contribs) (New page: __TOC__ 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'l...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 7 divided by 2 to be 2.5, don't despair. You'll just need to use the "float" type instead of integers.

Integer Overflow

Truth Values (Booleans)

Bit Vectors