Numbers and the Math library.
Exercises, examples and other material relating to training module U102. This topic is presented on public courses Learning to program in Lua
, Lua Programming
, Lua Programming
|Articles and tips on this subject||updated|
|4569||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 ...
|3917||BODMAS - the order a computer evaluates arithmetic expressions|
What order does a computer program use to evaluate expressions? If I write
2 + 3 * 4 + 5
does it start off, left to right ...
2 + 3 is 5
5 * 4 is 20
20 + 5 is 25
No! it does not, even though the newcomer might think that was the most natural way for ...
|2509||A life lesson from the accuracy of numbers in Excel and Lua|
Floating point numbers aren't held EXACTLY in a computer's memory - they're held to a certain accuracy (number of significant figures or binary digits to be more accurante) after which they are truncated or rounded. And this means that at time 1.0 / 3.0 * 3.0 doesn't go full circle and come back to ...
|2345||Automatically initialising Lua variables the first time|
In Lua, you need to initialise variables before you perform arithmetic on them, but you CAN test them (and if they don't exist, they will return false).
For example, this code initialises a variable to 0 if it doesn't exist, or takes its current value, then adds 1 to it straight away before saving it ...
|1744||Lua examples, Lua Courses|
Programming training is much more effective if the delegates are shown how a particular program is written, rather than just the final result ... show them just the final result and they'll ask "but how did you come up with that?"
So whenever possible, I write new examples in front of my delegates - ...
|1699||If you are learning Lua, here are some more examples|
During some courses, I write few (if any) new examples in front of my delegates, rather relying on the standard ones in the training notes (which I wrote anyway!) and making small changes and adjustments to illustrate how things work. On other courses, though, I write a some new examples from scratch ...
Examples from our training material
|allfloat|| All Lua numbers are floats|
|ccinit|| Initialising Variables|
|furry|| Lua basics - calculations, output and comments|
|ichange|| Lua - integer changes 5.2 to 5.3|
|larith|| Simple string format example|
|ludo|| Comments in Lua|
|prncr|| io.write allows output with no new line|
|rnum|| Reading from the keyboard, calculate and output|
|vars.lua|| Lua Comments and Variables|
|vatty|| Take the VAT back off a gross amount|
|vn|| Variable naming in Lua|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
Variables and variable conventions.
Expressions. Operators. Rounding.
Type and Conversion Functions.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.