# A cube root program.

• 04-26-2007
guest111
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.
• 04-26-2007
Salem
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?
• 04-27-2007
guest111
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."

• 04-27-2007
KONI
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.
• 04-27-2007
ssharish2005
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
• 04-27-2007
quzah
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.
• 04-27-2007
ulillillia
Quote:

Originally Posted by quzah
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
• 04-27-2007
quzah
Quote:

Originally Posted by ulillillia
@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.
• 04-27-2007
zacs7
I reakon it looks sexy :)
• 04-27-2007
ulillillia
Quote:

Originally Posted by quzah
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!
• 04-27-2007
quzah
Quote:

Originally Posted by ulillillia
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.
• 04-27-2007
KONI
God favors the ones with over 8k posts !
• 04-27-2007
zacs7
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 :D
• 04-27-2007
ssharish2005
Quote:

All power to quzah, He invented 'board fashion', fa shiz.
True, agreed. The other thing is his unique style makes others prefect :)

ssharish2005
• 04-27-2007
SevenThunders
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.