I am having trouble getting this program to evaluate a range of values through a formula.
range = (-3.0,4.0) formula = (9(x^3)-27(x^2)-4x+12) / (sqrt(3(x^2)+1)
+ abs(5-(x^4)))
The program prints out the y as -1.IND
Any help on shedding light on why all the y comes out as -1.IND is appreciated.
Thanks, Dave
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "This program outputs formula results\n";
long double x = -3.0;
long double y = 1.0;
long double a = (9*(pow(x,3))-27*(pow(x,2))-4*x+12);
long double b = (sqrt(3*(pow(x,2))+1) + abs(5-(pow(x,4))));
y = a/b;
for(;x <4.5; x=x+.5){
cout << "X = " << x << ", " << "Y = " << y;
if(y==0)
cout << "Y IS ZERO" << endl;
else if(y<0)
cout << "Y IS NEGATIVE" << endl;
else if(y>0)
cout << "Y IS POSITIVE\n" << endl;
}
return 0;
}
When
xis-3, or negative in general, you’re trying to calculatesqrt(3*(pow(x,3))which is a square root of a negative number. That’s why you’re getting weird results. Usingpowis not terribly efficient for calculating integer powers. But for small programs it may not matter.Addendum: As Nawaz notes in the comment, please do reevaluate y every time in the loop or else you will only get y for the initial x.