1
#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    int temp = 0, n = 1100, x = 0, t2 = 0, l = 10;
    while(temp < n)
    {
        t2 = temp;

        temp += pow(l, ++x);

        cout << t2 << " " << temp << " " << x <<endl;

    }

    return(0);
}

The output obtained is :

0 10 1 
10 109 2
109 1109 3

but I expect the output :

0 10 1
10 110 2
110 1100 3

Why this difference ?..please help ..I cant find out the problem

10
  • 3
    Are you allergic to spaces? Probably shouldn't use a variable named l. Looks awfully like 1. Commented Dec 8, 2013 at 6:29
  • 2
    Not knowing what toolchain you're using, all I can tell you is using <cmath> and clang 3.3 I get your expected output. Commented Dec 8, 2013 at 6:32
  • Your program is giving correct output in my system ! This undefined behavior of pow function has been observed many times ! Commented Dec 8, 2013 at 6:33
  • no.. I compiled it using codeblocks...I am getting the wrong output Commented Dec 8, 2013 at 6:35
  • Oh...finally I got it right somehow...I used temp+=ceil(pow(l,++x)); ceil did the job there...but I still dont understand why it was like that Commented Dec 8, 2013 at 6:36

2 Answers 2

1

Don't use pow for integer arithmetic. Try

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    int temp = 0, n = 1100, x = 0, t2 = 0, l = 10;
    while(temp < n)
    {
        t2 = temp;

        int t3 = 1, t4 = 0;
        ++x;
        while (t4++ < x) t3 *= l; 
        temp += t3;

        cout << t2 << " " << temp << " " << x <<endl;

    }

    return(0);
}

// or alternatively

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    int temp = 0, n = 1100, x = 0, t2 = 0, l = 10;
    while(temp < n)
    {
        t2 = temp;

        temp += floor(pow(l, ++x) + .5);

        cout << t2 << " " << temp << " " << x <<endl;

    }

    return(0);
}
Sign up to request clarification or add additional context in comments.

2 Comments

why not pow function ?
@KaustavRay, because depending on the implementation, it can produce approximate results even with integral powers. It always produces approximate result for non-integral powers. In your case, probably pow is returning a floating point value slightly less than 10, maybe 9.99999999987. But because you're assigning this value to an integer, the compiler implicitly converts the floating point number to the integer 9.
0

By default pow returns double. This means that when you use the expression temp += pow(l, ++x); there is a hidden cast from double to int, in order to match the type of temp.

Doubles do not have an exact representation (like ints). Therefore, the double value for 100 can be something like 99.999999..99832. When this value is converted to int, only the number before the decimal point is taken into account. Thus, the corresponding value for 100 will be 99. You can correct this by adding some very small value (like epsilon in mathematics):

while(temp < n)
{
    t2 = temp;
    double d = pow(l, ++x);
    cout << (int) d << endl;
    cout << (int) (d + 1e-10) << endl;  // 1е-10 is 0.0000000001

    temp += (int) (d + 1e-10);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.