简单 Fortran 函数返回奇数值
作为一名前锋,我对 Fortran 完全是个初学者。我花了很长时间查看 SO 上的其他问题,但我无法识别与此类似的问题,所以如果我的解决方案是显而易见的,或者已经得到回答,请原谅我:)
我正在尝试学习如何在 VB.net 应用程序中正确实现自编写的 Fortran DLL。我已经能够让 VB 识别该 DLL,并执行该函数而不会出现任何错误。与实际输出相比,错误的出现更像是预期的输出。
我的 Fortran DLL 函数如下:
function ex(i)
integer*4 i
ex=i+1
return
end
一个非常简单的函数,它将传递的参数加一并返回值。 (我认为)。 VB 应用程序具有以下代码。
<DllImport("ex.dll")> _
Public Shared Function ex(ByRef val As Integer) As Integer
End Function
Private Sub btn_Fortran_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Fortran.Click
Console.WriteLine(ex(1))
End Sub
因此,我向 ex 函数传递整数值 1。因此我希望将值 2 写入控制台。相反,我得到的值“1073741824”不完全相等。我有什么明显不足的想法吗?
Just as a forward, I am a complete beginner when it comes to Fortran. I've spent quite awhile looking at the other questions on SO, but I couldn't identify a similar question to this, so please forgive me if my solution is either obvious, or already been answered :)
I'm attempting to learn how to correctly implement a self-written Fortran DLL in a VB.net application. I've been able to have VB recognize the DLL, and execute the function without any errors. The error comes rather as expected output compared to actual output.
My Fortran DLL function reads as follows:
function ex(i)
integer*4 i
ex=i+1
return
end
A very simple function that increments the passed parameter by one and returns the value. (I think). The VB Application has the following code.
<DllImport("ex.dll")> _
Public Shared Function ex(ByRef val As Integer) As Integer
End Function
Private Sub btn_Fortran_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Fortran.Click
Console.WriteLine(ex(1))
End Sub
So, I'm passing the ex function the integer value 1. So I would expect the value 2 to be written to the console. Instead, I get the value "1073741824" Not exactly equal. Any ideas where I'm obviously falling short?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在混合语言内容中学习语言是“一件很难的事”。请注意,您获得的值为 2**30。
在 fortran 部分,您还应该声明函数的返回值:“integer*4 function ex (i)”是老式的方法。您可能通过隐式类型将 ex 视为真实的。在所有程序和过程中包含“implicit none”以防止隐式类型是一个非常好的主意。许多编译器都包含一个用于相同目的的选项。
后期编辑:
下面的程序通过显示当位模式实值 2.0 被解释为整数时获得的值来演示所发生的情况。首先,该程序将一个实数和一个整数相等。在这种情况下,编译器“知道”类型并转换值。在第二种情况下,原始位模式在不进行转换的情况下进行传输。
输出是:
Learning a language in a mixed language content is "a hard row to hoe". Note that value you obtained is 2**30.
In the fortran portion, you should also declare the return value of the function: "integer*4 function ex (i)" is the old fashioned way. You are probably getting ex as a real through implicit typing. It is a very good idea to include "implicit none" in all of your programs and procedures to prevent implicit typing. Many compilers include an option for the same purpose.
Late edit:
Here is a program that demonstrates what was happening by showing what value is obtained when the bit-pattern real value 2.0 is interpreted as an integer. First the program equates a real and an integer. In this case the compiler "knows" about the types and converts the value. In the second case the raw bit pattern is transferred without being converted.
Output is:
看来我几乎走上了正轨,但我宣布“我”的方式却导致了一些奇怪的事情发生。当使用以下约定时
函数返回正确的值。所以,我的功能看起来像这样
谢谢你们的帮助。我对你们俩投了赞成票,因为你们让我看到了我之前不完全理解的语言的某些方面。
It appears that I was nearly on the right track, but the way in which I declared 'i' made some weird things happen. When using the following convention of
The function returns the correct value. So, my function looks like this
Thanks both of you for the help. I upvoted both of you for simply opening my eyes to some aspect of the language I didn't fully understand beforehand.