# Complex number expression fails

I am wondering why this fails:

``````complex :: a
a = (cos(1.0) + 1.1, sin(1.0))
end
``````
``````main.f95:2:20:
2 | a = (cos(1.0) + 1.1, sin(1.0))
|                    1
Error: Expected a right parenthesis in expression at (1)
``````

I have noticed people use parameter `complex, parameter :: im = (0., 1.)` and `cos(1.0) + im * sin(1.0)`, but I would like to know the reason.

1 Like

The standard says that the simple parenthesis formulation `(x,y)` can be used only if `x` and `y` are literal constants or `parameter`â€™s. Otherwise the `cmplx()` intrinsic function must be used:

``````a = cmplx( x, y )
``````

Alternative way:

``````a%re = x
a%im = y
``````

`a = x + im * y`
is closer to the usual mathematical notation `a = x + i.y`

Note however that it requires a complex-complex multiplication (i.e. 4 multiplications+2 additions), a cast realâ€“>complex, and a complex addition (i.e. 2 additions), whereas `cmplx(x,y)` is a simple assignment. For an isolated instruction it doesnâ€™t matter, but if enclosed in a time-consuming loop it can make a difference.

Finally, in your particular case there is also:
`a = 1.1 + exp( (0.0,1.0) )`
more generally `exp( cmplx(0,arg) )` is equivalent to `cmplx( cos(arg), sin(arg) )`

3 Likes

I guess there is no such thing as complex constructor in a sense of derived type constructors, although an invocation of intrinsic function `cmplx(x,y)` looks very similar.

According to F2018 Standard Interpretation document, section 7.4.3.3 Complex type, the
(real-part,imag-part) construct is a complex-literal-constant (R718). So nothing unusual that both parts must be literal constants or (only since F2003) named constants a.k.a. parameters.

1 Like

Your main question has been answered. As a side point, I see that you named your source file with a `.f95` extension. Here is an issue I have raised in many GitHub projects. Often the author has agreed to use the `.f90` suffix in response.

Please consider changing the source file suffixes from .f95 to .f90.
The .f90 suffix means that the source code is free format, not that
the code conforms to the Fortran 90 standard. Code that uses the .f90
suffix can use features from any Fortran standard. All Fortran
compilers recognize .f90 as a suffix indicating free source form, but
some may not recognize a suffix such as .f95, .f03, .f08, or .f18.
Some users may have build tools that do not recognize suffixes other
than .f90. Most Fortran source code on GitHub that uses features from
a standard more recent than Fortran 90 still uses the .f90 suffix.

1 Like

In fact, I do always use `.f90` extension. This `main.f95` is autogenerated by the Online Fortran Compiler - online editor online compiler.

Nevertheless, thank you for pointing out the issue. And sorry for the confusion.

There are some semantic differences in the two perspectives. The `cmplx()` intrinsic is actually a generic type and kind conversion function that takes one, two, or three arguments which can be integer, real, or complex (with some restrictions for the mixed-type cases). There is an extra KIND= argument that can be used for nondefault KIND values, and it behaves a little quirky because when it was introduced into the language there was only one complex type.

1 Like