Reputation: 1
I want to find approximate polynomial roots of a polynomial equation.
#include <fstream>
#include <cmath>
#include <iostream>
using namespace std;
bool polynomial_algorithm(double a, double b, double c, double d, double e,int& sum, int i1, int i2)
{
double y = 0;
for (double i = i1; i <= i2; i += 0.0000001)
{
double x = (a * pow(i, 4) + b * pow(i, 3) + c * pow(i, 2) + d * i + e);
}
return true;
}
int main()
{
int i1,i2,sum=0;
double a, b, c, d, e;
//interval configuration:
i1 = -1;
i2 = 2;
cout << "interval (" << i1 << ";" << i2 << ")" << endl;
//configuration of values:
a = -1;
b = 3;
c = static_cast<double>(-4) / 9;
d = static_cast<double>(-4) / 3;
e = static_cast<double>(32) / 81;
cout << a << " " << b << " " << c << " " << d << " " << e << endl;
if (i2 < i1)
{
cout << "invalid interval";
}
cout << endl << "compiling" << endl;
cout << endl << "compilation success: "<<polynomial_algorithm(a,b,c,d,e,sum,i1,i2) << endl;
}
This is the code I currently have, how do I got about checking if double x
≈ 0 in bool polynomial_algorithm
?
I tried reading articles about the std::abs
function and other stuff but it just really fried my brain, I appreciate the help if I get any.
Upvotes: -1
Views: 137
Reputation: 1
I figured it out by using a bool
variable to determine when a polynomial hits 0
watching when it becomes negative or positive by reading from a file.
bool rootofpolynome(int sum)
{
fstream data("s.txt");
double numb,numb1;
bool ispositive;
data >>numb>>numb1;
if (numb < 0)
{
ispositive=0;
}
while (!data.eof())
{
data >>numb>>numb1;
if (ispositive == false && numb > 0)
{
cout << "root " << numb1 << endl;
result << "root " << numb1 << endl;
sum++;
ispositive=1;
}
if (ispositive==true && numb < 0)
{
cout << "root " << numb1 << endl;
result << "root " << numb1 << endl;
ispositive=0;
sum++;
}
}
cout << "found " << sum << " polynomial roots" << endl;
return true;
}
Upvotes: -1