Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by Jacques1
    And that's correct for x = 1.1. You're doing the calculation of e^x after the for loop, at which point x has just exceeded 1.0. I guess you meant the calculation to be inside of the loop.

    This is clearly a problem of your completely messed up formatting. You've stumbled upon your sloppy indentation.

    Correct formatting is crucial for correct code. It's not an optional prettifying step you may or may not do at the end. If you mess up the the layout, you'll almost always mess up the content as well, because you simply won't understand your own code.

    So I strongly suggest you grab a proper editor or IDE, choose your favorite indent style, choose an indent depth and then stick to that. If you accidentally break the formatting in your code, you fix it just like you fix a bug -- because it actually is a bug.

    I know that things like formatting or variable names don't seem to be important when you've just started to program ("The compiler doesn't care, so why should I?"). But you'll find out that those seemingly trivial things have a huge impact on the quality and correctness of your code. The compiler doesn't care about readbility. But your brain does, and that's where the code actually gets produced.
    I've redone the code.


    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
     
    long factorial (int);
    double exponent (double);
    double sinh (double);
    
    int main()
    {
        double x;
        double result;
        double e=0;
        double sin_h;
        
        for (x=-1.0; x<=1.0; x=x+0.1)
        {
            printf("X is: %.1lf\n", x);
            sin_h= sinh(x);
            e = exponent (x);
        }
      
        
        system("pause");
        return 0;
    }
    
    
    double exponent (double x)
    {
           double e;
           double sin_h;
           double sum=0;
    	   int n;
    	    
    	   n=0;
    	   while (1)
    	   {
    	           e = pow(x, n) / factorial(n);
    	           sum+= e;
    	           
    	          if( e < 0.000001 && e > -0.000001)
    	          break;
               
    	          n++;
    	   }
           printf("e is %lf\n",sum);
    }
    
    long factorial (int n)
    	{
    	   int factres= 1;
    	   while (n > 1)
    	   {
                 factres = factres*n;
                 n--;
            }
            return (factres);
    	}
    
    double sinh (double x)
    {
        double sin_h;
         
       
           sin_h= (exponent(x)-exponent(-x))/2; 
           
       
        printf("sinh is %lf\n",sin_h);
    }
    I know its almost the same code. However I want to know why when I run the code for every value of x sinh is equal to: 1.#QNAN0
  2. #17
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    C'mon, it shouldn't be so hard to properly format that stuff.

    • Statements on the same level start in the same column.
    • If you go one level down, you hit <tab> exactly once.
    • Consistent spacing around operators and functions.
    • When you change the code, you reformat it according to the rules above.

    Here, let me help you:

    c Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
     
    long factorial(int);
    double exponent(double);
    double sinh(double);
     
     
    int main()
    {
        double x;
        double result;
        double e = 0;
        double sin_h;
     
        for (x = -1.0; x <= 1.0; x = x + 0.1)
        {
            printf("X is: %.1lf\n", x);
            sin_h = sinh(x);
            e = exponent(x);
        }
     
        system("pause");
        return 0;
    }
     
     
    double exponent(double x)
    {
        double e;
        double sum = 0;
        int n;
     
        n = 0;
        while (1)
        {
            e = pow(x, n) / factorial(n);
            sum += e;
     
            if (e < 0.000001 && e > -0.000001)
                break;
     
            n++;
        }
     
        printf("e is %lf\n", sum);
    }
     
    long factorial(int n)
    {
        int factres = 1;
     
        while (n > 1)
        {
            factres = factres * n;
            n--;
        }
     
        return factres;
    }
     
    double sinh(double x)
    {
        double sin_h;
     
        sin_h = (exponent(x) - exponent(-x)) / 2;
     
        printf("sinh is %lf\n", sin_h);
    }

    You should also consider refactoring the while (1) ... break with a proper do-while loop.

    Your exponent() doesn't return anything.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by Jacques1
    C'mon, it shouldn't be so hard to properly format that stuff.

    • Statements on the same level start in the same column.
    • If you go one level down, you hit <tab> exactly once.
    • Consistent spacing around operators and functions.
    • When you change the code, you reformat it according to the rules above.

    Here, let me help you:

    c Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
     
    long factorial(int);
    double exponent(double);
    double sinh(double);
     
     
    int main()
    {
        double x;
        double result;
        double e = 0;
        double sin_h;
     
        for (x = -1.0; x <= 1.0; x = x + 0.1)
        {
            printf("X is: %.1lf\n", x);
            sin_h = sinh(x);
            e = exponent(x);
        }
     
        system("pause");
        return 0;
    }
     
     
    double exponent(double x)
    {
        double e;
        double sum = 0;
        int n;
     
        n = 0;
        while (1)
        {
            e = pow(x, n) / factorial(n);
            sum += e;
     
            if (e < 0.000001 && e > -0.000001)
                break;
     
            n++;
        }
     
        printf("e is %lf\n", sum);
    }
     
    long factorial(int n)
    {
        int factres = 1;
     
        while (n > 1)
        {
            factres = factres * n;
            n--;
        }
     
        return factres;
    }
     
    double sinh(double x)
    {
        double sin_h;
     
        sin_h = (exponent(x) - exponent(-x)) / 2;
     
        printf("sinh is %lf\n", sin_h);
    }

    You should also consider refactoring the while (1) ... break with a proper do-while loop.

    Your exponent() doesn't return anything.
    Sorry I didn't exactly know how to properly format it. Ok so now my sinh does give a result after I made my exponent function return a value. However I want it to return 2.71 (e) as the value.

    I want my output to be
    when x= -1 sinh(x)= -1.175201
    x= -0.9 sinh(x)= −1.026517
    and so on until x=1.

    Right now i get for x=1 sinh(x)= -0.91261
  6. #19
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by vilodic
    Right now i get for x=1 sinh(x)= -0.91261
    Are you sure you're looking at the right numbers? My output is

    Code:
    X is: 1.0
    e is 2.718282  -> this is exponent(1.0)
    e is 0.367879  -> this is exponent(-1.0)
    sinh is 1.175201
    And that's correct (within the precision limits you've chosen).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by Jacques1
    Are you sure you're looking at the right numbers? My output is

    Code:
    X is: 1.0
    e is 2.718282  -> this is exponent(1.0)
    e is 0.367879  -> this is exponent(-1.0)
    sinh is 1.175201
    And that's correct (within the precision limits you've chosen).
    I want the exponent function to always use e= 2.71... so that exponent(x) = e^x

    I don't get those readings for sinh. I only did the printing of the e value to check that my program was doing something at least. I just need to print x from -1 t0 1 (-1,-0.9,-0.8...) (which my program does) and for each x print the corresponding value of sinh (x).

    So when x=0.9 sinh= exponent(0.9) - exponent(-0.9) / 2

    and exponent should be e(2.71..)^x so e^0.9.
  10. #21
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by vilodic
    I want the exponent function to always use e= 2.71... so that exponent(x) = e^x

    I don't get those readings for sinh. I only did the printing of the e value to check that my program was doing something at least. I just need to print x from -1 t0 1 (-1,-0.9,-0.8...) (which my program does) and for each x print the corresponding value of sinh (x).

    So when x=0.9 sinh= exponent(0.9) - exponent(-0.9) / 2

    and exponent should be e(2.71..)^x so e^0.9.
    But you're code is producing the right number.
  12. #22
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Give us the exact code you're currently using (formatted, please).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #23
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    It is the same code but I added return sum to this.

    Code:
    double exponent (double x)
    {
        double e;
        double sum = 0;
        int n;
    	    
        n=0;
        while (1)
        {
            e = pow(x, n) / factorial(n);
            sum += e;
     
            if (e < 0.000001 && e > -0.000001)
                break;
     
            n++;
        }
           printf("e is %lf\n",sum);
    
           return sum;
    }
  16. #24
  17. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Are you still not getting correct answers?
  18. #25
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by bullet
    Are you still not getting correct answers?
    Ok I figured it out it was something with the compiler rather than my code. I'm getting the right answers.

    One question. Is there a way to Print values in a table like form

    like
    X =-1 Sinh(x)=-1.175
    X =-0.9 Sinh(x)=-1.026

    rather than how it is now

    x=-1
    sinh(x)= -1.175
  20. #26
  21. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by vilodic
    Ok I figured it out it was something with the compiler rather than my code. I'm getting the right answers.

    One question. Is there a way to Print values in a table like form

    like
    X =-1 Sinh(x)=-1.175
    X =-0.9 Sinh(x)=-1.026

    rather than how it is now

    x=-1
    sinh(x)= -1.175
    You can try inserting tabs
  22. #27
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by bullet
    You can try inserting tabs
    Thanks a lot. And thanks to all that helped and remained patient with me.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo