|
|
Subscribe / Log in / New account

Rust bandwagon

Rust bandwagon

Posted Jan 3, 2023 17:30 UTC (Tue) by fenncruz (subscriber, #81417)
In reply to: Rust bandwagon by summentier
Parent article: Welcome to 2023

It's pretty good for the bits it's strong in, number crunching. Would I want to write an OS in it? No, but I would/do use it to process a lot of data or run big simulations. Being compiled means lots of bad programming gets rewritten by the compiler (e.g., looping over arrays in the wrong order). There are also nice language features which I find nicer than say C. For instance, arrays/strings know their lengths (so no '\0' characters to worry about) and thus it becomes much harder to overflow a buffer. Fortran's version of malloc, called an allocatable array, also gets deallocated when it goes out of scope (so it's much harder, but not impossible, to leak memory). The downsides are no pointer voodoo-magic like in C (maybe that's a good thing?) and after writing a lot of Python, I dislike Fortran's string handling with a passion.

Perhaps its biggest strength (and weakness) is its backwards compatibility. Fortran versions are more of a guideline than a rule. So in your projects, you can combine different versions of Fortran, even in the same file. After living through the python 2 to 3 transition, Fortran's ability to still run code from the '60s unmodified is a miracle. Of course, we get stuck with language constructs that we would rather leave in the 60s, but I guess that is the price to pay.


to post comments

Rust bandwagon

Posted Jan 3, 2023 18:49 UTC (Tue) by Wol (subscriber, #4433) [Link] (5 responses)

> Fortran's ability to still run code from the '60s unmodified is a miracle.

Just watch out for that FOR LOOP. (I think I've got the right construct.)

The semantics between FORTRAN and Fortran is subtly different which can do serious damage if you don't realise the FORTRAN guy relied on it.

Namely

FOR I = 10 TO 1
...
...
NEXT

will execute the code in the loop if compiled with FORTRAN, but will skip it if compiled with Fortran.

So be careful, boys and girls :-)

Cheers,
Wol

Rust bandwagon

Posted Jan 3, 2023 19:15 UTC (Tue) by fenncruz (subscriber, #81417) [Link] (4 responses)

No, you dont.

Fortran does not have have for loops is has do loops, there is no NEXT statement, nor do you use TO, to sepcify a range. All versions of fortran would use: do I=10,1 which would not execute. The difference with between versions is modern fortran would recommend an end do statement to terminate a loop while older fortran would use a numbered label to specify the end of the loop.

Perhaps you have some very non standard vendor extensions but that is not standard fortran in any version.

Rust bandwagon

Posted Jan 3, 2023 20:26 UTC (Tue) by Wol (subscriber, #4433) [Link] (3 responses)

So I remembered incorrectly, BUT.

FORTRAN *would* execute the body of the loop. Once.

Fortran, as you say, wouldn't.

(Unless you use compiler specific extensions - the F77 compiler I used had a switch to enable the old FTN behaviour.)

I'm old enough to remember the difference between FORTRAN and Fortran :-) I've never heard of fortran.

Cheers,
Wol

Rust bandwagon

Posted Jan 3, 2023 20:46 UTC (Tue) by Wol (subscriber, #4433) [Link] (2 responses)

Whoops, I think I'm misleading myself slightly (but my point still stands) ...

DO I=10,1 would probably do an implicit STEP -1

J=10
K=1
DO I=K,J would not execute in Fortran, but would execute once with I set to 10 in FORTRAN.

Cheers,
Wol

Rust bandwagon

Posted Jan 7, 2023 9:31 UTC (Sat) by joib (subscriber, #8541) [Link] (1 responses)

In Fortran 66 (the first published 'official' standard) zero trip DO loops are indeed not possible, but that is enforced via the requirement that for the DO statement

DO I=m1, m2

then m1 must be <= m2. So the loop in your example is invalid. You're most likely describing some compiler-specific extension (or accidental behavior later documented by the compiler developers as expected behavior. :) ).

Rust bandwagon

Posted Jan 7, 2023 17:25 UTC (Sat) by Wol (subscriber, #4433) [Link]

Actually, I suspect I'm describing the normal behaviour of FORTRAN IV ... :-)

Fortran allows a lot of (specified) behaviour in the name of optimisation, that can lead to unexpected results. Like storing the index of a do loop in a register, such that it can only safely be read, and not relied on when the loop exits. It seems highly likely that most FORTRAN compilers (and certainly the one I was using in 1983, iirc) did not bother to check the loop limits. Given that I understood that FORTRAN explicitly said the index,limit check was done at the *end* of the loop, I would be surprised if there was a special check on entering the loop.

When Fortran moved the check to the start of the loop, then it makes sense that loops can execute zero times.

Cheers,
Wol


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds