|
|||
|
On Feb 26, 12:07*pm, nos...@see.signature (Richard Maine) wrote:
> Athanasios Migdalas <sami...@gmail.com> wrote: > > Thank you Dick & Richard, and yes I have encountered just today that > > gfortran will complain for array constructors not being of the correct type. > > Actually I was surprised because I was expecting it to auto cast the > > integers to reals. Actually one other compiler I tested silently does that. > > But now I know who follows the standard. > > It should cast them to reals if you tell it that's what you want, as > DIck showed earlier. Use the form (new to f2003) > > * [real:: v, 1, 2, 3, 4, 5] > > The problem without the "real::" part is that it doesn't know what to > cast to what. For example, why wouldn't it cast the reals to integers > instead of the integers to reals? Why on earth would it do that. Converting REALs to INTEGERs would be plain silly, even perverse, and would usually result in loss of information through truncation of any fractional parts. > Just giving "preference" to the first > element (the kind of rule that people tried to write) doesn't always > work out. A constructor containing numeric values could default to integer when all the constants etc within it are integer; otherwise it could default to REAL (of the longest kind). Conversions of this kind were resolved some 45 years ago when PL/I was designed. |
|
|
||||
|
||||
|
|
|
|||
|
In comp.lang.fortran Robin Vowels <robin.vowels@gmail.com> wrote:
(snip) >> It should cast them to reals if you tell it that's what you want, as >> DIck showed earlier. Use the form (new to f2003) >> [real:: v, 1, 2, 3, 4, 5] >> The problem without the "real::" part is that it doesn't know what to >> cast to what. For example, why wouldn't it cast the reals to integers >> instead of the integers to reals? > Why on earth would it do that. Converting REALs to INTEGERs > would be plain silly, even perverse, and would usually result in loss > of information through truncation of any fractional parts. Last I knew, PL/I and C allowed for array initializers, but nothing like Fortran array constructors. But PL/I allows for structure expressions, so there would be use for such in PL/I. > A constructor containing numeric values could default to integer > when all the constants etc within it are integer; > otherwise it could default to REAL (of the longest kind). > Conversions of this kind were resolved some 45 years ago > when PL/I was designed. DCL 1 STRUCT, 2 I FIXED BIN(31,0), 2 J FIXED DEC(10,0), 2 F FLOAT BIN(24), 2 G FLOAT DEC(15); Now, what value do you assign to STRUCT in an assignment statement? If PL/I had constructors, one could: STRUCT = [ 1, 2.0, 3.1, 4 ]; You can, though, assign a constant: STRUCT = 3; or use it in an expression: STRUCT = SQRT(STRUCT); -- glen |
|
|||
|
On Feb 28, 8:33*pm, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
> In comp.lang.fortran Robin Vowels <robin.vow...@gmail.com> wrote: > > (snip) > > >> It should cast them to reals if you tell it that's what you want, as > >> DIck showed earlier. Use the form (new to f2003) > >> [real:: v, 1, 2, 3, 4, 5] > >> The problem without the "real::" part is that it doesn't know what to > >> cast to what. For example, why wouldn't it cast the reals to integers > >> instead of the integers to reals? > > Why on earth would it do that. *Converting REALs to INTEGERs > > would be plain silly, even perverse, and would usually result in loss > > of information through truncation of any fractional parts. > > Last I knew, PL/I and C allowed for array initializers, but > nothing like Fortran array constructors. > > But PL/I allows for structure expressions, so there would > be use for such in PL/I. > > > A constructor containing numeric values could default to integer > > when all the constants etc within it are integer; > > otherwise it could default to REAL (of the longest kind). > > Conversions of this kind were resolved some 45 years ago > > when PL/I was designed. > > DCL 1 STRUCT, > * * * *2 I FIXED BIN(31,0), > * * * *2 J FIXED DEC(10,0), > * * * *2 F FLOAT BIN(24), > * * * *2 G FLOAT DEC(15); > > Now, what value do you assign to STRUCT in an assignment statement? > > If PL/I had constructors, In this ng, I posted an example of constructors using PL/I's preprocessor. Constructors are not a language feature of PL/I. The closest thing to a constructor in P/I is: declare c(*) character (32000) controlled; allocate c(5) initial (1, 5.5e0, 3.4, 7, 99.999); which, effectively, is all an assignment of a constructor really is. For a character example similar to the earlier Fortran one: allocate c(5) initial ((a), (b), 'abc', 'defg', (some_char_function()) ); > one could: > > STRUCT = [ 1, 2.0, 3.1, 4 ]; > > You can, though, assign a constant: > > * *STRUCT = 3; > > or use it in an expression: > > * *STRUCT = SQRT(STRUCT); Or STRUCT could be assigned from another structure having entirely different types from the elements of STRUCT. |
|
|||
|
In comp.lang.fortran Robin Vowels <robin.vowels@gmail.com> wrote:
(snip on array constructor type) >> Last I knew, PL/I and C allowed for array initializers, but >> nothing like Fortran array constructors. (snip) > In this ng, I posted an example of constructors using PL/I's > preprocessor. Constructors are not a language feature of PL/I. > The closest thing to a constructor in P/I is: > declare c(*) character (32000) controlled; > allocate c(5) initial (1, 5.5e0, 3.4, 7, 99.999); Maybe, but it isn't very close. Well, I consider array constructors like unnamed array constants. You can use PARAMETER for a named array constant, which has a declared type. > which, effectively, is all an assignment of a constructor really is. In C, one uses an initialized static array, which is pretty similar to an initialized Fortran array, either in a DATA statement, or initialized on its declaration. Java has static final, which means that you can't modify it, so it really is a constant. C string constants are pretty much constant arrays of char. In K&R C, string constants were modifyable, but in ANSI C you aren't supposed to do that. Some compilers allow it as an extension. I tried: use iso_c_binding print *,c_loc( [ 1, 2, 3 ] ) end in gfortran 4.4.5 to see if array constructors had an address, but it got an internal compiler error. -- glen |
|
|||
|
On Feb 29, 4:58*am, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
> In comp.lang.fortran Robin Vowels <robin.vow...@gmail.com> wrote: > > (snip on array constructor type) > > >> Last I knew, PL/I and C allowed for array initializers, but > >> nothing like Fortran array constructors. > > (snip) > > > In this ng, I posted an example of constructors using PL/I's > > preprocessor. *Constructors are not a language feature of PL/I. > > The closest thing to a constructor in PL/I is: > > declare c(*) character (32000) controlled; > > allocate c(5) initial (1, 5.5e0, 3.4, 7, 99.999); > > Maybe, but it isn't very close. But c = [1, 5.5e0, 3.4, 7, 99.999]; is. And, of course, it's not far from allocate c(5) initial (1, 5.5e0, 3.4, 7, 99.999); > Well, I consider array constructors like unnamed array constants. Only if they contain constants. > You can use PARAMETER for a named array constant, which has > a declared type. But only for constants. > > which, effectively, is all an assignment of a constructor really is. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|