Thread: A cube root program.

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    A cube root program.

    Just wondering if anyone could help.
    I have written this code and can't seem to complete the program. It should allow a small tolerance to the program. About 0.0001%. Sorry if this seems muddled, my brain is fried.
    Code:
    /* Calculating cube roots using a converging approximation.
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define LIMIT 0.0001
    
    int near_equal(double, double);
    double cube_root(double);
    
    int
    main(int argc, char **argv)
    {
                      double x, y;
                      
                      printf("Enter a value: ");
                      if(scanf("%lf", &x) !=1)  {
                                 printf("No value entered.\n");
                                 exit(EXIT_FAILURE);
    }
    
                    y = cube_root(x);
                    printf(" cube root etc etc)
     
                     return 0;
    }
    
    int near_equal(double x, double y)
    {
                   double scaled_limit;
                         if(fabs(x) >= fabs(y))  {
                                  scaled_limit = LIMIT*fabs(x);
                } else {
                                  scaled_limit = LIMIT*fabs(y);
                           }
    
    #define CUBE_LOWER 1e-6
    #define CUBE_UPPER 1e+6
    #define CUBE_ITERATIONS 25
    
    
    double
    cube_root(double v)  {
                      double next=1.0
                      int i;
                      if (fabs(v)<CUBE_LOWER || fabs(v)>CUBE_UPPER)  {
                                     printf("Warning: cube root may be inaccurate\n");
                      }
                      for (i=0; i<CUBE_ITERATIONS; i++)  {
                                  next = (2*next + v/next/next)/3;
                                  }
                       return next;
    }
    Any help would be greatly appreciated.
    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It doesn't compile.
    It's badly indented (hint, don't mix spaces and tabs in code you post online).
    What do you want us to do?

    Single step it with a debugger, and put 'next' into a watch window, and see how the value changes over time.

    Maybe 25 iterations isn't enough to get within tolerance.

    What input values did you test with?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    I couldn't get tabs to work in the post.
    It compiles for me but i can't get the tolerance working in the program.
    It just gives me cube roots and warns when out of the given range.

    What do you mean by this bit

    "Single step it with a debugger, and put 'next' into a watch window, and see how the value changes over time."

    Thanks for your help

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    He means that you should use a debugger to execute your code line by line and to add a "watch" to the "next" variable to see its content change over time.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    What compiler are u using. What salem is trying to say to put some breakpoint in your code and have a look at the actual values and see how the values change. May be u will get an idea

    What compiler are using??

    ssharish2005

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It depends on your compiler suite if you have debugging tools. The idea is that you can step through the program one statement at a time and have it watch various values for you, displaying what they contain at the given time. It's an alternate to scattering loads of printf statements throughout your program.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Quote Originally Posted by quzah View Post
    It depends on your compiler suite if you have debugging tools. The idea is that you can step through the program one statement at a time and have it watch various values for you, displaying what they contain at the given time. It's an alternate to scattering loads of printf statements throughout your program.


    Quzah.
    @quzah: This is extremely hard to read. Reading yellow on light gray is almost impossible without a keen, good eye. the contrast difference is 3 or so on a 0 to 255 scale (white and black are 255).

    @original poster: If you want tabs, you can either add one in Notepad then copy and paste it as needed, type your post in Notepad with needed tabs, and copy it over, or, if it's in your compiler, copy it over with simple control+C and control+V operations. If you're trying to perform a cubic root operation, the formula is simple, and works if you don't have negative values:

    Code:
    double cubrt(input)
    {
    	return (pow(input, 0.3333333333333333));
    }
    Raising a number to the 1/3 power is the same as taking the cubic root of something. You'll need to include math.h to use this method. The square root is the same as taking the half power of something. You'd use this function in the same way as the sqrt function:

    MyVar = cubrt(27.0); // should give 3

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ulillillia View Post
    @quzah: This is extremely hard to read. Reading yellow on light gray is almost impossible without a keen, good eye. the contrast difference is 3 or so on a 0 to 255 scale (white and black are 255).
    Thank you Capian Obvious. Fortunately for me, the forum doesn't disallow color, because if it did, I wouldn't have anything to amuse myself with. But just you wait, it gets better. You'll see!


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I reakon it looks sexy

  10. #10
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Quote Originally Posted by quzah View Post
    Thank you Capian Obvious. Fortunately for me, the forum doesn't disallow color, because if it did, I wouldn't have anything to amuse myself with. But just you wait, it gets better. You'll see!


    Quzah.
    If you insist on using color, use colors with at least a contrast of 64. Basically, the difference between the color 808080 and either 404040 or C0C0C0 is a contrast of 64. Anything less and it makes text hard to read, especially for those with poor vision. With snagging a screenshot, the color looks to be something like D0D0D0 so anything brighter than the color 909090 should not be allowed. Yellow (especially the slightly darker versions), at 246 CTBC (or F6F6F6; CTBC is color-to-black contrast) is almost unreadable with this and green, at 224 CTBC (or E0E0E0) is far harder to read. Your case of black and magenta is barely acceptable in terms of contrast (magenta is around 144 for the CTBC).

    In addition, having all the color details just wastes space on the server. Each letter, instead of using just 1 byte, uses 24 bytes. If I did that with this message, ignoring the quoted part, it'd be 25 KB!

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ulillillia View Post
    If you insist on using color, use colors with at least a contrast of 64. Basically, the difference between the color 808080 and either 404040 or C0C0C0 is a contrast of 64. Anything less and it makes text hard to read, especially for those with poor vision. With snagging a screenshot, the color looks to be something like D0D0D0 so anything brighter than the color 909090 should not be allowed. Yellow (especially the slightly darker versions), at 246 CTBC (or F6F6F6; CTBC is color-to-black contrast) is almost unreadable with this and green, at 224 CTBC (or E0E0E0) is far harder to read. Your case of black and magenta is barely acceptable in terms of contrast (magenta is around 144 for the CTBC).

    In addition, having all the color details just wastes space on the server. Each letter, instead of using just 1 byte, uses 24 bytes. If I did that with this message, ignoring the quoted part, it'd be 25 KB!
    Frankly my dear, I don't give a damn.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    God favors the ones with over 8k posts !

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    All power to quzah , He invented 'board fashion', fa shiz.

    I wouldn't complain, he makes a huge contribution. His time isn't worthless, show some respect
    Last edited by zacs7; 04-27-2007 at 07:17 AM.

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    All power to quzah, He invented 'board fashion', fa shiz.
    True, agreed. The other thing is his unique style makes others prefect

    ssharish2005

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    ugh the code is missing a bunch of matching brackets and even the double quotes for one of the print statements don't match. The near equal code makes no sense. The only thing that seems kind of correct is the Newton Raphson iteration for the cube root. If v is close to zero however or if next ventured into negative territory (in this case I think you are safe though) you'd be in trouble.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. set root through program
    By linuxman in forum Linux Programming
    Replies: 6
    Last Post: 06-06-2004, 12:34 AM
  3. never compile as root!
    By Lynux-Penguin in forum Linux Programming
    Replies: 6
    Last Post: 08-21-2003, 05:46 PM
  4. Fourth root program. Help plese?
    By correlcj in forum C Programming
    Replies: 7
    Last Post: 07-12-2002, 09:07 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM