0

Hi I am wondering if there is good way to compare a "double" value up to some decimal places! For example here is my code:

else if (choice == 2)
            {
                srand(seed);

                firstVal = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
                secondVal = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
                ans = firstVal / secondVal; //this part!

                cout << "Generating Values..";
                pause(3);
                cout << endl;

                cout << firstVal << endl;
                cout << secondVal << endl;

                cout << "Please enter the division of two Values(top value / bottom): ";
                cin >> userAns;

                if (userAns == ans) //This part!!
                    cout << "Congraturation. You got right answer" << endl;
                else
                    cout << "Wrong dummy!" << endl;

            }

I have tested some numbers. They works if there is no remainder but when it comes to decimal places, how should I compare the values???? for example if the answer comes out to be irrational number like 3.979797979797....

goosamsf
  • 19
  • 1
  • 2
  • subtraction, and then compare to your max difference? – Mooing Duck May 19 '21 at 20:35
  • Sadly irrational numbers are the least of your worries. `double` (and `float`) values are imprecise, so any application of `==` is prone to failure. For example on one side you could have 10.0000000003 and on the other 9.9999999997. – user4581301 May 19 '21 at 20:36
  • 3
    All real numbers that a `double` can represent are rational. `double`s do not use "decimal places". – Drew Dormann May 19 '21 at 20:40
  • irrational numbers are cool in maths, because they fill unpleasant gaps, but practically useless anywhere else. Its just impossible to measure something with infinite precision. – 463035818_is_not_an_ai May 19 '21 at 20:54
  • I like to start with Knuth's algorithms for [comparing floating point numbers](https://stackoverflow.com/a/253874/4641116). And then adjust (if needed) from there. – Eljay May 19 '21 at 22:57
  • *"irrational number like 3.979797979797...."* -- the number you chose is the ratio of the integers `394` and `99`; it is rational by definition. In fact, one characterization of a rational number is one whose decimal representation ends with a repeating pattern of digits (such as your repeating `97`). – JaMiT May 20 '21 at 00:14
  • To more fully specify your requirements, you should give an example of your desired behavior. For example, if asking for the *quotient* of `394` and `99`, should `4` be an acceptable answer? How about `4.0` or `3.98` or `3.97980` or `3.979797979797979797979797979797979797979797979797979797979797979798`? Do you want to require a certain level of precision, or should the program adapt to the precision of the entered value? – JaMiT May 21 '21 at 00:49

1 Answers1

2

One thing is very important: computers don't understand the concept of irrational numbers.

In case you need to verify if a number "equals" an "irrational" number (like sqrt(2) or pi), the best thing you can do is agree on an accuracy and a way to calculate that (absolute or relative) and use that same system for the whole application.

Example: take 1E-12 as the accuracy and use relative calculation. Then, you can check a number (let's say double d) as follows:

abs((d - sqrt(2)) / sqrt(2)) <= 1E-12
Dominique
  • 16,450
  • 15
  • 56
  • 112