What is floating point error in C ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • talibbhai

    What is floating point error in C ?

    hello friends;

    I have an error in my program what is the reason behind it.
    Code:
    /*this is mathematical function of EXPONENT*/
    
    
    #include<conio.h>
    #include<stdio.h>
    #include<math.h>
    
    double exponent(float e)
    {
     float x,i,f=20; 
     float g;            
    
      for (i=1;i<=f;i++)
    	 {
    		 e=pow(x,i);
    		 g=e/(fct(i));
    	 }
      return(e);
    }
    
    void main()
    {
    clrscr();
    {
    double exponent(float e);
    	float x,y;
    	printf("\n insert number for exponent\t\t");
    	scanf("%d",&x);
    	y=exponent(x);
    	printf("\n\n the exponent value is%d",y);
    }
    getch();
    }
    please help me and find out why the program is not running succesfully.
    Last edited by MMcCarthy; Oct 20 '10, 06:03 AM. Reason: added code tags
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    You have taken float variables and in print and scan functions you are using %d. I think %f will help get rid of this error.

    Comment

    • Gauarv Kumar
      New Member
      • Oct 2010
      • 10

      #3
      Probable reasons for a floating point exception include:
      Overflow on signed types
      Division by zero.

      There is no function called fct() in math.h
      Also you are using pow() function on two uninitialized float variables.
      I think pow() function is giving the exception.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        It would help immensely if you told us what error you're getting. Compiler error? Linker error? Run-time error? Incorrect result? Don't make us guess.

        What is the purpose of the loop in lines 13-17? As far as I can see, each pass through the loop discards all of the work of the previous pass. You could simply set i to the value it gets for the last pass through the loop and execute the loop body once.

        Is there any reason why you're mixing double and float variables? Why not use double throughout?

        It is considered bad form to use non-integral types to control execution in a for loop. It can be done, but it is much harder to achieve your desired result than with integral types.

        What is fct?

        Comment

        Working...