Pier Paolo
Pier Paolo

Reputation: 920

Why does Lua truncate division to first digit?

I have written a small script (I am at a very basic level in Lua) that outputs a table of values:

-- ProbabilityOfOralExam.lua
-- This script outputs a data file where in the first column are
-- written the numbers that are the possible sum of the digits for
-- the numbers belonging to Range and in the second column is
-- computed how many times that number is repeated in Range.
--
-- $ lua ProbabilityOfOralExam.lua RANGE OUTFILE
-- (OUTFILE = outprob.txt)
--
-- If RANGE is not given, default is 100.
-- If OUTFILE is not given, standard output will be used.
--
-- ATTENTION: ATM DOESN'T WORK IF range IS NOT GIVEN.
dofile("./statistic.lua")

function Main (Range, OutFileName)
   Range = Range or 100
   local OutHandle = OutFileName and io.open(OutFileName, "w") 
      or io.stdout
   local C, A = Statistic(Range)
   for k, v in ipairs(C) do
      -- to be improved
      OutHandle:write(string.format("%d  %d\n", k, v))
   end
   OutHandle:close()
end

Main(...)

and the function the script calls upon is the following:

-- statistic.lua
function Statistic (Range)
   -- Store in the array `A' the numbers from 1 to Range (as the 
   -- key/index) and the sum of their digits (as value.)
   local A = {}
   -- Computes the sum of the digits that Number is made of.
   local function SumOfDigits (Number)
      -- We treat Number as a string.
      Number = tostring(Number)
      -- Decompose Number (now a string) in its digits and store 
      -- them in the array B.
      local B = {}
      for I = 1, #Number do
     B[I] = string.sub(Number, I, I)
      end
      -- Sum the values in B to get the sum of the digits of Number.
      local Sum = 0
      for key, value in ipairs(B) do
     Sum = Sum + B[key]
      end
      return Sum
   end
   for I = 1, Range do
      table.insert(A, SumOfDigits(I))
   end
   -- Find the highest value in A.
   local MaxSum = math.max(table.unpack(A))
   -- Store in `C' all the numbers from 1 to MaxSum (which are all
   -- the possible sums of digits for numbers in Range) and the 
   -- times they appear.
   local C = {}
   for I = 1, MaxSum do
      C[I] = 0
      for key, value in ipairs(A) do
     if I == value then
        C[I] = C[I] +1
     end
      end
   end
   --[[ Turn absolute frequency into relative frequency.
   for k, v in ipairs(C) do
      C[k] = (C[k] / Range) * 100
   end
   --]]
   return C, A
end

Although there are still some issues, it seems to work fine if I comment out the last three lines in the function before return. But if I "enable" them, I get that values are rounded to their first digit, while I would like Lua's default behaviour (like in >=5/6.)

Calling the script from terminal (with the aforementioned lines commented out) I get the following output

$ lua ProbabilityOfOralExam.lua 500
1  3
2  6
3  10
4  15
5  21
6  25
7  30
8  35
9  40
10  43
11  44
12  43
13  40
14  35
15  30
16  25
17  20
18  15
19  10
20  6
21  3
22  1

which is basically correct, but in the second column I get an "absolute frequency", while I would like a "relative frequency."

If I "enable" the last lines of the function, I get:

$ lua ProbabilityOfOralExam.lua 500
1  0
2  1
3  2
4  3
5  4
6  5
7  6
8  7
9  8
10  8
11  8
12  8
13  8
14  7
15  6
16  5
17  4
18  3
19  2
20  1
21  0
22  0

while it should be:

1  0.6 -- = 3/500*100
2  1.2 -- = 6/500*100
3  2.0 -- = 10/500*100
...

Why is that?

Additional Info:

The function SumOfDigits could be improved looking at this question: Sum of the digits of an integer in lua, but I would like to salvage my code as much as it is possible.

Upvotes: 1

Views: 276

Answers (1)

ryanpattison
ryanpattison

Reputation: 6251

Take a look at your format string:

OutHandle:write(string.format("%d  %d\n", k, v))

You want the output to be printed as floats %f instead of integers %d.

    OutHandle:write(string.format("%d  %f\n", k, v))

Basically the specifier %d asks it to print the value as a whole number. The documentation for this only references the C printf. Have a look at C's printf format specifiers, most of them can be used in Lua.

http://www.cplusplus.com/reference/cstdio/printf/

Upvotes: 5

Related Questions