【问题标题】:Why does Lua truncate division to first digit?为什么Lua会将除法截断到第一位?
【发布时间】:2014-11-03 18:04:03
【问题描述】:

我编写了一个输出值表的小脚本(我在 Lua 中处于非常基础的水平):

-- 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(...)

脚本调用的函数如下:

-- 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

虽然还是有一些问题,但是如果我注释掉函数中return之前的最后三行似乎可以正常工作。但是如果我“启用”它们,我会得到值被四舍五入到它们的第一个数字,而我希望 Lua 的默认行为(如>=5/6。)

从终端调用脚本(上述行被注释掉)我得到以下输出

$ 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

这基本上是正确的,但是在第二列中我得到了一个“绝对频率”,而我想要一个“相对频率”。

如果我“启用”函数的最后几行,我会得到:

$ 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

应该是这样的:

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

这是为什么呢?

附加信息:

看看这个问题可以改进SumOfDigits 函数:Sum of the digits of an integer in lua,但我想尽可能多地挽救我的代码。

【问题讨论】:

  • 您能否指定循环前C 中的值、循环后C 中的值以及您期望的值?顺便说一句,说明问题的一个最小示例将由一个包含样本输入值的数组、您的循环和循环后的输出值组成。
  • @siffiejoe:我已经更新了我的问题。这够了吗?
  • 显然如此。我认为 rpattiso 发现了问题...

标签: debugging lua


【解决方案1】:

看看你的格式字符串:

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

您希望将输出打印为浮点数 %f 而不是整数 %d

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

基本上,说明符%d 要求它将值打印为整数。 此文档仅引用 C printf。看看 C 的 printf 格式说明符,大部分都可以在 Lua 中使用。

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

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 2018-03-17
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多