1

I am trying to use the power function in a program but it is displaying/returning different values in the different text editors. Below is the simple code. I have typecasted since power returns double. In code blocks text editor, the power function is returning 100. But in an atom text editor, it is returning 99. But the same function in atom returns 100 if I replace count by 2. Am I missing installation of any extension in atom?. I don't know what is going on. Any suggestions/corrections are welcome.

#include <stdio.h>
#include <math.h>

 int main(){
  int count=2;
  printf("%d",(int)pow(10,count));
  return 0;
}
Kirollos Mallak
  • 359
  • 4
  • 12
Abhishek
  • 11
  • 2
  • 1
    duplicates: [Why pow(10,5) = 9,999](https://stackoverflow.com/q/9704195/995714), [Why the result of pow(10,2) 99 instead of 100?](https://stackoverflow.com/q/54057687/995714), [Why does pow(5,2) become 24? [closed]](https://stackoverflow.com/q/22264236/995714). In short: never use `pow` for integer power – phuclv Jan 31 '22 at 05:41
  • this may be useful https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Hussien Mostafa Feb 02 '22 at 15:54

2 Answers2

1

My guess is that this is due to a floating point rounding error. It may be that, while the real answer is 100, pow(10, 2) is returning 99.99998. When you convert that to an int, the decimal part gets chopped off.

What you can do is, instead of casting right away, run the result through the lround function (also found in math.h). This will return a long.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 2
    Shouldn't that always produce the same value regardless of the code editor? I think this might have something to do with compiler flags. – swamp Jan 30 '22 at 18:08
  • 1
    Very possible. I admit that I'm not an expert when it comes to floating point subleties. – Daniel Walker Jan 30 '22 at 18:09
  • https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate – Hussien Mostafa Jan 30 '22 at 18:20
  • https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Hussien Mostafa Jan 30 '22 at 18:21
  • The power function is returning 100 in case of pow(10,2). But if I introduce a variable count and initialize it to 2 and pass this in power function I am getting 99. If it was floating point rounding error, in both the cases I should have received 99. Don't you think? – Abhishek Jan 30 '22 at 18:46
  • @Abhishek With something like division, yes. But certain FP operations are not required to follow the normal exact rounding rules. (Hussien’s understanding of FP is completely wrong, though.) – Sneftel Jan 30 '22 at 20:59
  • @HussienMostafa Have you actually read the page you linked to? Specifically the section on how IEEE-754 arithmetic is rounded. – Sneftel Jan 30 '22 at 21:02
  • 1
    @HussienMostafa BTW, the one and only representation of 5.0 as a 32-bit IEEE-754 binary floating point number has the hexadecimal representation 0x40A00000. That representation is exact; it is equal to 5, and not to any other number. – Sneftel Jan 30 '22 at 21:06
  • I think you are right @Sneftel ... I will leave the links and delete my explanation – Hussien Mostafa Jan 31 '22 at 03:51
0

Avoid the pow function at all cost! It calculates a double raised with a double (for all possible values). This is an extremely complex calculation and it's done using an approximation. Rounding the result instead of using the default double to integer cast (which is defined as "round to zero") helps a bit, but not when the approximation errors more than 0.5.

If you intend to calculate "integer raised with an integer" I strongly suggest that you write your own that simply loops over the exponent -- it's way faster and it doesn't have any approximation problems.

(I say this as someone who has spent 25 years writing C compilers.)

Lindydancer
  • 25,428
  • 4
  • 49
  • 68