受启发的Lua程序在计算机和计算器上有所不同 [英] Inspired Lua Program is Different on Computer and Calculator
问题描述
我正在尝试为TI-Nspire CX CAS创建一个简单的二次公式程序.我似乎一切正确,并且可以在计算机上运行:
I am attempting to create a simple quadratic formula program for my TI-Nspire CX CAS. I seem to have everything correct, and it works on the computer:
但是,它在计算器上不起作用.我得到第二个正确的,但第一个是-4.44089 ... e-16. (不说...,只是使用它是因为我不想输入整个内容)
However, it doesn't work on the calculator. I get the second one correct, but the 1st is -4.44089...e-16. (doesn't say ..., just using it because I don't want to type out the whole thing)
(简化的)代码如下:
function quadraticA(f,s,t)
return ((-1*s)+math.sqrt(s^2-4*f*t))/(2*f)
end
function quadraticB(f,s,t)
return ((-1*s)-math.sqrt(s^2-4*f*t))/(2*f)
end
function on.paint(gc)
formula:setExpression("0s: "..quadraticA(tonumber(a),tonumber(b),tonumber(c)))
formulaB:setExpression(quadraticB(tonumber(a),tonumber(b),tonumber(c)))
end
为什么在计算器上然后在计算机上得到不同的答案?我该如何解决该问题?
Why do I get a different answer on the calculator then on the computer? How can I fix the issue?
提前谢谢!
推荐答案
正如Dimitry所指出的,本质上,我必须编写一个CAS引擎.这是lua的平方根简化:
As Dimitry pointed out, I must write, essentially, a CAS engine. Here is a square root simplification in lua:
function factors(a)
factorsOfA={}
counter = 0
for i = 1, a do
counter = counter + 1
if modulo(a,i) == 0 then
factorsOfA[counter]=i
end
end
return factorsOfA
end
function simplifySqrt(radicand)
radicandFactors = factors(radicand)
outsideRadicand = 1
for m,i in pairs(radicandFactors) do
if math.floor(math.sqrt(i))^2 == i then
outsideRadicand = outsideRadicand * math.floor(math.sqrt(i))
end
end
insideRadicand = radicand/outsideRadicand^2
return outsideRadicand.."sqrt("..insideRadicand..")"
end
我希望这会有所帮助!
这篇关于受启发的Lua程序在计算机和计算器上有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!