1

I wrote this code and first time of loop result is 99. Why is result 99, when it should be 100?

#include <iostream>
#include<math.h>
using namespace std;
int main ()
{
  int skt = 0;
  int sk[3];
int nsk = 3;
sk[0]=1;
sk[1]=2;
sk[2]=8;
for (int i=0; i<nsk; i++)
{
    skt = skt + (sk[i]*pow(10.0,nsk-i-1));
    cout <<" "<<skt<<endl;
}

}

the result of this code

   99
   119
   127

,but if I use library cmath it is correct answer

#include <iostream>
#include<cmath>
using namespace std;
int main ()
{
  int skt = 0;
  int sk[3];
int nsk = 3;
sk[0]=1;
sk[1]=2;
sk[2]=8;
for (int i=0; i<nsk; i++)
{
    skt = skt + (sk[i]*pow(10.0,nsk-i-1));
    cout <<" "<<skt<<endl;
}

}

the result of this code

    100
    120
    128

Could anybody explain why?

  • 1
    pow is for doubles not integers. Remember conversion from a double to integer truncates. – drescherjm Sep 28 '16 at 20:03
  • 2
    Since you are using `pow` you should read: http://stackoverflow.com/questions/588004/is-floating-point-math-broken – NathanOliver Sep 28 '16 at 20:03
  • Not 100% sure if it should be used as a dupe target. – NathanOliver Sep 28 '16 at 20:03
  • 1
    [See also this about using pow and integer exponents](http://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os). – PaulMcKenzie Sep 28 '16 at 20:06
  • Also, there really is no reason to be calling `pow` if all you want are powers of 10. A simple lookup table would be much faster than calling the `pow` function. – PaulMcKenzie Sep 28 '16 at 20:19
  • 1
    [Cannot duplicate](http://coliru.stacked-crooked.com/a/17eac6debeec0b73). But again, it depends on how the `pow` function is implemented, compiler switches, compiler versions, etc. But if you want consistency, stop using `pow` and use a simple table, [as this shows you](http://coliru.stacked-crooked.com/a/ed3df19aad0edfb1) – PaulMcKenzie Sep 28 '16 at 20:38

3 Answers3

0

from this link cplusplus.com

Additional overloads are provided in this header ( cmath ) for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).

I bet if you cast it it would be correct in math.h

Cubbi
  • 46,567
  • 13
  • 103
  • 169
corn3lius
  • 4,857
  • 2
  • 31
  • 36
0

The math.h and cmath versions of pow are slightly different. Basically there are more supported inputs for the cmath version. You can compare the two at these links.

math.h and cmath

Jacob McCarthy
  • 218
  • 3
  • 15
0

As people have commented, it's probably related to variable type conversion and floating point errors. Pow operates on doubles, which can have floating point errors. Chances are pow is returning a value like 99.9999999 which is then being converted to an integer. Since the integer conversion truncates instead of rounds, you get 99.

DavidP
  • 613
  • 5
  • 12