0

I am trying to solve a very simple equation in c++ by brute force. The basic idea is to run up or down the value of x until the left side of the equation is equal to the right side. I am not getting any errors, but the value of x is always either 0.01 or -0.01. I assume that my do/while loop is flawed, but you are all probably more experienced than I am, so any help is appreciated.

#include <iostream>
using namespace std;

int main()
{
    double x, y, z, q, w;
    x = 0;
    cout << "enter y*x + q = z*x + w in order of appearance" << endl;
    cin >> y;
    cin >> q;
    cin >> z;
    cin >> w;

    if ((y-z)/(w-q) > 0) // checks if x is positive
    {
        do
        {
            (x = x + 0.01);
        } while ((y * x + q) == (z * x + w));
    }
    else if ((y - z) / (w - q) < 0) // checks if x is negative
    {
        do
        {
            (x = x - 0.01);
        } while ((y * x + q) == (z * x + w));
    }
    else
    {
        x = 0;
    }
    cout << "x is " << x << endl;
    return 0;
}

Thanks!

  • 1
    dont compare floats with `==` or `!=` , see https://stackoverflow.com/questions/588004/is-floating-point-math-broken – 463035818_is_not_an_ai Nov 21 '19 at 16:53
  • 1
    `while ((y * x + q) == (z * x + w));` is going to evaluate false immediately, wouldn't you want to use `!=` anyways? EDIT: Also, like the commenter above me said (sorry, your name is too long, lol), introduce a tolerance range into your `double` comparisons, since `double`'s are rarely equal. Allow your values to be +-.001 or something like that. Try this: `while ((y * x + q) > ((z * x + w) + .001) && (y * x + q) > ((z * x + w) - .001));` This will remain true while `(y * x + q)` is outside of the acceptable range of values. – alteredinstance Nov 21 '19 at 16:54
  • When I use !=, it always gives value of x as 0. EDIT: How would I implement a tolerance? Could you provide an example? – Curious Coder Nov 21 '19 at 16:58
  • @CuriousCoder Check my edited comment, that may work. Sorry, it's become rather over-packed with information lol – alteredinstance Nov 21 '19 at 17:00
  • Got it, would I have to always do this when doing decimal arithmetic? – Curious Coder Nov 21 '19 at 17:01
  • @CuriousCoder unfortunately, yes. My suggestion probably isnt a best practice, but decimal arithmetic (better known as Floating Point Arithmetic in comp sci) is not "perfect", and will almost always leave a trailing value. e.g., we have no way of accurately representing irrational numbers or numbers with 300+ (guessing here) decimal positions. – alteredinstance Nov 21 '19 at 17:06

1 Answers1

1

A few things.

First, when comparing floats, you probably want to do so within a tight range, frequently referred to as epsilon. This is especially true because you're incrementing by a fairly wide margin -- 0.01. You are probably skipping over the value you want.

What I would do is pay attention to:

  • Am I getting closer or further from the answer?
  • And did I skip over the answer?

Some code:

float leftSide = y * x + q;
float rightSide = z * x + w;
float delta = leftSide - rightSide;
if (abs(delta) < epsilon) {
    // You're really close
}

Note also this will never work if y and z are identical values unless q and w are as well.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36