[petsc-dev] fortran literals

Munson, Todd tmunson at mcs.anl.gov
Thu Sep 1 15:56:33 CDT 2016


>> I'd like to be able to do something like the following in Fortran:
>> 
>>      PetscReal c
>> 
>>      c = PetscRealCast(5.78D0)
>> 
>> where PetscRealCast is real() for single precision and dble()
>> for double precision.  Without the cast, you would get all 
>> kinds of warnings of the form "change of value in 
>> conversion" for a single precision installation.
>> 
>> This macro should work for real numbers (not sure about complex as
>> real() is overloaded as the real part of a complex number), avoids 
>> generating warnings for single and double installs, and produces
>> the same output for the C and Fortran code.
> 
>  Does your proposed PetscRealCast() work? Note that for double you shouldn't even need the dble()
> 
>  If it does then I can live with it. 

Yes, this does work at least for assignment statements on my mac with gfortran
and for real scalars.  It likely does not work for data statements or other 
types of initialization.

For complex, it also works if you use

  c = complex(PetscRealCast(0.12D0), PetscRealCast(0.12D0))

but not if you use the cmplx() function for the assignment.  Looks like
cmplx() always truncates the arguments to single precision (at least on
my mac with gfortran).

I would keep the dble() in the macro and let the compiler sort it out 
though.  Keeping it means that you get the correct behavior in single, 
double, and quad precision when your complier lets you use 
PetscRealCast(0.12Q0).

Todd.




More information about the petsc-dev mailing list