-2
for (int i=0; i<30; i ++)
    {   a=pow(i,2);
       cout<<a<<endl;
    }

the code produces the following output .. 0 1 4 9 16 24 36 49 64 81 99 120 and so on it is reducing the squares at certain values by1 like for 5 it produces 24 and 10 it gives 99 and so on.

for (int i=0; i<30; i ++)
    {   
       cout<<pow(i,2);
    }

the above code works fine . please help me ..and tell me whats wrong when i am doing it with the help of a variable

user3079430
  • 29
  • 1
  • 1
  • 4
    Again problem with `POW` :P – P0W Dec 08 '13 at 08:37
  • @P0W LOL, fair enough :D –  Dec 08 '13 at 08:38
  • 4
    This question appears to be off-topic because it is the (n + k + 2)th incarnation of the "stupid floating-point doesn't work" question. –  Dec 08 '13 at 08:38
  • [Read this.](http://www.validlab.com/goldberg/paper.pdf) –  Dec 08 '13 at 08:40
  • 1
    Please show us a minimal *complete* program that we can run and that demonstrates the problem. – NPE Dec 08 '13 at 08:40
  • problem solved ..thank you guys ....just changed the a from int to double :D – user3079430 Dec 08 '13 at 08:47
  • @user3079430 That is **not** the solution. The solution is to use `int`s when you need them, and **not** use `pow()` (or at least not its overload for floating-point numbers). –  Dec 08 '13 at 08:48
  • (Oh, and just FYI, the (n + k + 1)th question [can be found here](http://stackoverflow.com/questions/20450873/incorrect-rounding-by-c#comment30555065_20450873)). –  Dec 08 '13 at 08:49
  • @H2CO3 This question is different from the most-repeated floating-point questions on StackOverflow in that it asks why the result of a floating-point operation is not N for a single operation whose mathematical result, N, is exactly representable as a floating-point number. Confusing the two kinds of questions is to display the same fundamental misapprehension about the nature of floating-point. `pow(10, 2)` is not different of 100 because floating-point. `pow(10, 2)` is different of 100 because the particular `pow()` being used is crap. – Pascal Cuoq Dec 08 '13 at 10:12
  • See http://blog.frama-c.com/index.php?post/2013/04/06/Non-experts-need-accurate-floating-point-the-most for a discussion of pow() and how you would be right to expect it to work as you expect, but often will find that it doesn't. – Pascal Cuoq Dec 08 '13 at 10:16
  • @PascalCuoq I agree that the `pow()` used is crap, but it *has the right to behave as it behaves.* And all what it boils down is the inexact nature of FP. (Which, of course, gets worse when the inexact nature of the implementation interferes with it as well.) –  Dec 08 '13 at 10:21
  • 1
    This question is more properly a duplicate of http://stackoverflow.com/questions/16923249/unusual-output-from-pow – Pascal Cuoq Dec 08 '13 at 10:36

3 Answers3

3

while pow has an overload with int exponent, and while e.g. double can represent integers exactly, pow is not guaranteed to special-case integer powers. it might just calculate them using logarithms or whatever. as a result, an integer power might be computed approximately.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

because depending on the implementation, it can produce approximate results even with integral powers. It always produces approximate result for non-integral powers.

Kaustav Ray
  • 744
  • 6
  • 20
  • 1
    A low quality `pow()` implementation can produce approximate results even when the mathematical result is exactly representable. A correctly rounded `pow()` implementation produces the exact result when the mathematical result is exactly representable. This includes some non-integral powers, for instance `pow(4, 0.5) == 2` for a correctly rounded `pow()` function. – Pascal Cuoq Dec 08 '13 at 10:07
0

YOU CAN USE THE BELOW FORMAT TO GET DESIRED OUTPUT:

for (int i=0; i<30; i ++)
{  
  floor(pow(l, ++x) + .5);
   cout<<a<<endl;
}