I wrote the following code example to find the harmonic value of N. (1 + 1/2 + 1/3 + ... 1 / N). Read the comments in the code written in BOLD and help me find out why this is happening.
#include <stdio.h> float harmonic(float n, float har) { if(n==0) { return 0; } if(n==1) { printf("%f\n", har+1.0f);***/* This prints value 1.5000*/*** return har+1.0f; }else{ harmonic(n-1, (har+(1/n))); } } int main() { printf("%f\n", harmonic(2, 0.0f)); **/* But this prints value nan(Not a Number)*/** return 0; }
Thanks Naga
I think you want to do:
return harmonic(n-1, (har+(1/n)));
, float , "if (n == 0)" "if (n <= EPSILON)" "if (n == 1)" "if (n <= 1,0f + EPSILON)", EPSILON , , 1,0e-5. , .
, n int. . "n" , .
double float.
, NaN. "else", , , . , NaN.
Source: https://habr.com/ru/post/1734892/More articles:ASP.NET/JavaScript - Ajax call, how? - javascriptJFace Label Provider Overview - eclipseHow to export code formatting settings in Delphi 2010? - delphiАутентификация форм с помощью sqlite - asp.net-mvcEXC_BAD_ACCESS when calling pushViewController - iphonejquery: print hidden element - jqueryCall the TDataModule method in TThread.Execute - multithreadingConvert Arraylist to String in vb.net - .netКак присоединиться к элементам ArrayList, преобразующим его в строковое представление? - stringWPF DataTemplate does not throw an exception - exceptionAll Articles