Use a debugger
Code:
$ g++ -Wall -g bar.cpp
$ gdb -q ./a.out
Reading symbols from /home/sc/Documents/a.out...done.
(gdb) b fun_main
Breakpoint 1 at 0x400a89: file bar.cpp, line 53.
(gdb) run
Starting program: /home/sc/Documents/a.out
Breakpoint 1, fun_main (z=-10, c1=0.355028063, c2=0.258819401) at bar.cpp:53
53 ret1=fun_1(z);
(gdb) n
54 ret2=fun_2(z);
(gdb) p ret1
$1 = -nan(0x400000)
(gdb) n
55 ret= c1*(1+ret1)-c2*(z+ret2);
(gdb) p ret2
$2 = -nan(0x400000)
OK, so now we know where all the nan's are coming from.
So delete the first breakpoint and set one at the start of fun_1 instead, and let's analyse it's behaviour.
Code:
(gdb) delete 1
(gdb) b fun_1
Breakpoint 2 at 0x400881: file bar.cpp, line 9.
(gdb) c
Continuing.
-10 -nan
Breakpoint 2, fun_1 (z=-9.98999977) at bar.cpp:9
9 float ret1=0;
(gdb) list
4 using namespace std;
5
6 float fun_1(float z)
7 {
8 int k;
9 float ret1=0;
10 for(k=1; k<=1000; k++)
11 {
12 int c, d;
13 int pk=1;
(gdb)
14 int factk=1;
15 for(c=1; c<=k; c++)
16 {
17 pk= pk*(3*c-2);
18 }
19 for(d=1; d<=3*k; d++)
20 {
21 factk=factk*d;
22 }
23 ret1= ret1+((pk*pow(z,3*k))/(factk));
(gdb) b 23
Breakpoint 3 at 0x4008fc: file bar.cpp, line 23.
(gdb) c
Continuing.
Breakpoint 3, fun_1 (z=-9.98999977) at bar.cpp:23
23 ret1= ret1+((pk*pow(z,3*k))/(factk));
(gdb) printf "%d %d %d\n", k, pk, factk
1 1 6
(gdb) c
Continuing.
Breakpoint 3, fun_1 (z=-9.98999977) at bar.cpp:23
23 ret1= ret1+((pk*pow(z,3*k))/(factk));
(gdb) printf "%d %d %d\n", k, pk, factk
2 4 720
(gdb) c
Continuing.
Breakpoint 3, fun_1 (z=-9.98999977) at bar.cpp:23
23 ret1= ret1+((pk*pow(z,3*k))/(factk));
(gdb) printf "%d %d %d\n", k, pk, factk
3 28 362880
(gdb) c
Continuing.
Breakpoint 3, fun_1 (z=-9.98999977) at bar.cpp:23
23 ret1= ret1+((pk*pow(z,3*k))/(factk));
(gdb) printf "%d %d %d\n", k, pk, factk
4 280 479001600
Do you see just how quickly factk is growing.
It's only iteration 4 out of 1000 and it's already approaching the numeric limit for an integer.
You have a serious numeric overflow issue (or possibly several).