Recurrent harmonic function returns NAN

I wrote the following sample code to find the harmonic value of N (1 1/2 1/3… 1 / N). Read the comments in the code written with BOLD, and Help me find why this happens.

#include 

float harmonic(float n, float har ) {

if(n==0) {
return 0;
}

if(n==1) {
printf("%f ", har+1.0f);***/* This prints value 1.5000*/***
return har+1.0f;
}else{
harmonic(n-1, (har+(1/n)));
}
}

int main()
{
printf(" %f ", harmonic(2, 0.0f)); **/* But this prints value nan(Not a Number)*/**
return 0;
}

< p>Thank you,
Naga

I think you want to do:

return harmonic(n-1, (har+(1/n)));

I wrote the following example Code to find the harmonic value of N (1 1/2 1/3… 1 / N). Read the comments in the code written with BOLD and help me find why this happens.

#include 

float harmonic(float n, float har) {

if(n==0) {
return 0;
}

if(n==1) {
printf("%f ", har+1.0f);***/* This prints value 1.5000*/***
return har+1.0f;
}else{
harmonic(n-1, (har+(1/n)));
}
}

int main()
{
printf("%f ", harmonic(2, 0.0f)); **/* But this prints value nan(Not a Number) */**
return 0;
}

Thank you,
Naga

I want You want to do:

return harmonic(n-1, (har+(1/n)));

< /p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5308 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.