0

I'm writing a code in Python that calls some subroutines written in Fortran. When the variables are defined in Fortran as:

real*8, intent(in)   :: var1,var2

and, respectively in Python,

var1 = 1.0
var1 = 1.0

everything is fine. But if I define an extended real, that is:

real*16, intent(in)   :: var1,var2

and in python use

import numpy as np

var1 = np.float16(2)

var2 = np.float16(2)

the variables take a strange number when passing them to the fortran routine. Can anyone see what I'm doing wrong?

wuampa
  • 273
  • 4
  • 15
  • 1
    real* is an obsolete nonstandard fortran convention , read this .. http://stackoverflow.com/questions/838310/fortran-90-kind-parameter – agentp Oct 02 '13 at 16:45
  • Thanks for your suggestion. The Fortran code is an old code written in Fortran 77, which contains such declarations. I modified them to the kind convention. – wuampa Oct 03 '13 at 08:33
  • you may be past this based on your other comment but numpy float16 is 16 bit (half precision)..you would want float128 if your platform supports it http://stackoverflow.com/questions/9062562/what-is-the-internal-precision-of-numpy-float128 – agentp Oct 03 '13 at 14:03
  • Yes, I had already noticed it, but thanks. – wuampa Oct 03 '13 at 15:07

1 Answers1

0

This numpy-discussion thread from last year indicates that numpy's quadruple precision varies form machine to machine. My guess is that your bunk data comes from two different language's inconsistency as to what quad-precision means.

Note also that f2py really only understands <type>(kind=<precision>) where <type> is REAL/INTEGER/COMPLEX and <precision> is an integer 1, 2, 4, 8 (cf the FAQ).

Kyle Kanos
  • 3,257
  • 2
  • 23
  • 37
  • You were right. In order to adapt the quad-precision definition in both languages I've tried to use the BigFloat package (http://pythonhosted.org/bigfloat/) which allows the definition of a quad-precision float in Python according to IEEE standard 754, the same definition used in Fortran, I think. However I keep having the same problem, Fortran variables take a different number than the one defined in Python. Still looking for... – wuampa Oct 03 '13 at 10:00
  • For the time being I've solved the problem using aux variables. In this case, aux is defined as an 8 bit real (real*8, intent(in) :: aux1,aux2), and later in the code: var1=aux1 and var2=aux2. However I think this is not much rigorous. – wuampa Oct 03 '13 at 15:10