Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua的数学函数问题_Lua - Fatal编程技术网

Lua的数学函数问题

Lua的数学函数问题,lua,Lua,我们把任务交给了他 以度为单位读取角度,并按相同顺序打印该角度的tan、cosec、sec和cot值。要通过rad函数使用,请将标准库函数的角度从度转换为弧度 为此,我们编写了如下代码: x = io.read() radianVal = math.rad (x) io.write(string.format("%.1f ", math.tan(radianVal)),"\n") io.write(string.format("%.1f &

我们把任务交给了他

以度为单位读取角度,并按相同顺序打印该角度的tan、cosec、sec和cot值。要通过rad函数使用,请将标准库函数的角度从度转换为弧度

为此,我们编写了如下代码:

x = io.read()

radianVal = math.rad (x)

io.write(string.format("%.1f ", math.tan(radianVal)),"\n")

io.write(string.format("%.1f ", math.cosh(radianVal)),"\n")

io.write(string.format("%.1f ", math.sinh(radianVal)),"\n")

io.write(string.format("%.1f ", math.cos(radianVal)),"\n")
但产出不如预期,不确定哪里出了问题

Run as Custom Input
30

Your Output (stdout)
0.6 
1.1 
0.5 
0.9 

Expected Output
0.57735026918963
2.0
1.1547005383793
1.7320508075689
工作正常的正确代码是

x = io.read()

function cot(radAngle)
  return 1 / math.tan(radAngle)
end

function cosec(radAngle)
  return 1 / math.sin(radAngle)
end

function sec(radAngle)
  return 1 / math.cos(radAngle)
end

local radAngle = math.rad(x)
print(math.tan(radAngle))
print(cosec(math.rad(x)))
print(sec(math.rad(x)))
print(cot(math.rad(x)))

如果计算coshx而不是cscx,您如何期望得到正确的结果?你用双曲余弦来计算余割。我认为很明显为什么你的结果不被接受

使用sinhx计算secx,使用cox计算cotx

你混淆了数学函数。这不是Lua编程的问题

我建议你花几个小时研究三角函数

使用正确的公式或函数计算这些值。而不是使用数学库中具有类似名称的任何函数

自Lua 5.3 btw以来,cosh已被弃用

可以使用math.tan计算角度的tangens

对于割线、余割和余割,您必须实现自己的函数

还请注意,小数位数可能会导致结果验证出现问题。确保可以四舍五入到小数点后1位

例如:

没有计算角度余切的函数。所以我们定义了一个

function cot(radAngle)
  return 1 / math.tan(radAngle)
end

local radAngle = math.rad(30)
print("tan(30°):", math.tan(radAngle))
print("cot(30°):", cot(math.rad(30)))

->
tan(30°):   0.57735026918963
cot(30°):   1.7320508075689

我们现在使用x=io.read radianVal=math.rad x io.writestring.format%.1f,math.tanradianVal\n io.writestring.format%.1f,math.cscradianVal\n io.writestring.format%.1f,math.secradianVal\n io.writestring.format%.1f,math.cotradinval,\n获取错误为lua5.3:Solution.lua:11:尝试调用一个空值字段'cot'堆栈回溯:Solution.lua:11:在主块[C]:中?我告诉过你,你必须自己实现这些函数。没有math.cot、math.sec和math.csc。因此,您不能调用这些函数。请阅读我的答案,而不仅仅是其中的一部分。在尝试使用任何功能之前,请参阅Lua手册。这将告诉您这些函数是否存在以及如何正确使用它们。您可以从现有函数计算所有这些值。谢谢,但您可以澄清这一点,读取角度(以度为单位),并通过rad函数使用,将标准库函数的角度从度转换为弧度。同样对于tan,我们得出的值为0.6,应该是0.57735026918963,如何进行编辑。您的0.6是由舍入0.577引起的。。。到1位数。在开始编程之前,请先学习一些数学基础知识。感谢在考虑您的意见并再次学习数学函数后问题得到解决