Recent

Author Topic: Remove goto from procedure  (Read 5912 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 16532
  • Kallstadt seems a good place to evict Trump to.
Re: Remove goto from procedure
« Reply #15 on: February 07, 2024, 04:51:54 pm »
goto: Spaghetti
Goto removed: structured.
The program flow is easier to digest.

@wp
I would not worry too much about the license:
This only hold for textual content, not for the sourcecode, because that is almost completely based on previous, prior knowledge, which makes such claims invalid.
Implementing the same algorithm in a different programming language doesn’t necessarily grant new copyright, because the algorithm itself can be copyrighted - but usually: Copyright primarily covers the expression, not the underlying idea or concept. Which ties it to implementing language, imnsho.
« Last Edit: February 07, 2024, 05:16:11 pm by Thaddy »
But I am sure they don't want the Trumps back...

Thaddy

  • Hero Member
  • *****
  • Posts: 16532
  • Kallstadt seems a good place to evict Trump to.
Re: Remove goto from procedure
« Reply #16 on: February 07, 2024, 04:55:36 pm »
It's the FORTRAN legacy.
No it is a very low level substitution of an asm compare/jump instruction which is hardware and much older than even fortran.
In the case of goto removed, try examining the assembler for any compiled language and any CPU. Goto's (compare/jumps) all over the place.
« Last Edit: February 07, 2024, 05:10:48 pm by Thaddy »
But I am sure they don't want the Trumps back...

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 594
Re: Remove goto from procedure
« Reply #17 on: February 07, 2024, 06:05:55 pm »
No it is a very low level substitution...

I'm afraid you miss the point.  All of the algorithms in my '86 edition are written in FORTRAN with Pascal translations (such as they are) packed into the last chapter. 

Otherwise yeah -- I sort of knew how goto works.

(BTW -- I wrote some of the code for the TI 3x - 4x - series calculators back in the '70s.  Mundane stuff (display, switch debounce and such).  There was a team of about four guys that did the serious math code and testing.)
« Last Edit: February 07, 2024, 06:30:38 pm by Curt Carpenter »

440bx

  • Hero Member
  • *****
  • Posts: 5001
Re: Remove goto from procedure
« Reply #18 on: February 07, 2024, 06:06:47 pm »
Probably this one: https://en.wikipedia.org/wiki/Numerical_Recipes
https://books.google.de/books/about/Numerical_Recipes_in_Pascal.html?id=69uJzQEACAAJ&redir_esc=y

Acc. to a PDF for the C-Source-code, the Function "hqr" computes "eigenvalues of a Hessenberg matrix"

EDIT: FWIW, the C-Source-Code has no Goto's, so whoever ported that to Pascal.....
Thank you for providing that information.

Between what the Wikipedia article states and the rather poor Pascal code, I'm going to stay away from this one... <chuckle>
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Zvoni

  • Hero Member
  • *****
  • Posts: 2844
Re: Remove goto from procedure
« Reply #19 on: February 08, 2024, 08:50:32 am »
Well, without proper definitions for all the types, and without a functions definition for sign(), the code cannot compile, so it cannot be tested.
I looked at the C-Source again
Code: C  [Select][+][-]
  1. /* in "nrutil.h" */
  2. #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
Huh?
I was always under the impression, a SIGN-Function actually returns the "Sign" in the form of
Code: Pascal  [Select][+][-]
  1. If arg>0 Then
  2.    Result:=1
  3. Else If arg=0 Then
  4.    Result:=0
  5. Else IF arg<0 Then
  6.    Result:=-1;
Admittedly, THAT Sign-Function takes only one Argument as compared to the Sign-Function used in that Algorithm (2 Arguments), but boy, this looks weird to me.

EDIT: Noticed something else:
His first two loops (Line 8 and 9) looks "wrong" to me
Code: Pascal  [Select][+][-]
  1. for i := 2 to n do
  2.        for j := i-1 to n do
From C-Source
Code: C  [Select][+][-]
  1. for (i=1;i<=n;i++)
  2.    for (j=IMAX(i-1,1);j<=n;j++)
  3.       anorm += fabs(a[i][j]);
His outer loop starts at 2, the C-Loop starts at 1, nevermind the IMAX in the second loop,
with IMAX being a simple MAX-Macro for Integers
Code: C  [Select][+][-]
  1. static int imaxarg1,imaxarg2;
  2. #define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ?\
  3.         (imaxarg1) : (imaxarg2))

And his Line 5: What are "TElems"?
In the C-Source those are "Floats"

I'm probably going to take a stab at it porting it to Pascal if i'm not bogged down with other things
« Last Edit: February 08, 2024, 10:51:31 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2844
Re: Remove goto from procedure
« Reply #20 on: February 08, 2024, 01:17:05 pm »
Right. I took a stab at it.
What i thought: No Goto's necessary. And if i see this correctly even clearly distinct differences to the code in Post 1.

It..... compiles..... No idea how to test it.

And no idea, if i'm allowed to post it here.
The legalese in that PDF just gives me headaches..... ;-)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

