-- input: real number
-- output: round to the nearest integer number.
--         In case number ends in 0.5 it is rounded up.
function round(x)
  return math.floor(x + 0.5)
end

-- input: string
-- output: string with zeros added on the left, until reaching the desired length
function padding_on_left(s, length)
  local ans = s
  local n_fill = length - string.len(s)
  if (n_fill > 0) then
    ans = string.rep('0', n_fill) .. ans
  end
  return ans
end

-- input: positive integer number
-- output: string containing binary representation of the input
function binary(num)
    local ans = ''
    while (num > 0) do
        remainder = num%2
        ans = tostring(remainder) .. ans 
        num = math.tointeger((num-remainder)/2)
    end
    return ans
end

-- input: real number
-- output: list of strings containing the three elements of its representation 
--         as IEEEE 754 floating point
function components_ieee754(number)
  -- first reduce the problem to working with a 
  -- nonzero positive number
  local sign = 1
  if (number == 0) then
    return string.rep('0', 1), string.rep('0', 8), string.rep('0', 23)
  elseif (number < 0) then
    number = -number
    sign = -1
  end
  -- handle the inputs that are too large 
  -- pending
  
  -- handle the inputs that are too small
  -- pending 
  
  -- =====================
  -- === regular cases ===
  -- =====================
  -- compute mantissa and exponent
  local bias = 127
  local mantissa, exponent = math.frexp(number) -- this mantissa belongs to [0.5, 1)
  
  -- to simplify the problem, we convert and round the mantissa 
  -- to an integer number less than 2^24 and reformat the previous exponent
  mantissa = round(math.ldexp(mantissa, 24))
  exponent = exponent - 1
  mantissa_bin = binary(mantissa)
  exponent_bin = binary(bias + exponent)
  exponent_bin = padding_on_left(exponent_bin, 8)
  
  -- we set the first bit, according to sign
  local ans_sign = ''
  if (sign == 1) then 
    ans_sign = '0'
  else
    ans_sign = '1'
  end
  -- 8 bits for exponent + 23 bits for mantissa
  -- ans = ans .. exponent_bin .. string.sub(mantissa_bin, 2)
  return ans_sign, exponent_bin, string.sub(mantissa_bin, 2)
end

function num_to_ieee754(num)
  local s_sign, s_exponent, s_mantissa = components_ieee754(num)
  return tonumber(s_sign .. s_exponent .. s_mantissa)
end

-- ===============================================================
-- ===============================================================
-- some tests
testing_cases = {0, 
  0.121, 
  222, 
  -123.633, 
  0.145, 
  0.1,
  1079194419.000,
  1081711002.000
}

for i=1,#testing_cases do
  print('testing number: ' .. tostring(testing_cases[i]))
  print(components_ieee754(testing_cases[i]))
  print(num_to_ieee754(testing_cases[i]))
  print('\n')
end

 

Lua online compiler

Write, Run & Share Lua code online using OneCompiler's Lua online compiler for free. It's one of the robust, feature-rich online compilers for Lua language, running the latest Lua version 5.4. Getting started with the OneCompiler's Lua editor is easy and fast. The editor shows sample boilerplate code when you choose language as Lua and start coding.

Taking inputs (stdin)

OneCompiler's Lua online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Lua program which takes name as input and prints hello message with your name.

name = io.read("*a")
print ("Hello ", name)

About Lua

Lua is a light weight embeddable scripting language which is built on top of C. It is used in almost all kind of applications like games, web applications, mobile applications, image processing etc. It's a very powerful, fast, easy to learn, open-source scripting language.

Syntax help

Variables

  • By default all the variables declared are global variables
  • If the variables are explicitly mentioned as local then they are local variables.
  • Lua is a dynamically typed language and hence only the values will have types not the variables.

Examples

-- global variables
a = 10

-- local variables

local x = 30
Value TypeDescription
numberRepresents numbers
stringRepresents text
nilDifferentiates values whether it has data or not
booleanValue can be either true or false
functionRepresents a sub-routine
userdataRepresents arbitary C data
threadRepresents independent threads of execution.
tableCan hold any value except nil

Loops

1. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition)
do
--code
end

2. Repeat-Until:

Repeat-Until is also used to iterate a set of statements based on a condition. It is very similar to Do-While, it is mostly used when you need to execute the statements atleast once.

repeat
   --code
until( condition )

3. For:

For loop is used to iterate a set of statements based on a condition.

for init,max/min value, increment
do
   --code
end

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increase re-usuability and modularity.

optional_function_scope function function_name( argument1, argument2, argument3........, argumentn)
--code
return params with comma seperated
end