mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-08 21:16:26 +08:00
formatting source-code for d7af6fdc8c
This commit is contained in:
@@ -23,29 +23,36 @@
|
||||
|
||||
/** define \f$f(x)\f$ to find root for
|
||||
*/
|
||||
static double eq(double i) {
|
||||
static double eq(double i)
|
||||
{
|
||||
return (std::pow(i, 3) - (4 * i) - 9); // original equation
|
||||
}
|
||||
|
||||
/** get the sign of any given number */
|
||||
template <typename T>
|
||||
int sgn(T val) {
|
||||
int sgn(T val)
|
||||
{
|
||||
return (T(0) < val) - (val < T(0));
|
||||
}
|
||||
|
||||
/** main function */
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
double a = -1, b = 1, x, z;
|
||||
int i;
|
||||
|
||||
// loop to find initial intervals a, b
|
||||
for (int i = 0; i < MAX_ITERATIONS; i++) {
|
||||
for (int i = 0; i < MAX_ITERATIONS; i++)
|
||||
{
|
||||
z = eq(a);
|
||||
x = eq(b);
|
||||
if (sgn(z) == sgn(x)) { // same signs, increase interval
|
||||
if (sgn(z) == sgn(x))
|
||||
{ // same signs, increase interval
|
||||
b++;
|
||||
a--;
|
||||
} else { // if opposite signs, we got our interval
|
||||
}
|
||||
else
|
||||
{ // if opposite signs, we got our interval
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -54,15 +61,19 @@ int main() {
|
||||
std::cout << "\nSecond initial: " << b;
|
||||
|
||||
// start iterations
|
||||
for (i = 0; i < MAX_ITERATIONS; i++) {
|
||||
for (i = 0; i < MAX_ITERATIONS; i++)
|
||||
{
|
||||
x = (a + b) / 2;
|
||||
z = eq(x);
|
||||
std::cout << "\n\nz: " << z << "\t[" << a << " , " << b
|
||||
<< " | Bisect: " << x << "]";
|
||||
|
||||
if (z < 0) {
|
||||
if (z < 0)
|
||||
{
|
||||
a = x;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
b = x;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user