Floating Point Number + NAN - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Floating Point Number + NAN

sonu_1984sonu_1984 Posts: 6Member




float square(float); void main() { clrscr(); float a,b; printf("ENter a Number"); scanf("%f",&a); printf("Number=%f",a); b=square(a); printf("\nSquare of %f is %f",a,b); getch(); }

float square(float x) { float y; y=x*x; return(y); }


In the above program, I am calculating the square of float number. But sometimes the number is entered as NAN and sometimes Output is NAN. What is NAN? I am entering floating point number, then y NAN is entered? SEE the Image attached for the OUTPUT.

228 x 159 - 2K


  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭

    When making any post, please make use of the preview button to make sure the code is formatted properly. Edit your post until the preview looks as clean as possible before finally clicking on the "Post Comment" button. What you've got now is not very readable and likely to make many ignore your post because it isn't worth their time to try and understand.

    When coding, try your best to make code that complies with the standard. This makes it easy for more people to copy/paste your code and help you out with running/debugging.

    As an example of non-standard coding, the <conio.h> header and the clrscr/getch functions are non-standard.

    Also, the main function returns an int according to the standard.

    NAN means "Not A Number", it is an indication that some value stored into the variable cannot be represented as a number. I compiled and ran your code (minus the non-standard <conio.h> and clrscr/getch bits) without problem getting a correct result of 9 for an input value of 3 without any NAN value being shown.

    It might also help to know what your dev environment is OS and IDE.

Sign In or Register to comment.