On 6/12/2015 6:27 AM, glen herrmannsfeldt wrote:
> John Harper <
john....@vuw.ac.nz> wrote:
>
> (snip, someone wrote)
>>>> So in f95 you don't get it by just asking for it. Perhaps that
>>>> was the background of the question? (After all, just asking
>>>> does work in a lot of other languages, like Mathematica, matlab,
>>>> etc.)
>
>>> Many interpreted languages use dynamic typing, where the type of
>>> a variable changes depending on the value assigned to it.
>
>>> In that case, sqrt() can return a different type depending on
>>> it arugument. That is rare in compiled languages.
>
>> But Fortran can do that because sqrt is generic. The following program:
>
>> program testsqrt
>> implicit none
>> real:: x = 1
>> complex:: z = -1
>> print *,sqrt(x),sqrt(z)
>> end program testsqrt
>
> Yes, but Fortran can't do generics that depend on the value of
> the argument, like sqrt(1.) is real, but sqrt(-1.) should be complex.
And I should add that on second thought Algol68 most likely
could not do this either. (Context analysis, although it was
elaborate, would not go as far as dealing with values occurring
for the argument.)
So probably in my old experiment I had to guess the name of
the type first (not too difficult if INT and REAL are known
to exist) and then cast -1 to that type and take the root:
print(( sqrt(COMPL -1) ))
An when that worked I could define a constant
COMPL Ima = sqrt(COMPL -1);
which was enough to work with complex numbers by writing
them as a+b*Ima and using the existing operators (since in
A68 they all were generics from the start).
At that point I did not know that instead of 3+4*Ima, one
can write 3 I 4 (or something similar, it was) to denote a
number. Somehow I found that out later but it is not essential.
So if you can guess the name of the type, and the language
has all operators defined as generics already, that's all
you need. This works in f90 as well!
complex :: test = -1 ! <-- this you have to guess
print *, sqrt(test)
end
Of course this my sound strange, in an era where you just
can Google "Fortran complex numbers" or "Algol68 complex
numbers" to see how they are used if you don't know it,
but in 1977 the procedure was different.
--
Jos