Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
 
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
  print(g/h)
  j = 4
  print(g/j)

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)
  12.0
  > h = 7
  > print (h+i)
  12


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 articles
U102 - Lua - Numbers and the Math library.
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
  [2345] Automatically initialising Lua variables the first time - (2009-08-10)
  [1744] Lua examples, Lua Courses - (2008-08-08)
  [1699] If you are learning Lua, here are some more examples - (2008-07-06)


Back to
Moderation - and the tendency to over-moderate
Previous and next
or
Horse's mouth home
Forward to
A warm welcome awaits you at Well House Manor, Melksham
Some other Articles
Classic style OO code - in Lua
Tables with values and code in Lua - looks like an object?
Lua - using modules to add your own utilities
A warm welcome awaits you at Well House Manor, Melksham
Lua - changes to how integers and floats are handled - 5.2 to 5.3
Moderation - and the tendency to over-moderate
Japanese and Malaysian food in Melksham
C - why is slow to write and debug) but fast to run?
Allocation of memory for objects in C++ - Stack v Heap
Perl, PHP, Python, Lua, Tcl, C++, Ruby - final public courses for 2015
4755 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, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4569_Lua ... o-5-3.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat