Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29276 Discussions

How do I pass double precision variables into single precision subroutine?

david_sallngc_com
2,543 Views
0 Kudos
4 Replies
dannycat
New Contributor I
2,543 Views

If you are passing by value you can use the real() function in the subroutine argument. If however you wish to obtain a real value from the subroutine you can use a local real(8) dummy argument and then equate to your real(4) variable.

real(8) :: dValue

Call Sub(real(dValue))

or:

real(4) :: fValue

real(8) :: dValue

call Sub(dValue)

fValue = real(dValue)

0 Kudos
david_sallngc_com
2,543 Views
Quoting - dannycat

If you are passing by value you can use the real() function in the subroutine argument. If however you wish to obtain a real value from the subroutine you can use a local real(8) dummy argument and then equate to your real(4) variable.

real(8) :: dValue

Call Sub(real(dValue))

or:

real(4) :: fValue

real(8) :: dValue

call Sub(dValue)

fValue = real(dValue)

Hi dannycat!

I can't believe it but my entire question was missing! I need to give you a bit more information. I am trying to pass double precision variables into a single precision subroutine. After the calculation in the subroutine (in single precision), I need to pass the data back to the calling routine in double precision. for example,

implicit none

real*8 a, b(10)

complex*16 c(5,3)

...

call xxx(a, b, c)

...

end

subroutine xxx(a, b, c)

implicit none

real a, b(10)

complex c(5,3)

...

return

end

I doing a lot of coumptations in subroutine xxx and need to return with a complex matrix c that is double precision in the calling routine.

Thanks for your help!

Sincerely,

David

0 Kudos
dannycat
New Contributor I
2,543 Views

Hi dannycat!

I can't believe it but my entire question was missing! I need to give you a bit more information. I am trying to pass double precision variables into a single precision subroutine. After the calculation in the subroutine (in single precision), I need to pass the data back to the calling routine in double precision. for example,

implicit none

real*8 a, b(10)

complex*16 c(5,3)

...

call xxx(a, b, c)

...

end

subroutine xxx(a, b, c)

implicit none

real a, b(10)

complex c(5,3)

...

return

end

I doing a lot of coumptations in subroutine xxx and need to return with a complex matrix c that is double precision in the calling routine.

Thanks for your help!

Sincerely,

David

David,

It would be neater to make a copy of your subroutine and convert it to double precision. Both could be placed in a module and then you can either create a generic procedure which calls the appropriate precision or use the optional arguments to specify the passed variable kinds.

module xxxlib

contains

interface xxx
module procedure xxx4
module procedure xxx8
end inteface

subroutine xxx4(a,b,c)

...

end subroutine

subroutine xxx8(a,b,c)

...

end subroutine

end module

If you can't change the subroutine I would just copy variables into real(4) type, call subroutine and then copy back to real(8)

real(4) a4,b4(10)

complex c4

a4 = real(a)
b4 = real(b)
c4 = cmplx(c)

call xxx(a4,b4,c4)

a = dble(a4)
b = dble(b4)
c = dcmplx(c4)

....

You will lose precision unless you use a double precision subroutine.

0 Kudos
ender01
New Contributor I
2,543 Views
Quoting - dannycat

David,

It would be neater to make a copy of your subroutine and convert it to double precision. Both could be placed in a module and then you can either create a generic procedure which calls the appropriate precision or use the optional arguments to specify the passed variable kinds.

module xxxlib

contains

interface xxx
module procedure xxx4
module procedure xxx8
end inteface

subroutine xxx4(a,b,c)

...

end subroutine

subroutine xxx8(a,b,c)

...

end subroutine

end module

If you can't change the subroutine I would just copy variables into real(4) type, call subroutine and then copy back to real(8)

real(4) a4,b4(10)

complex c4

a4 = real(a)
b4 = real(b)
c4 = cmplx(c)

call xxx(a4,b4,c4)

a = dble(a4)
b = dble(b4)
c = dcmplx(c4)

....

You will lose precision unless you use a double precision subroutine.

David raises an excellent point, why do you want to convert to single precision? I would seriously consider converting the called procedure to double precision if you have access to the source. In this era of cheap memory there is rarely a good reason to use single precision and usually some pretty good reasons not to.

0 Kudos
Reply