How to combine a minus-sign with a self-defined operator?

Hi!

I have overloaded the “*”-operator such that now I can use it with
a REAL and a self-defined type. When I have a line z = x * y, all goes well.
When I have z = -1. * y, I get “unknown result for arithmetic operation”.
This can be repaired by coding z = (-1.) * y
I find this solution a nuisance and would like to have my operator
recognize -1. as a valid first arguement.

How can I do this?

Thanks,


Arjan
E-mail: arjan.van.dijk@rivm.nl

Hi Arjan,

Do you have an interface for real and user defined type and vice versa? If not, you will need ones.

After you have created an interface for realuser_defined_type and user_defined_typereal, a workaround for now is below.

Note that currently we have a bug at compile time when passing negative real constant to overloaded operator *. As * has higher precedence than - and the compiler does not handle it correctly. I have filed a TPR #14395.

  1. passing a variable instead of constant
    For example, real abc = -1.0
    …= abc * your_user_defined_type_variable
  2. put constant as a second operand.
    …=your_user_defined_type_variable * -1.0


    Hope it helps. Thank you.
    Hongyon

Hi Arjan,

I would like to correct the above comment I just made. Actually, -1.0 * user_defined_type won’t work the way you want it to be because * has higher precedence and hence it the interpretation in Fortran is -(1.0 * user_defined_type). That means you will need to put () or store -1.0 in a variable.

Now regarding a TPR, we still need to fix the problem and gives an appropriate warning message when user try to do that.

Hongyon

Hi,

This will be fixed in 7.2.

Hongyon