Calling function with configurable real precision

tgoossens

The goal:

Have a function work with configurable working precision.

When I try this:

program vierkantsvergelijking
implicit none

integer, parameter :: dp = kind(0.d0)
integer, parameter :: sp = kind(0.0)

print *, algoritme1(-5771.,2.,dp)
contains

  function algoritme1(b,c,wp) result( solution)
    integer :: wp ! working precision
    real(kind=wp) :: b,c,D
    real(kind=wp), dimension(2) :: solution
    D = sqrt((b/2)**2 - c)
    solution(1) = -b/2 + D
    solution(2) = -b/2 - D
  end function algoritme1

end program

I get: Error: Type mismatch in argument 'b' at (1); passed REAL(4) to UNKNOWN

Why is this not working and how can I achieve my goal?

High Performance Mark

Yes, or rather no, that's not going to work, not no how. The Intel Fortran compiler complains, about this line:

real(kind=wp) :: b,c,D

that

A kind type parameter must be a compile-time constant.   [WP]

It makes the same complaint about real(kind=wp), dimension(2) :: solution too. This is a deep-rooted feature of Fortran.

To do what you want you will have to define a generic interface, along these lines

interface algoritme1
    procedure :: algoritme1_sp, algoritme1_dp
end interface

and write the code for both those procedures. The compiler can then determine which one is called by the function signature; presumably one would have sp arguments, the other dp arguments.

You might think that this all means that Fortran doesn't do generic procedures, I'll leave that question to the sophists and language nit-pickers. It would be worth your while to search around for generic programming in Fortran; even here on SO there are tricks and tips on how to better avoid writing multiple implementations of what the programmer believes (at odds with the compiler) to be the 'same' code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling C function inside copy of a real function

From Dev

Confusing double precision real in Fortran

From Dev

Mockito : Calling real implementation

From Dev

Double precision in FORMAT function

From Dev

Double precision in FORMAT function

From Dev

LOCALTIMESTAMP function with precision parameter

From Dev

Use Group By for real or float column with specify precision

From Dev

Z3 precision for real and decimal values

From Dev

Precision issue of integrate function in R

From Dev

Precision of acos function in c++

From Dev

Precision of calculations after exponent function

From Dev

Precision of acos function in c++

From Dev

Middleware function not calling the function

From Dev

Calling function on load of function

From Dev

calling a function into another function

From Dev

using real(A[,kind=]) in passing a single precision array as a double precision to a subroutine in Fortran 90/2003

From Dev

A monad with a "real" return function

From Dev

Nested function or calling function in a function

From Dev

Calling real method in Mockito, but intercepting the result

From Dev

Calling a method on Mock object is calling real method instead of mocked implementation

From Dev

PostgreSQL timestamp/numeric precision ignored on function parameter?

From Dev

logistic / sigmoid function implementation numerical precision

From Dev

Implementing an accurate cbrt() function without extra precision

From Dev

C++ sqrt function precision for full squares

From Dev

Is there any function to calculate Precision and Recall using Matlab?

From Dev

Guaranteed precision of sqrt function in C/C++

From Dev

GLSL double precision complex number power function

From Dev

GLSL double precision complex number power function

From Dev

Guaranteed precision of sqrt function in C/C++

Related Related

  1. 1

    Calling C function inside copy of a real function

  2. 2

    Confusing double precision real in Fortran

  3. 3

    Mockito : Calling real implementation

  4. 4

    Double precision in FORMAT function

  5. 5

    Double precision in FORMAT function

  6. 6

    LOCALTIMESTAMP function with precision parameter

  7. 7

    Use Group By for real or float column with specify precision

  8. 8

    Z3 precision for real and decimal values

  9. 9

    Precision issue of integrate function in R

  10. 10

    Precision of acos function in c++

  11. 11

    Precision of calculations after exponent function

  12. 12

    Precision of acos function in c++

  13. 13

    Middleware function not calling the function

  14. 14

    Calling function on load of function

  15. 15

    calling a function into another function

  16. 16

    using real(A[,kind=]) in passing a single precision array as a double precision to a subroutine in Fortran 90/2003

  17. 17

    A monad with a "real" return function

  18. 18

    Nested function or calling function in a function

  19. 19

    Calling real method in Mockito, but intercepting the result

  20. 20

    Calling a method on Mock object is calling real method instead of mocked implementation

  21. 21

    PostgreSQL timestamp/numeric precision ignored on function parameter?

  22. 22

    logistic / sigmoid function implementation numerical precision

  23. 23

    Implementing an accurate cbrt() function without extra precision

  24. 24

    C++ sqrt function precision for full squares

  25. 25

    Is there any function to calculate Precision and Recall using Matlab?

  26. 26

    Guaranteed precision of sqrt function in C/C++

  27. 27

    GLSL double precision complex number power function

  28. 28

    GLSL double precision complex number power function

  29. 29

    Guaranteed precision of sqrt function in C/C++

HotTag

Archive