Author Topic: pow function not working properly  (Read 6151 times)

Offline gbogya

  • Single posting newcomer
  • *
  • Posts: 3
pow function not working properly
« on: March 02, 2016, 08:07:06 am »
Hi guys!
I discovered recently that the pow function isn't working as it should. For example: pow(10,2) is 99, whereas it should be 100. Pow(100,1) is also 99. I could go on and on giving examples, but I think you understand my point. Could you fix it please? I'm using version 13.12

Offline raynebc

  • Almost regular
  • **
  • Posts: 217
Re: pow function not working properly
« Reply #1 on: March 02, 2016, 08:34:11 am »
1.  This isn't a Codeblocks problem.
2.  pow() returns a floating point value, so you're probably seeing a rounding error converting it to integer.  Find or build a replacement function that uses integer math, or just simply round the result up to the nearest integer like this:
Code
int x = pow(10,2) + 0.5;

Offline gbogya

  • Single posting newcomer
  • *
  • Posts: 3
Re: pow function not working properly
« Reply #2 on: March 02, 2016, 08:42:56 am »
Thanks m8!

studentShiro

  • Guest
Re: pow function not working properly
« Reply #3 on: July 29, 2020, 11:55:26 am »
Can some one explain further about this?

But when I try using Visual C++, it works as it should.
Why can't Code::Blocks?

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1564
Re: pow function not working properly
« Reply #4 on: July 29, 2020, 01:24:08 pm »
Code::Blocks is not a compiler, it knows nothing about pow(). You should ask this in a forum related to your compiler or read about floating point errors.