tetrastes

  • Hero Member
  • *****
  • Posts: 633
Re: Remove goto from procedure
« Reply #21 on: February 08, 2024, 06:37:34 pm »
Well, without proper definitions for all the types, and without a functions definition for sign(), the code cannot compile, so it cannot be tested.
I looked at the C-Source again
Code: C  [Select][+][-]
  1. /* in "nrutil.h" */
  2. #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
Huh?

This is port of standard FORTRAN function, see f.e. https://gcc.gnu.org/onlinedocs/gfortran/SIGN.html.
The "native" language of NR is FORTRAN, AFAIK.

Nicole

  • Hero Member
  • *****
  • Posts: 1009
Re: Remove goto from procedure
« Reply #22 on: February 08, 2024, 07:07:29 pm »
Sometimes the case syntax is useful.
Every time I need it, I forget how it has worked.
Therefore I save the syntax for it, see below:

assume the value of "eingabe" is 1 then the first line would to it fine.
You can define actions for each value and even group of values of eingabe.

(translation of the first line from German to English
1: output:= 'You keyed in 1' ; )

case eingabe of
 1: ausgabe := 'Sie haben 1 eingegeben';
 2: ausgabe := 'Sie haben 2 eingegeben';
 3: ausgabe := 'Sie haben 3 eingegeben';
 else ausgabe := 'Sie haben nicht 1, 2 oder 3 eingegeben';
end;


case eingabe of
 1,3,5,7,9: ausgabe := 'Sie haben eine ungerade Zahl kleiner als 10 eingegeben';
 2,4,6,8,0: ausgabe := 'Sie haben eine gerade Zahl kleiner als 10 eingegeben';
 10..20:    ausgabe := 'Sie haben eine Zahl zwischen 10 und 20 eingegeben';
end;
« Last Edit: February 08, 2024, 08:23:52 pm by Nicole »

Thaddy

  • Hero Member
  • *****
  • Posts: 16532
  • Kallstadt seems a good place to evict Trump to.
Re: Remove goto from procedure
« Reply #23 on: February 08, 2024, 07:38:01 pm »
case with ordinals is always way more efficient then comparisons like if the else, especially if they are in natural order (which in our case the compiler optimizes for). Alas FPC supports it for strings too, which is a fallacy.
But I am sure they don't want the Trumps back...

Bart

  • Hero Member
  • *****
  • Posts: 5511
    • Bart en Mariska's Webstek
Re: Remove goto from procedure
« Reply #24 on: February 08, 2024, 10:16:11 pm »
Alas FPC supports it for strings too, which is a fallacy.
No, it's a nice feature and makes for more readable code then multilpe if..then..else.
As long as you remember that the code actually just translates to that.
And you better not use ranges.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 16532
  • Kallstadt seems a good place to evict Trump to.
Re: Remove goto from procedure
« Reply #25 on: February 09, 2024, 09:35:10 am »
And you better not use ranges.
You mean overlapping ranges I guess? Because that will throw a duplicate case label.
But I am sure they don't want the Trumps back...

Zvoni

  • Hero Member
  • *****
  • Posts: 2844
Re: Remove goto from procedure
« Reply #26 on: February 09, 2024, 11:38:16 am »
How did we get from a badly ported code to a discussion about the case-operator?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Remove goto from procedure
« Reply #27 on: February 09, 2024, 01:49:49 pm »
Despite in most cases (also in the case presented here) using goto is not recommended nor necessary, there are some situations where I personally found much more effective to use this construct, and obtaining the same logic without would have produced a much less intelligible code structure.

Moreover, there are important projects where usage of goto is not only allowed, but also recommended.

https://www.kernel.org/doc/html/v4.19/process/coding-style.html#centralized-exiting-of-functions
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Zvoni

  • Hero Member
  • *****
  • Posts: 2844
Re: Remove goto from procedure
« Reply #28 on: February 09, 2024, 02:23:26 pm »
I‘m not denying there possibly being situations that justify Goto, but I haven’t come across them

That said: while porting C-Code to pascal, and encountering a Goto for premature exit (with Cleanup) i could always solve it with a nested boolean-function, doing cleanup, that always returns true.

That way i always could do in pascal
Code: Pascal  [Select][+][-]
  1. If SomeErrorCondition Then If CleanUp(SomeArgument) Then Exit;
With „CleanUp“ being my nested boolean-function (Arguments optional)
No need for Goto, pretty easy to read source code
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dseligo

  • Hero Member
  • *****
  • Posts: 1461
Re: Remove goto from procedure
« Reply #29 on: February 09, 2024, 02:45:59 pm »
That said: while porting C-Code to pascal, and encountering a Goto for premature exit (with Cleanup) i could always solve it with a nested boolean-function, doing cleanup, that always returns true.

Why do you use function that always returns true?

Why not like that (procedure instead of function):
Code: Pascal  [Select][+][-]
  1. If SomeErrorCondition Then
  2. begin
  3.   CleanUp(SomeArgument);
  4.   Exit;
  5. end;

 

TinyPortal © 2005-2018