Lua - changes to how integers and floats are handled - 5.2 to 5.3
Lua has historically worked internally with floats (actually doubles) even when given whole numbers. The logic was a good one - to keep the language small with a limited number of data types, and for the latest generation of hardware the speed of operations on floating points and doubles really wasn't a great deal different.
A piece of code such as
g = 9
h = 3
j = 4
would produce results 3
from the first print (as you would expect from what looks like an integer division) but 2.25
from the second.
As from Lua 5.3, calculations have a new internal data type (an integer) has been introduced, with data being converted from integer to float where appropriate - for example when a division is done. The net result is that the formatter in print
knows what's an integer and what's a float, and will now format as appropriate, rather following its previous behaviour of printing out whole number floating point results without a decimal point - so the program above will now produce results 3.0
from the first print and 2.25
from the second. Much more logical!
With other operations, performed on 2 integers you'll now get an integer result, but perfored on an integer and a float (or on two floats) the result will now float:
WomanWithCat:course grahamellis$ lua
Lua 5.3.1 Copyright (C) 1994-2015 Lua.org, PUC-Rio
> h = 7.0
> i = 5
> print (h+i)
> h = 7
> print (h+i)
Although fundamental to Lua, its quite surprising that this change will make little if any difference to most code (thank goodness - compatabiity is important). The areas for you to watch are on the output formats if a program that reads back your data insists on numbers with (or without) decimals, and if you're using massive numbers that will burst integer limits on your system. Neither's likely to be a daily occurrence.
Lua Programming course
starts tomorrow (get in touch quick if you would like to attend)! and again on 1st December (2015). If you've not programmed before, Learning to Program in Lua
starts on 30th November. Both courses are also scheduled to run again in February 2016, and at later dates too - see [here]
for the current schedule. (written 2015-11-02)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesU102 - Lua - Numbers and the Math library. 
BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09) 
A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21) 
Automatically initialising Lua variables the first time - (2009-08-10) 
Lua examples, Lua Courses - (2008-08-08) 
If you are learning Lua, here are some more examples - (2008-07-06)
Some other Articles
Classic style OO code - in LuaTables with values and code in Lua - looks like an object?Lua - using modules to add your own utilitiesA warm welcome awaits you at Well House Manor, MelkshamLua - changes to how integers and floats are handled - 5.2 to 5.3Moderation - and the tendency to over-moderateJapanese and Malaysian food in MelkshamC - why is slow to write and debug) but fast to run?Allocation of memory for objects in C++ - Stack v HeapPerl, PHP, Python, Lua, Tcl, C++, Ruby - final public courses for 2015
4750 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).