0% found this document useful (0 votes)
18 views

Programming in Lua - 2.3

The document discusses the number type in Lua which represents real double-precision floating-point numbers. It notes that Lua has no integer type and that floating-point numbers can accurately represent long integers without rounding errors. It also discusses compiling Lua to use other number types like longs or single-precision floats.

Uploaded by

Qin Krein
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Programming in Lua - 2.3

The document discusses the number type in Lua which represents real double-precision floating-point numbers. It notes that Lua has no integer type and that floating-point numbers can accurately represent long integers without rounding errors. It also discusses compiling Lua to use other number types like longs or single-precision floats.

Uploaded by

Qin Krein
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

This first edition was written for Lua 5.0.

While still largely relevant for later versions, there are


some differences.
The fourth edition
targets Lua 5.3 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.

Programming in Lua
Part I. The Language
Chapter 2. Types and Values

2.3 – Numbers
The number type represents real
(double-precision floating-point) numbers.
Lua has no integer type, as it does not need it.
There is a widespread
misconception about
floating-point arithmetic errors
and some people fear
that even a simple increment can go weird with
floating-point numbers.
The
fact is that,
when you use a double to represent an integer,
there is no
rounding error at all
(unless the number is greater than 100,000,000,000,000).
Specifically, a Lua number can represent any long integer
without rounding
problems.
Moreover, most modern CPUs do floating-point arithmetic as fast as
(or even faster than) integer arithmetic.

It is easy to compile Lua so that it uses another type


for numbers, such as
longs or single-precision floats.
This is particularly useful for platforms
without hardware support for floating point.
See the distribution for detailed
instructions.

We can write numeric constants with an optional decimal part,


plus an
optional decimal exponent.
Examples of valid numeric constants are:

4 0.4 4.57e-3 0.3e12 5e+20

Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.

You might also like