将公式从 Excel 复制到 ASP 得到不同的结果

发布于 2024-08-18 21:20:25 字数 1081 浏览 2 评论 0原文

我试图将 Excel 工作表中的公式复制到 ASP 页面上,但复制公式后得到了不同的结果(实际上是一个错误,因为 ASP 最终尝试对负数求平方根)。

Excel 处理运算符优先级的方式确实与经典 ASP 相同吗?

我试图复制的公式是一个简单的“T 检验”(报告统计显着性 - 我没有创建公式),并且在 Excel 中可以正常工作。在 Excel 中,单元格看起来像

A2 = 1098
B2 = 183

A4 = 20.4
B4 = 17.49

并且单元格 E4 包含此公式:

=(A4-B4)/SQRT(((($A$2*A4)+($B$2*B4))/($A$2+$B$2))*
(1-((($A$2*A4)+($B$2*B4))/($A$2+$B$2)))*((1/$A$2)+(1/$B$2)))

因此,我只需将该公式复制/粘贴到经典 ASP 中,删除所有 $ 并将 SQRT 更改为 SQR,声明 4 个变量 A2、B2、A4、B4。这将返回一个错误(无效的过程调用)。

如果我删除公式的第一部分 - (A4-B4)/SQRT - 在 ASP 中,代码将返回 -2.41868099141296,但在 Excel 中,它会返回 0.001019435。

我已经尝试过先分别计算公式的各个部分,然后将其放回一起准备进行 SQRT,但仍然面临同样的问题。

这只是一个愚蠢的错误吗? 有一次,我一直在研究负数的平方根,直到我在 Excel 中尝试 =SQRT(-9)

我正在使用的 ASP 代码是(我在第一个和最后一个位置插入了换行符)线):

A2 = cdbl(1098.0)
A4 = cdbl(20.4)
B2 = cdbl(183.0)
B4 =cdbl(17.49)

' Remove all the $, and (A4-B4)/SQRT part
response.write "<p>Result: " & ((((A2*A4)+(B2*B4))/(A2+B2))*(1-(((A2*A4)+(B2*B4))
/(A2+B2)))*((1/A2)+(1/B2))) & "</p>"`

I'm trying to replicate a formula from an Excel worksheet onto an ASP page, but having copied the formula over I'm getting different results (in fact an error because the ASP ends up trying to square root a negative number).

Surely Excel handles operator precedence the same as classic ASP?

The formula I'm trying to replicate is a simple "T-Test" (to report on statistical significance - I didn't create the formula) and works correctly in Excel. In Excel, the cells look like

A2 = 1098
B2 = 183

A4 = 20.4
B4 = 17.49

And cell E4 contains this formula:

=(A4-B4)/SQRT(((($A$2*A4)+($B$2*B4))/($A$2+$B$2))*
(1-((($A$2*A4)+($B$2*B4))/($A$2+$B$2)))*((1/$A$2)+(1/$B$2)))

So, I simply copy/pasted that formula into classic ASP, removed all the $ and changed SQRT to SQR, declared 4 variables A2, B2, A4, B4. That returns an error (invalid procedure call).

If I remove the first part of the formula - (A4-B4)/SQRT - in ASP the code returns -2.41868099141296, yet in Excel it's returning 0.001019435.

I have tried and tried calculating parts of the formula separately first, and then putting it back together ready to do the SQRT but keep facing the same thing.

Is this just a silly mistake? At one point I was looking into square roots of negative numbers until I tried =SQRT(-9) in Excel

ASP Code I'm using is (I've inserted a line break on the first and last line):

A2 = cdbl(1098.0)
A4 = cdbl(20.4)
B2 = cdbl(183.0)
B4 =cdbl(17.49)

' Remove all the $, and (A4-B4)/SQRT part
response.write "<p>Result: " & ((((A2*A4)+(B2*B4))/(A2+B2))*(1-(((A2*A4)+(B2*B4))
/(A2+B2)))*((1/A2)+(1/B2))) & "</p>"`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱成瘾 2024-08-25 21:20:25

我是杜甫。

在 Excel 电子表格中,A4 和 B4 列的格式为百分比。这些单元格中的实际值分别为 0.204 和 0.1749。当(几乎)逐字复制并粘贴到 ASP 中时,Excel 公式确实有效 - 我只是没有发现我的输入比预期大 100 倍。

下面的代码可以工作

A2 = cdbl(1098.0)
A4 = cdbl(0.204)
B2 = cdbl(183.0)
B4 =cdbl(0.1749)

response.write "<p>Result: " & (A4-B4)/SQR((((A2*A4)+(B2*B4))/(A2+B2))*
(1-(((A2*A4)+(B2*B4))/(A2+B2)))*((1/A2)+(1/B2))) & "</p>"

所以现在我只需要把它转换成一个函数。

I'm a dufus.

In the Excel spreadsheet columns A4 and B4 are formatted as percentage. The actual value in these cells are 0.204 and 0.1749 respectively. The Excel formula does work when copied and pasted (almost) literally into ASP - I just didn't catch that my inputs were 100x larger than expected.

The following code works

A2 = cdbl(1098.0)
A4 = cdbl(0.204)
B2 = cdbl(183.0)
B4 =cdbl(0.1749)

response.write "<p>Result: " & (A4-B4)/SQR((((A2*A4)+(B2*B4))/(A2+B2))*
(1-(((A2*A4)+(B2*B4))/(A2+B2)))*((1/A2)+(1/B2))) & "</p>"

So now I just need to convert that into a function.

深巷少女 2024-08-25 21:20:25

在您的 Excel 工作表中,您是否进行了任何输入验证,从而阻止您获取负数的根?

另外,能贴一下相关的ASP代码吗?这可能只是一个简单的转录错误......

In your Excel sheet, are you doing any input validation that's preventing you from getting the root of a negative number?

Also, could you post the relevant ASP code? It could just be a simple transcription error...

时光沙漏 2024-08-25 21:20:25

我认为这是 Excel 始终使用浮点数而 ASP 使用整数的情况(fi 1/A2 - 这是一个整数 - 可能会产生不同的结果。

尝试在整数计算中使用 cDbl( ... ) 。

I think it's a case of Excel always using floats and ASP using integers (f.i. 1/A2 - which is an integer - might produce different results.

Try using cDbl( ... ) around integer calculations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文