I wrote a function to calculate the value of the function y = f(x) = x^3 + 2*x^2 - 4x + 1.
But then I realise that the result when using the function vs the result when not using the function is different at some value of x. Here's my code:
#include<iostream>
#include<cmath>
using namespace std;
int function(int x) {
int y= pow(x, 3) + 2*x*x - 4*x + 1;
return y;
};
int main(void) {
for(int i = -5; i < 10; i++) cout << function(i) << std::endl;
for(int i = -5; i < 10; i++) cout << pow(i, 3) + 2*i*i - 4*i+1 << ' ';
return 0;
}
And the result is:
-53 -15 3 9 6 1 0 9 34 81 155 265 414 609 856
-54 -15 4 9 6 1 0 9 34 81 156 265 414 609 856
I'm so confused right now. Please help.