2

I'm making a program, that findes the cubic root of the given number.

I wan't it without using pow(), or only in a way that I could also write here num * num * num, but that doesn't matter. The method, I have to do this is like the method in the code below.

I don't know, where is the problem. It works with the cubic numbers(1,8,27,64), but with other numbers don't. Here's the code:

        cout << "Cube root";
        cout << "Enter a number: ";
        int x; double num = 0;
        cin >> x;
        while (true) {
            if (pow(num + 1, 3) > x) {
                if (pow(num + 0.1, 3) > x) {
                    if (pow(num + 0.01, 3) > x) {
                        if (pow(num + 0.001, 3) > x) {
                            break;
                        }
                    }
                    else { num += 0.01; }
                }
                else { num += 0.1; }
            }
            else { num += 1; }

        }
        cout << num;

For example:
Input: 8 Output: 2
Input: 64 Output: 4
Input : 12 Output: (none)
Input : 12340 Output: (none)

"Output: (none)" means no crash, but I think there is an endless loop.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Please provide an indented [mre] which demonstrates both success and failure, without asking for input. I.e. please hardcode some examples. Show the output and the desired output. – Yunnosch Nov 30 '19 at 12:17
  • @Evg I do not think this is about efficiency or optimisation. It is about debugging. – Yunnosch Nov 30 '19 at 12:19
  • 3
    If you can use `std::pow` then why don't you use it to calculate the cube root directly? If as you say _"... without standard functions..."_ then I think you need another approach which does not use `std::pow`. – Richard Critten Nov 30 '19 at 12:21
  • What do you mean by "Output: (none)" ? Crash? Hang? Endless loop? – Yunnosch Nov 30 '19 at 12:22
  • Try inserting a `else { num += 0.001; }` after `break;}`. – Yunnosch Nov 30 '19 at 12:24
  • I wan't it without pow. I can write here num*num*num, but that doesn't matters. And sorry, i drowe up it bad. The method, i have to do this is like the method in the code. – Gergő Szabó Nov 30 '19 at 12:25
  • "Output: (none)" means no crash, I think endless loop. – Gergő Szabó Nov 30 '19 at 12:26
  • Yes, it is an endless loop if the solution has anything but a "0" in the third decimal place. – Yunnosch Nov 30 '19 at 12:37
  • Yunnosch, I read everything, and I'm trying to answer, but sometimes i have to go away, or if I have to do things fast, Ionly reply, after all the probpems are solved(not only those, i asked). – Gergő Szabó Nov 30 '19 at 13:09
  • I understand and am happy to apologise for assuming that you ran early. But I stand with my comment, on StackOverflow please participate in making each Q/A pair helpful. Do so for each question as it is seen by others. The ultimate success of your project is not of relevance for other users. So please, complete this question. I admit I assume that mine is the most helpful answer, but that you do not need to agree. Also, waiting a little to see whether better answers arrive is actually appreciated. Waiting until all your problems have been solved however is not the right way. – Yunnosch Nov 30 '19 at 13:16
  • that's one of the worst ways to calculate cube root. Not only it's far slower, it's far less precise than other mathematical methods like Newton-Raphson or Taylor series. See [How can I obtain the cube root in C++?](https://stackoverflow.com/q/18103769/995714), [finding cube root in C++?](https://stackoverflow.com/q/4269069/995714), [Trouble writing cube root function [closed]](https://stackoverflow.com/q/21490237/995714), [Seeding the Newton iteration for cube root efficiently](https://stackoverflow.com/q/7463486/995714) – phuclv Nov 30 '19 at 15:59
  • I would suggest having a look at [Computing a correctly rounded / an almost correctly rounded floating-point cubic root](https://stackoverflow.com/questions/18063755/computing-a-correctly-rounded-an-almost-correctly-rounded-floating-point-cubic). – njuffa Dec 02 '19 at 23:49

3 Answers3

1

You can have a really easy solution by just doing:
This is equivalent to getting the cube root.

pow(num+0.0, 1.0/3.0);

I know, but I have to do it with the method like in the code.

Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
MorganS42
  • 656
  • 4
  • 21
  • You missed one of OPs comments, which explicitly excludes (rephrased) a solution with fractionals in `pow()`. – Yunnosch Nov 30 '19 at 12:34
  • But it's impossible to do it without using any use of "pow()", even OPs code uses "pow()" – MorganS42 Nov 30 '19 at 12:41
  • 1
    As OP explained, the use of `pow(X,3)` can easily be replaced. And within the obvious scope (precision to 3 or 4 decimal places and no need for speed) it IS possible. See OPs comment under my answer. OP did not get the formatting in the comment correct. What they meant is of course `num * num * num`. – Yunnosch Nov 30 '19 at 12:42
0

At this point

if (pow(num + 0.01, 3) > x) {
    if (pow(num + 0.001, 3) > x) {
        break;
    }
}
else { num += 0.01; }

you turn into an endless loop, if num + 0.001 is not higher , but e.g. num + 0.002 (or of course num + 0.01) would be.

Insert a else { num += 0.001; } after break;}.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Hmm. In that case I should see a large green "tick" next to my answer and you should have an increased reputation. That is why I doubt that you accepted in the sense described in the link I provided. – Yunnosch Nov 30 '19 at 13:31
  • That is what I meant, you found it. Sorry for nagging you. I hope it feels like help with the StackOverflow idea. I do not intend to punish you. (I do however admit that I feel satisfied now.) – Yunnosch Nov 30 '19 at 13:36
  • Sorry, i didn't see that big ✔ – Gergő Szabó Nov 30 '19 at 13:37
  • It is normal to need some help with finding it the first time. Good luck for your future participation on StackOverflow. – Yunnosch Nov 30 '19 at 13:38
0

I like this solution (first guess is just a half of a value, but you could use anything less than value and greater than 0) https://wandbox.org/permlink/4kYBkJomEGWbc5ay

It uses Taylor series to approximate value of cubic root, and should be faster than linear incrementing/decrementing guess value.

bialy
  • 193
  • 2
  • 14
  • This is another answer to the question "How to optimise my cubic root finder?", which hoever is not the question actually asked here. – Yunnosch Nov 30 '19 at 13:18