Difference between revisions of "Lua: Numbers"
(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 

(2 intermediate revisions by one other user not shown)  
Line 1:  Line 1:  
{{Lua}}  {{Lua}}  
−  Lua supports one '''number''' datatype, which are generally  +  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  +  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: Stringsstrings]], but the reverse is not always true. String to number coercion can be attempted with <tt>tonumber()</tt>, which will return <tt>nil</tt> if the coercion is not possible. 
−  Because doubles are used, and they are subject to precision loss, equality comparisons  +  Because doubles are used, and they are subject to precision loss, equality comparisons could 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 [2<sup>52</sup>, 2<sup>52</sup>  1] (about ±4.5 {{dot}} 10<sup>15</sup>), though without using <tt>string.format</tt> anything larger than 10<sup>14</sup> or smaller than 10<sup>14</sup> will be printed in scientific notation. Integer precision will be lost if doubles exceed their range. 
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: Booleansboolean]] context.  Unlike many other languages, 0 is considered to be <tt>true</tt> in a [[Lua: Booleansboolean]] context. 
Latest revision as of 20:30, 27 August 2013
Lua 

Basics 
Intermediate 
Advanced 

XLua 
Add to this template 
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(), which will return nil if the coercion is not possible.
Because doubles are used, and they are subject to precision loss, equality comparisons could 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 [2^{52}, 2^{52}  1] (about ±4.5 · 10^{15}), though without using string.format anything larger than 10^{14} or smaller than 10^{14} will be printed in scientific notation. Integer precision will be lost if doubles exceed their range.
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.