1

I've been trying to solve this for an hour now and it is very frustrating, I cannot understand why the next loop just doesn't verify itself

while(p>1) {
    p--;
    if (pow(n, contor) == p) {
        contor2++;
    }
}

the full code being

#include <iostream>
#include <cmath>

int main()
{
    int n, p, resetP;
    float contor = 0;
    float contor2 = 0;
    n = 3;
    p = 100;
    resetP = p;
    while (p > 1)
    {
        contor++;
        p = p / n;
    }
    p = resetP;
    while (p > 1)
    {
        p--;
        if (pow(n, contor) == p)
            contor2++;
    }

    std::cout << contor2;
}

what I am trying to do here is verify if(pow(3, 4)==81 but it just doesn't seem to work. The p I set it from 100 to go one by one towards 0. When it gets to 81, it simply gets past over it, like it just doesn't verify the if and my contor2 stays the exact same (that being 0). I've been studying C++ for over 6 months now, but this is the first time I'm really stuck on something.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • What do you mean by "a loop that verifies itself"? – mkrieger1 May 06 '20 at 22:45
  • 2
    Why do you expect `pow( 3, 4.0f )` to be exactly equal 81? Is `contor2` increased when `p == 80`? – Slava May 06 '20 at 22:47
  • The code provided would not compile, could you try including a [minimal reproducible example?](https://stackoverflow.com/help/minimal-reproducible-example) – Derek C. May 06 '20 at 22:48
  • @mkrieger1 sorry, not verifies itself, I meant why doesn't the loop verify the `if pow` statement. – jaketherazvy May 06 '20 at 22:49
  • @Slava That is the problem, contor2 never increases at all. I expect `pow(3, 4)==81` because 3x3x3x3=81 – jaketherazvy May 06 '20 at 22:50
  • And by "verify an if statement" you mean that the condition in the `if` is true? – mkrieger1 May 06 '20 at 22:51
  • @DerekC. I think this is the same thing : int p = 100, contor = 4, contor2 = 0, n = 3; while(p>1){ p--; if(pow(n, contor)==p) contor2++; } cout< – jaketherazvy May 06 '20 at 22:53
  • @mkrieger1 yes.. I don't know how to properly explain it sorry, i've always had problems on explaining or reading the code – jaketherazvy May 06 '20 at 22:55
  • @jaketherazvy, this is a good oportunity yo use a debugger. – anastaciu May 06 '20 at 22:57
  • 1
    The `pow` function returns a floating point value. You are assigning the result to an integer. There's going to be some truncation here. – Thomas Matthews May 06 '20 at 22:58
  • Due to the imprecise nature of floating point variables, you can't compare for equality. Subract them and compare for a "delta" value. – Thomas Matthews May 06 '20 at 23:05
  • 1
    "I expect pow(3, 4)==81 because 3x3x3x3=81" read about floating point arithmetic and why it is not a good idea to compare floating point using equality operator – Slava May 06 '20 at 23:06
  • Beware of *integer division*. The result of `p / n` may not produce the results you are looking for. Better to convert one of them to floating point before the division. – Thomas Matthews May 06 '20 at 23:07
  • @anastaciu I did, it goes to 81 and `contor2` never reaches 1. It only stays at 0 – jaketherazvy May 06 '20 at 23:07
  • @jaketherazvy, it does change to 1, check it here https://wandbox.org/permlink/3ZB498COxvlcdtKV – anastaciu May 06 '20 at 23:09
  • @anastaciu hmm that is true, I copied the exact same code and I am still getting 0. It's probably something from my codeblocks, I'll try what Thomas Matthews said to me. – jaketherazvy May 06 '20 at 23:13
  • @jaketherazvy He is correct, integer division truncates the result, though it shouldn't be a problem in this instance. – anastaciu May 06 '20 at 23:17
  • @anastaciu the part you removed was changing counter to 4 , with your code it's 0 – emaditaj May 06 '20 at 23:23
  • @emaditaj, you're right I deleted the contor increment while messing with it and thought it was the original code. – anastaciu May 06 '20 at 23:26
  • @jaketherazvy, in this case using a float p wolud be worse, since without truncation you will have decimal values, the chances of a mach reduces dramatically. – anastaciu May 06 '20 at 23:31
  • so I figured something weird, if I put `if(3*3*3*3)==81` instead of `if(pow(n, contor)==81)` it works. it gives me a 1. – jaketherazvy May 06 '20 at 23:32
  • So the problem is clearly your implementation of pow, I guess the solution could be to try some other compiler, you can also try to assign the result of pow to a local variable, before testing it, maybe an int, another thing you can try is to optimize your compilation. – anastaciu May 07 '20 at 00:13
  • don't use pow for such purposes. Write an integer pow function instead. See [Why does pow(n,2) return 24 when n=5, with my compiler and OS?](https://stackoverflow.com/q/25678481/995714), [Why pow(10,5) = 9,999 in C++](https://stackoverflow.com/q/9704195/995714), [Why does pow(5,2) become 24?](https://stackoverflow.com/q/22264236/995714)... – phuclv May 07 '20 at 02:15

1 Answers1

0

i runned your code a few time with msvsc 19 and g++ both with x86 and x64, it worked as excepted and printed 1 entered into if statement when p got to 81 and counter2 added by 1 here's the code i tried

#include <iostream>
#include <cmath>
int main() {
    int n, p, saveP = 0, resetP;
    float contor = 0;
    float contor2 = 0;
    n = 3;
    p = 100;
    resetP = p;
    while (p > 1) {
        contor++;
        p = p / n;
    }
    p = resetP;
    while (p > 1) {
        p--;
        if (pow(n, contor) == p)
            contor2++;
    }
    std::cout << contor2;
}

check your compiler setting(maybe you are not compile the cpp file you are editing) and also check for syntax error (like scopes in if statement )

i don't know what your whole code is but for here it's beeter to declare counter and counter2 as int , and p as float

emaditaj
  • 177
  • 9
  • thanks, but I've tried them right now, it's still 0 for me. this is weird. – jaketherazvy May 06 '20 at 23:10
  • @jaketherazvy i runned at multi online cpp compilers too (example https://onlinegdb.com/rklX9NagqU) , it works . copy paste my exact code ,and chec if you are editing the right cpp file .. if not worked try creating new project and trying. it must be some thing with the compiler, also try in debug mode or disable optimization – emaditaj May 06 '20 at 23:14
  • @jaketherazvy if stll not working , post the disassembly , it can be a optimization error with your compiler – emaditaj May 06 '20 at 23:16
  • on debug mode it is 0, on release mode it is 99 – jaketherazvy May 06 '20 at 23:25
  • @jaketherazvy if the result change on debug vs release mode , and it's always 0 in debug and 99in release, than this is a bug with your compiler . what compiler are you using ? can you generate the disassembly and post in your question , if we can verify it's a bug in compiler than what you should do is use another one and report this to manufacture – emaditaj May 06 '20 at 23:29
  • @jaketherazvy also if you are running this in a part of much larger project, it's possible that some other part of your code is messing up with memory of this part (that's how c++ works), try this alone and see if it works – emaditaj May 06 '20 at 23:30
  • so I figured something weird, if I put `if(3*3*3*3)==81` instead of `if(pow(n, contor)==81)` it works. it gives me a 1. – jaketherazvy May 06 '20 at 23:31
  • No.. I was just doing some problems off the internet. no other projects. I am running codeblocks if that's what you're asking – jaketherazvy May 06 '20 at 23:32
  • GNU GCC Compiler. Sorry. im still new to this – jaketherazvy May 06 '20 at 23:34
  • @jaketherazvy why not debug and see exact values of n and contor before if statement and see what is going wrong with your compiling – emaditaj May 06 '20 at 23:39
  • @jaketherazvy i tried gcc in here https://rextester.com/l/cpp_online_compiler_gcc it prints 1, try compling with gcc from console without codeblocks , if still not working try downloading official gcc installer for your os, or recompiling gcc – emaditaj May 06 '20 at 23:41
  • I debugged it with Watches, N is 3 and Contor is 4, while p is going down one by one from a hundred. It is definitely my compiler. I will try to fix more tommorow, i'll let you know. thanks a lot for your time – jaketherazvy May 06 '20 at 23:56