Java 中的 Fortran GOTO

发布于 2024-12-10 19:59:07 字数 856 浏览 0 评论 0原文

是的,我研究了在 java 中实现 GOTO 的各种方法,但这是现实世界: 我需要将最新的 fortran LAPACK 例程之一转换为 java, 看 http://www.netlib.org/lapack/timing/eig/eigsrc /dlasq3.f 例如:

10 CONTINUE
      IF( N0.LT.I0 )
     $   RETURN
      IF( N0.EQ.I0 )
     $   GO TO 20
      NN = 4*N0 + PP
      IF( N0.EQ.( I0+1 ) )
     $   GO TO 40
      OPS = OPS + DBLE( 3 )
      IF( Z( NN-5 ).GT.TOL2*( SIGMA+Z( NN-3 ) ) .AND.
     $    Z( NN-2*PP-4 ).GT.TOL2*Z( NN-7 ) )
     $   GO TO 30
   20 CONTINUE
      fortran code ...
      GO TO 10
   30 CONTINUE
      OPS = OPS + DBLE( 2 )
      IF( Z( NN-9 ).GT.TOL2*SIGMA .AND.
     $    Z( NN-2*PP-8 ).GT.TOL2*Z( NN-11 ) )
     $   GO TO 50
   40 CONTINUE
      fortran code ...
      GO TO 10
   50 CONTINUE

处理所有可能的 GOTO 的“标准”方法是什么?

Yes, I looked at various ways of implementing GOTO in java, but here is the real world:
I need one of the latest fortran LAPACK routine converted to java,
see
http://www.netlib.org/lapack/timing/eig/eigsrc/dlasq3.f
e.g.:

10 CONTINUE
      IF( N0.LT.I0 )
     $   RETURN
      IF( N0.EQ.I0 )
     $   GO TO 20
      NN = 4*N0 + PP
      IF( N0.EQ.( I0+1 ) )
     $   GO TO 40
      OPS = OPS + DBLE( 3 )
      IF( Z( NN-5 ).GT.TOL2*( SIGMA+Z( NN-3 ) ) .AND.
     $    Z( NN-2*PP-4 ).GT.TOL2*Z( NN-7 ) )
     $   GO TO 30
   20 CONTINUE
      fortran code ...
      GO TO 10
   30 CONTINUE
      OPS = OPS + DBLE( 2 )
      IF( Z( NN-9 ).GT.TOL2*SIGMA .AND.
     $    Z( NN-2*PP-8 ).GT.TOL2*Z( NN-11 ) )
     $   GO TO 50
   40 CONTINUE
      fortran code ...
      GO TO 10
   50 CONTINUE

What would be a "standard" way to deal with all possible GOTOs?

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

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

发布评论

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

评论(2

甜心 2024-12-17 19:59:07

处理这个问题的最佳方法是将每个逻辑块组成一个部分,并为整个函数制作一个状态图。

不要忘记,越过状态的开始被视为过渡,并且应该如此对待。当您将它们分解为状态转换时,您可以开始了解在哪里可以将它们简化为少数函数,并在必要时应用递归或迭代。

现在,我完全承认我不理解该函数或它正在做什么或应该做什么,但这是制作状态图的第一次尝试,让您了解我的意思。注意 80 上的循环,可能需要一个循环。请注意,10 和 100 是您唯一的返回状态。请注意,一旦从 30 岁上升到 50 岁,就无法回头。这对我来说意味着 50+ 可能是它自己的独立函数,而 10-40 是它自己的带有循环的函数,当它达到 30 时,会说 return functionRepresenting50Pluss(...)

在此处输入图像描述

请注意,某些状态转换上的填充方块表示如果没有其他转换离开该状态,则保证选择该转换。请注意,它在 80 上不存在,因为我无法真正确定 80 还是 90 是它的保证目的地。有没有可能某个东西会永远围绕 80 循环?如果不了解该功能,我无法说。

The best way to handle this is to compose each logical block as a section and make a state diagram for the entire function.

Don't forget that falling through past the start of a state is considered a transition and should be treated as such. When you have these broken out into their state transitions, you can start to see where they can be reduced into a handful of functions, applying recursion or iteration where necessary.

Now, I fully admit that I don't understand the function or what it's doing or supposed to do, but this was a first attempt at making the state diagram, to give you an idea of what I mean. Notice the loop on 80, probably going to need a loop. Notice that 10 and 100 are your only return states. Notice how once you go from 30 to 50 there is no going back. That indicates to me that 50+ might be it's own isolated function, while 10-40 is it's own function with a loop that when it hits 30 says return functionRepresenting50Pluss(...)

enter image description here

Just a note, the filled in squares on some state transitions indicate this transition is guaranteed to be chosen if no other transition is made away from the state. Notice it doesn't exist on 80 because I couldn't really decide if 80 or 90 was it's guaranteed destination. It could be it's possible for something to loop around 80 forever? Without understanding the function more I can't say.

清欢 2024-12-17 19:59:07

GOTO 被认为是一种反模式。在不考虑重新设计代码的情况下,切勿尝试将其直接转换为 Java。

例如,当您看到 GOTO 标签时,这可能表明该代码将被重用。它应该属于将来再次调用的方法吗?使用面向对象来实现新设计,而不是使用与 FORTRAN 中相同的过程序列。

Java 确实可以在没有 GOTO 的情况下在现实世界中工作。

GOTOs are considered an anti-pattern. You should never try to convert it straight to Java without considering redesigning the code.

For example, when you see a label for GOTO, that is likely a sign that this code would be reused. Should it belong in a method to be called again in the future instead? Approach the new design using OO rather than using the same procedural train of though as that in FORTRAN.

Java does work in the real world without GOTOs.

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