-1

I set the variable i to 0 as an initial condition of my for loop. I know for a fact that at the end of the loop, i >= 1 (and I will manipulate my input such that this holds true). I then subtract 1 from i and THEN use the pow() function. At worst, it would have (0,0) which it can handle and would output 1.

When I run my code, however, the program outputs nothing. No exit codes, nothing. When I went into the debugger (my first time there, mind you), I saw pow(2,-1) hidden amid the complex stuff I didn't understand.

Does the computer still check for i = 0 even after manipulation?

Below is the function I'm talking about. I'm trying to write the input in base 2, but such that I can always change the base later.

#include <iostream>
#include <vector>
using namespace std;

vector<int> base(int x)
{
    int i;
    int mult;
    vector<int> res;
    while (true) {
        for (i = 0; pow(2, i) < x; i++) {}
        i = i - 1;
        for (mult = 1; mult * pow(2, i) < x; mult++) {}
        mult = mult - 1;
        res.push_back(mult);
        x = x - (mult * pow(2, i));
        if (x == 0) {
            break;
        }
    }
    return res;
}

int main()
{
    cout << base(90)[0];
}
DVnyT
  • 1
  • 1
  • 2
    You aren't doing anything inside of your `for` loop – stefan_aus_hannover Jun 29 '21 at 17:47
  • I'm incrementing 'i' and 'mult', right? – DVnyT Jun 29 '21 at 17:49
  • What exactly are you trying to do? – Leonardo Alves Machado Jun 29 '21 at 17:52
  • 3
    Do you mean it literally produces no output at all? When I run it, it outputs "1". – David Schwartz Jun 29 '21 at 17:52
  • 1
    The program is invalid because `pow` is not declared in any of the header files you `#include`. – n. m. could be an AI Jun 29 '21 at 17:53
  • Does [adding debug prints](https://godbolt.org/z/PGfnKK5de) help? – Ted Lyngmo Jun 29 '21 at 17:54
  • @David Schwartz none at all. 1 should be the correct output. – DVnyT Jun 29 '21 at 17:54
  • Then add a `<< '\n';` to the line `cout << base(90)[0];` making it `cout << base(90)[0] << '\n';` – Ted Lyngmo Jun 29 '21 at 17:54
  • After including math, I also get 1 as an output – stefan_aus_hannover Jun 29 '21 at 17:55
  • 2
    Any program that uses `pow()` with two integer parameters is automatically wrong, especially if one of those two integers is 2. This is not what `pow()` is for, and it doesn't do what you think it does. – Sam Varshavchik Jun 29 '21 at 17:55
  • @Leonardo Alves Machado Trying to write the input in base 2, but also keeping it such that I can expand it to other bases. – DVnyT Jun 29 '21 at 17:56
  • @Sam Varshavchik Could you elaborate? Is it something to do with how pow() outputs 25.000001 for (5, 2)? I believe I've read that some compilers will output the incorrect answer for even basic arguments. – DVnyT Jun 29 '21 at 17:59
  • Yes. To compute 2^n for an integral n, you can do `1 << n`, as long as the answer fits into an integral type. – HolyBlackCat Jun 29 '21 at 18:01
  • You are correct. No C++ compiler can alter the fundamental fact that [floating point math is broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). The shown code will not work. It's unclear what problem the shown code is trying to solve, but the attempted approach is fundamentally flawed and will not work correctly in some cases due to the fact that floating point math is broken. – Sam Varshavchik Jun 29 '21 at 18:04
  • 1
    Aside from the problems being discussed regarding `pow` and floating-point approximations -- should this question be closed for not being reproducible? It sounds like the application not running is likely a setup issue, which is unlikely to be resolved without a lot more information. – Human-Compiler Jun 29 '21 at 18:21
  • use `static_cast(pow(2,i)+.5)` which will get rid of floating errors within your range of ints for i>=0 – doug Jun 30 '21 at 03:00

1 Answers1

-1

Floating point math is your first problem, pow simply ain't exact, and you should have used repeated multiplication or just 1 << x with integers instead.

That's not your only issue though, try an figure out what happens with your loop for x == 1 (which is what you end up if rounding errors kick in). pow(2, 0) < 1 is already not fulfilled, giving you that -1 you observed.

Should have used <= instead. Together with pure integer arithmetic, that yields the correct result.

Ext3h
  • 5,713
  • 17
  • 43