Fortran was there first...and got it right...you can change Fortran to
start at other index values for many cases.
A reasonable language would let you specify the lower index. Fortran
and C are both retarded.
A reasonable language would let you specify the lower index. Fortran.
and C are both retarded.
“Why do arrays start at 0?" https://buttondown.email/hillelwayne/archive/why-do-arrays-start-at-0/.
"It's not the reason you think. No, it's not that reason either.”
My Fortran starts at one. My C++ starts at zero. This has made my life
hell.
On Saturday, August 27, 2022 at 6:49:55 AM UTC+10, Lynn McGuire wrote: FORTRAN followed centuries-old mathematical convention.Nope. Just as the linked article mentions, in Mathematics, starting an index from 0 or 1 are both often used, e.g., for elements of sequences and series, 0 seems to be most often used, for vector and matrix indices, 1. If a convergence of a sequence is
FORTRAN started at 1 for subscripts.
On Saturday, August 27, 2022 at 5:31:37 AM UTC+2, Robin Vowels wrote:
On Saturday, August 27, 2022 at 6:49:55 AM UTC+10, Lynn McGuire wrote: FORTRAN followed centuries-old mathematical convention.Nope. Just as the linked article mentions, in Mathematics, starting an index from 0 or 1 are both often used, e.g., for elements of sequences and series, 0 seems to be most often used, for vector and matrix indices, 1.
If a convergence of a sequence is considered, you often don't care about
"early" elements, so, if that yields a simple formula, you start from any
number convenient.
In Physics, you often start vector and matrix indices with 0 if the zeroth component is in some sense distinguished (e.g., in relativistic theories, distinguishing the time-like coordinate from the 3 space-like ones).
On Saturday, August 27, 2022 at 5:31:37 AM UTC+2, Robin Vowels wrote:.
On Saturday, August 27, 2022 at 6:49:55 AM UTC+10, Lynn McGuire wrote: FORTRAN followed centuries-old mathematical convention.Nope. Just as the linked article mentions, in Mathematics, starting an index from 0 or 1 are both often used, e.g., for elements of sequences and series, 0 seems to be most often used,
If a convergence of a sequence is considered, you often don't care about "early" elements, so, if that yields a simple formula, you start from any number convenient. In Physics, you often start vector and matrix indices with 0.
if the zeroth component is in some sense distinguished (e.g., in relativistic theories, distinguishing the time-like coordinate from the 3 space-like ones)..
FORTRAN started at 1 for subscripts.
Now for this case, you want a DO loop to start at zero, which Fortran 66 didn't allow.
Really? You mean that
DO 10 I=0,1
would not be allowed?
You probably meant zero-trip loops which were undefined.
On 8/27/22 01:47, Thomas Koenig wrote:
Lynn McGuire schrieb:
“Why do arrays start at 0?"
https://buttondown.email/hillelwayne/archive/why-do-arrays-start-at-0/
"It's not the reason you think. No, it's not that reason either.”
My Fortran starts at one. My C++ starts at zero. This has made my life
hell.
If you want to declare your Fortran arrays to start at zero, just
declare them with a lower bound of zero, like
real, dimension(0:n-1) :: aCan you declare other lower bounds the same way?
“Why do arrays start at 0?"
https://buttondown.email/hillelwayne/archive/why-do-arrays-start-at-0/
"It's not the reason you think. No, it's not that reason either.”
My Fortran starts at one. My C++ starts at zero. This has made my life hell.
Lynn
program main
implicit none
! these indices are seen in local scope
integer :: cartesian(-50:50,-50:50)
type cart
! these indices are seen even in called procedures
integer :: data(-50:50,-50:50)
end type
type(cart) :: cartesian_t
write(*,*)lbound(cartesian),ubound(cartesian)
write(*,*)lbound(cartesian_t%data),ubound(cartesian_t%data)
call lookatbounds(cartesian,cartesian_t)
contains
subroutine lookatbounds(array,array_t)
integer :: array(:,:)
type(cart) :: array_t
write(*,*)'in called procedure'
write(*,*)lbound(array),ubound(array)
write(*,*)lbound(array_t%data),ubound(array_t%data)
end subroutine lookatbounds
end program main
-50 -50 50 50
-50 -50 50 50
In called procedure
1 1 101 101
-50 -50 50 50
Lynn McGuire <lynnmc...@gmail.com> writes:
While the second pointer is a bad pointer, it is not a illegal
pointer. Variables can point to anywhere, they are not illegal until referenced. Otherwise, code would be crashing all over the place. If
I malloc'd some space, then free'd the space, and did not NULL the
pointer, that dangling pointer would crash if ever referenced
(hopefully) but not if it just hangs around.
Computing a pointer before the beginning of an array causes undefined behavior in C and C++, even if you never dereference it.
gah4 <ga...@u.washington.edu> schrieb:(snip)
On Saturday, August 27, 2022 at 2:30:15 AM UTC-7, Thomas Koenig wrote:
Really? You mean that
DO 10 I=0,1
would not be allowed?
Yes, not allowed.
I have the Fortran 66 standard before me, and I find no such
restriction in 7.1.2.8 (nor would it make sense at all).
Which part of the standard are you referring to?
Oversimplifying a bit, but if you are using non-default starting indices for the first
time it is important to know there is a difference in how the ranges will appear in
a called procedure when the passed array is an intrinsic type and when it is a component
of a user-defined type ...
Note that when you specify local bounds for intrinsic types it applies
just to the current scope, which is often just the procedure you declared
it in. This makes sense or everything you passed the array to would then
be required to handle the non-standard dimensioning for a relatively
rare case.
So if you declared something to have indices from -50 to 50 and then
passed it to another procedure, the other procedure sees it as going
from 1 to 101. That is, the called procedure sees it as if declared with
a default starting index of 1.
With user-defined types that is not the case; so generally if you want something to retain its non-standard starting index when being passed
it is easiest to declare a special type.
It makes sense, but might not be intuitively what you would guess
at first.
program main
implicit none
! these indices are seen in local scope
integer :: cartesian(-50:50,-50:50)
type cart
! these indices are seen even in called procedures
integer :: data(-50:50,-50:50)
end type
type(cart) :: cartesian_t
write(*,*)lbound(cartesian),ubound(cartesian) write(*,*)lbound(cartesian_t%data),ubound(cartesian_t%data)
call lookatbounds(cartesian,cartesian_t)
contains
subroutine lookatbounds(array,array_t)
integer :: array(:,:)
type(cart) :: array_t
write(*,*)'in called procedure'
write(*,*)lbound(array),ubound(array) write(*,*)lbound(array_t%data),ubound(array_t%data)
end subroutine lookatbounds
end program main
-50 -50 50 50
-50 -50 50 50
In called procedure
1 1 101 101
-50 -50 50 50
Am 26.08.2022 um 22:49 schrieb Lynn McGuire:
“Why do arrays start at 0?"
On the CPU-level you heave the least number of calculations to
determine an address of an indexed entity if the index starts
at zero.
Am 26.08.2022 um 22:49 schrieb Lynn McGuire:.
“Why do arrays start at 0?"
https://buttondown.email/hillelwayne/archive/why-do-arrays-start-at-0/
"It's not the reason you think. No, it's not that reason either.”
My Fortran starts at one. My C++ starts at zero. This has made my life hell.
On the CPU-level you heave the least number of calculations to.
determine an address of an indexed entity if the index starts
at zero.
On Saturday, August 27, 2022 at 3:13:09 PM UTC-7, Thomas Koenig wrote:
gah4 <ga...@u.washington.edu> schrieb:(snip)
On Saturday, August 27, 2022 at 2:30:15 AM UTC-7, Thomas Koenig wrote:
Really? You mean that
DO 10 I=0,1
would not be allowed?
Yes, not allowed.
I have the Fortran 66 standard before me, and I find no such
restriction in 7.1.2.8 (nor would it make sense at all).
Which part of the standard are you referring to?
Yes 7.1.2.8.
"At time of execution of the DO statement, m1, m2, and m3 must be greater than zero."
On 8/28/2022 12:26 AM, gah4 wrote:
"At time of execution of the DO statement, m1, m2, and m3 must be greater than zero."
What ? Our code has negative indexes dating back to F66 days. I am
assuming that this is:
DO 10 I = ncp, 1, -1
Hm. Seems like this was carried over from the very first FORTRAN.
The 1956 Programmer's Reference Manual restricted them to unsigned
fixed point constants.
Looking at the Fortran IV language at http://www.bitsavers.org/pdf/ibm/360/fortran/C28-6515-6_FORTRAN_IV_Language_1966.pdf
we have the same restriction.
So, seems like an extension at the time (a very useful one, too).
On Sunday, August 28, 2022 at 11:42:28 AM UTC-7, Lynn McGuire wrote:.
On 8/28/2022 12:26 AM, gah4 wrote:(snip, I wrote)
"At time of execution of the DO statement, m1, m2, and m3 must be greater than zero."
What ? Our code has negative indexes dating back to F66 days. I am
assuming that this is:
DO 10 I = ncp, 1, -1Many systems had extensions to Fortran 66, and that might not have been unusual.
Those competing with IBM needed a way to make there systems look better,
and such extensions were one way to do it. DEC had a lot of extensions like that.
On the other hand, IBM was careful with theirs. The had some big extensions where there was no way around them. END= on READ was very useful!
But DO look changes that were easy to make with a temporary variable
did not get an extension. Making m3 negative means that the test
has to be inverted. IBM Fortran IV compilers don't change the test.
As note, though, IBM Fortran IV compilers will also disallow:
DO 10 I=0,9
though if you use a variable set to 0, it works just fine.
(Unless the optimizer figures it out, and disallows it.)
Similarly,
DO 10 I = ncp, 1, -1
won't compile. If you use a variable set to -1, it will
loop until the variable wraps to a large value.
On 8/28/2022 12:26 AM, gah4 wrote:.
On Saturday, August 27, 2022 at 3:13:09 PM UTC-7, Thomas Koenig wrote:
gah4 <ga...@u.washington.edu> schrieb:(snip)
On Saturday, August 27, 2022 at 2:30:15 AM UTC-7, Thomas Koenig wrote:
Really? You mean that
DO 10 I=0,1
would not be allowed?
Yes, not allowed.
I have the Fortran 66 standard before me, and I find no such
restriction in 7.1.2.8 (nor would it make sense at all).
Which part of the standard are you referring to?
Yes 7.1.2.8.
"At time of execution of the DO statement, m1, m2, and m3 must be greater than zero."What ? Our code has negative indexes dating back to F66 days. I am
assuming that this is:
DO 10 I = ncp, 1, -1
gah4 <gah4@u.washington.edu> schrieb:
On Saturday, August 27, 2022 at 2:30:15 AM UTC-7, Thomas Koenig wrote:
(snip)
Now for this case, you want a DO loop to start at zero, which Fortran 66 >>>> didn't allow.
Really? You mean that
DO 10 I=0,1
would not be allowed?
Yes, not allowed.
I have the Fortran 66 standard before me, and I find no such
restriction in 7.1.2.8 (nor would it make sense at all).
Which part of the standard are you referring to?
The authors of "Numerical Recipes" started out with F77 and so all the
code in their first book has arrays starting at 1. When they brought
out the C version, they resorted to just this trick!
On 8/27/22 5:13 PM, Thomas Koenig wrote:.
gah4 <ga...@u.washington.edu> schrieb:
On Saturday, August 27, 2022 at 2:30:15 AM UTC-7, Thomas Koenig wrote:
(snip)
Now for this case, you want a DO loop to start at zero, which Fortran 66 >>>> didn't allow.
Really? You mean that
DO 10 I=0,1
would not be allowed?
Yes, not allowed.
I have the Fortran 66 standard before me, and I find no such
restriction in 7.1.2.8 (nor would it make sense at all).
Which part of the standard are you referring to?It is right there in that section in the paragraph that starts with (3).
The three integers m1, m2, and m3 must be greater than zero.
The only justification I can see for this is that they wanted to be able
to implement the loop logic with unsigned integers.
Even then I don't know why zero was not allowed.
It is right there in that section in the paragraph that starts with (3).
The three integers m1, m2, and m3 must be greater than zero.
The only justification I can see for this is that they wanted to be able
to implement the loop logic with unsigned integers. Even then I don't
know why zero was not allowed.
On Monday, August 29, 2022 at 5:07:13 PM UTC+10, Ron Shepard wrote:[...]
The only justification I can see for this is that they wanted to be able.
to implement the loop logic with unsigned integers.
Definitely not.
The control variable at least would be an ordinary signed integer,
because that variable could be used in integer and/or real
expressions.
And if m2 and m3 are variables,, then for the same reason,
these would be ordinary signed integers.
.
Even then I don't know why zero was not allowed.
On 8/29/22 3:30 AM, Robin Vowels wrote:.
On Monday, August 29, 2022 at 5:07:13 PM UTC+10, Ron Shepard wrote:[...]
Yes, the index variable and all three variables m1, m2, and m3 areThe only justification I can see for this is that they wanted to be able >> to implement the loop logic with unsigned integers..
Definitely not.
The control variable at least would be an ordinary signed integer,
because that variable could be used in integer and/or real
expressions.
fortran integers. There was no unsigned integer type in fortran then. or
now for that matter, so these variables were all regular fortran signed integers.
And if m2 and m3 are variables,, then for the same reason,
these would be ordinary signed integers.
.
Even then I don't know why zero was not allowed..
Yes, see above. Fortran did not have unsigned integers, then or now. But
as I said before, the only reason I can imagine for having that
limitation in the language is that they wanted to allow the processor to implement the loop control logic using unsigned integers in whatever instruction set the compiled code was running.
This is not the case. I have a version of a manual for VS Fortran,
from February 1981, which describes a Fortran 77 compiler.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 159 |
Nodes: | 16 (0 / 16) |
Uptime: | 98:36:36 |
Calls: | 3,209 |
Files: | 10,563 |
Messages: | 3,009,579 |