Difference between revisions of "Lua: Numbers"

From Mario Fan Games Galaxy Wiki
(Created page with '{{Lua}} Lua supports one '''number''' datatype, which are generally C doubles. They perform the usual purpose of representing numerical data. Numbers can be specified in…')
 
m
Line 4: Line 4:
 
Numbers can be specified in the common decimal format (with a possible exponent and sign) or [[hexadecimal]] format. Numbers can always be coerced, or converted, into [[Lua: Strings|strings]], but the reverse is not always true. String to number coercion can be attempted with <tt>tonumber()</tt>
 
Numbers can be specified in the common decimal format (with a possible exponent and sign) or [[hexadecimal]] format. Numbers can always be coerced, or converted, into [[Lua: Strings|strings]], but the reverse is not always true. String to number coercion can be attempted with <tt>tonumber()</tt>
  
Because doubles are used, and they are subject to precision loss, equality comparisons can cause problems; fortunately, doubles can also exactly represent integers and these issues will not occur as long as the number does not end up getting a fractional component (such as through division).
+
Because doubles are used, and they are subject to precision loss, equality comparisons can cause problems; fortunately, doubles can also exactly represent integers and these issues will not occur as long as the number does not end up getting a fractional component (such as through division). The integer range that can be represented exactly with doubles is [-10<sup>14</sup>, 10<sup>14</sup>] (100 trillion each way), any higher (or lower) and precision is lost; this is close to 47,000 times the range of a 32-bit [[integer]] type.  
  
 
The <tt>math</tt> library is used to perform more complex operations on numbers, but basic arithmetic is part of the core language.
 
The <tt>math</tt> library is used to perform more complex operations on numbers, but basic arithmetic is part of the core language.
  
 
Unlike many other languages, 0 is considered to be <tt>true</tt> in a [[Lua: Booleans|boolean]] context.
 
Unlike many other languages, 0 is considered to be <tt>true</tt> in a [[Lua: Booleans|boolean]] context.

Revision as of 04:37, 27 September 2009

Lua
Lua.gif
Basics
Intermediate
Advanced
XLua
Add to this template
 Standardwikimessagebox.png This article assumes the use of Lua 5.1.

Information may not be accurate or may need revision if you are using a different version.

Lua supports one number datatype, which are generally C doubles. They perform the usual purpose of representing numerical data.

Numbers can be specified in the common decimal format (with a possible exponent and sign) or hexadecimal format. Numbers can always be coerced, or converted, into strings, but the reverse is not always true. String to number coercion can be attempted with tonumber()

Because doubles are used, and they are subject to precision loss, equality comparisons can cause problems; fortunately, doubles can also exactly represent integers and these issues will not occur as long as the number does not end up getting a fractional component (such as through division). The integer range that can be represented exactly with doubles is [-1014, 1014] (100 trillion each way), any higher (or lower) and precision is lost; this is close to 47,000 times the range of a 32-bit integer type.

The math library is used to perform more complex operations on numbers, but basic arithmetic is part of the core language.

Unlike many other languages, 0 is considered to be true in a boolean context.