[blfs-dev] Lua can be used as versy simple calculator

Fernando de Oliveira famobr at yahoo.com.br
Sun Jan 25 05:00:32 PST 2015


It is very simple:

{{{
$ lua
Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> (5*60+7.3)/173
1.7763005780347
> 1.2345e6+2.56e-4
1234500.000256
> width = 7.5
> height = 12.7
> width * height
95.25
> depth = 2.8
> area = width * height
> volume = area * depth
> print(area, volume)
95.25	266.7
> math.sqrt(101)
10.049875621121
> math.pi
3.1415926535898
> math.sin( math.pi/3 )
0.86602540378444
> tonumber("123") + 25
148
> x = tonumber("123.456e5")
> x*2
24691200.0
> volume
266.7
> area
95.25
> 100 + "7"
107.0
> 100 + "hello"
stdin:1: attempt to perform arithmetic on a string value
stack traceback:
	stdin:1: in main chunk
	[C]: in ?
> 100 == "100"
false
> 100 ~= "hello"
true
> 100 == tonumber("100")
true
> 100 <= "100"
stdin:1: attempt to compare number with string
stack traceback:
	stdin:1: in main chunk
	[C]: in ?
>
}}}

The only problem I find is that there is no "end", "quit", "exit" or
something like that. Options to quit are: control-D, control-C or a
function os.exit():

{{{
$ lua
Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> os.exit()
$
}}}

Frequently used mathematical functions are available (some used above):

math.abs     math.acos    math.asin       math.atan    math.atan2
math.ceil    math.cos     math.cosh       math.deg     math.exp
math.floor   math.fmod    math.frexp      math.ldexp   math.log
math.log10   math.max     math.min        math.modf    math.pow
math.rad     math.random  math.randomseed math.sin     math.sinh
math.sqrt    math.tan     math.tanh

Yesterday I almost include in "Introduction to Lua" the sentence:

"Lua can also be used as a simple console calculator."

But I would need to write somewhere in the text info on how to use it.
The tutorial in the wiki is very good, I reproduced part of it above.
However it is for Lua-5.2, which needed an "=" sign before each
mathematical operation. In Lua-5.3, "no need to prefix with '='".

Unsure about what to do, I omitted this in the update.

I think it is worth including something in the page, but would like to
have opinions and some help about where and what to include in the text.

Of course, if there is no reply, I assume that the page should be left
as is now.

-- 
[]s,
Fernando


More information about the blfs-dev mailing list