Akhil Akkapelli
Akhil Akkapelli

Reputation: 176

Value of Derived Type changes unexpectedly in C-Fortran Interoperability Code

In the following Fortran code which uses C library the value of tol changes unexpectedly. What might be the reason behind this behaviour?

PROGRAM ComplexIntegral
USE farblib
IMPLICIT NONE

TYPE(fmag_t) :: tol
TYPE(facb_t) :: s, t, a, b
INTEGER(c_long) :: prec, goal
TYPE(facb_calc_integrate_opt_t) :: options
TYPE(c_ptr) :: params


CALL facb_calc_integrate_opt_init(options)

prec = 64_c_long
goal = prec
params = c_null_ptr

CALL facb_init(a)
CALL facb_init(b)
CALL facb_init(s)
CALL facb_init(t)
CALL fmag_init(tol)

CALL fmag_set_ui_2exp_si(tol, 1_c_long, -prec)

PRINT*, "Initial Value of tol:"
CALL facb_print(tol)

CALL facb_set_d(a, 0._c_double)

PRINT*, "Intermediate Value of tol:"
CALL facb_print(tol)

CALL facb_set_d(b, 1._c_double) 

PRINT*, "Final Value of tol:"
CALL facb_print(tol) 
                
CALL facb_calc_integrate(s, fatanderiv, params, a, b, goal, tol, options, prec)
CALL facb_mul_2exp_si(s, s, 2_c_long)

 CALL facb_printf(s)

CALL facb_clear(a)
CALL facb_clear(b)
CALL facb_clear(s)
CALL facb_clear(t)
CALL fmag_clear(tol)

END PROGRAM ComplexIntegral

The Output of the program is as follows:

Initial Value of tol:
(536870912 * 2^-93)
Intermediate Value of tol:
(914731972 * 2^-116)
Final Value of tol:
(914731972 * 2^-116)

Compilation:

ifort farblib.F90 farb.F90 -lflint -larb -lm -g
./a.out

Upvotes: 0

Views: 34

Answers (0)

Related Questions